From 1f0d1213235e1fd191dc21d27fed4d46ef2b280a Mon Sep 17 00:00:00 2001 From: Eliott Vincent Date: Thu, 21 Dec 2023 13:38:14 +0100 Subject: [PATCH] v1.0.61 Enforce multiline props --- index.js | 3 +- package-lock.json | 4 +-- package.json | 2 +- recommended-vue.js | 2 ++ rules/vue-props-declaration-multiline.js | 46 ++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 rules/vue-props-declaration-multiline.js diff --git a/index.js b/index.js index 1bd8f2d..61075ec 100644 --- a/index.js +++ b/index.js @@ -36,6 +36,7 @@ module.exports = { "vue-header-check": require("./rules/vue-header-check"), "vue-html-quotes": require("./rules/vue-html-quotes"), "vue-no-regex-data": require("./rules/vue-no-regex-data"), - "vue-props-declaration-order": require("./rules/vue-props-declaration-order") + "vue-props-declaration-order": require("./rules/vue-props-declaration-order"), + "vue-props-declaration-multiline": require("./rules/vue-props-declaration-multiline") } }; diff --git a/package-lock.json b/package-lock.json index 722a153..dbfabfd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "eslint-plugin-crisp", - "version": "1.0.60", + "version": "1.0.61", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "eslint-plugin-crisp", - "version": "1.0.60", + "version": "1.0.61", "license": "MIT", "dependencies": { "doctrine": "3.0.0", diff --git a/package.json b/package.json index 0afcdde..5103f06 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-crisp", - "version": "1.0.60", + "version": "1.0.61", "description": "Custom ESLint Rules for Crisp", "author": "Crisp IM SAS", "main": "index.js", diff --git a/recommended-vue.js b/recommended-vue.js index c3163f5..c406e85 100644 --- a/recommended-vue.js +++ b/recommended-vue.js @@ -238,6 +238,7 @@ module.exports = { "vue/html-quotes": "off", "vue/no-v-html": "off", "vue/prefer-true-attribute-shorthand": "error", + "vue/require-name-property": "error", "vue/comma-dangle": "error", "vue/eqeqeq": "error", "vue/key-spacing": ["error", { "beforeColon": false, "afterColon": true }], @@ -321,6 +322,7 @@ module.exports = { "crisp/vue-header-check": "error", "crisp/vue-html-quotes": "error", "crisp/vue-no-regex-data": "error", + "crisp/vue-props-declaration-multiline": "error", "crisp/vue-props-declaration-order": "error" } } diff --git a/rules/vue-props-declaration-multiline.js b/rules/vue-props-declaration-multiline.js new file mode 100644 index 0000000..9d392be --- /dev/null +++ b/rules/vue-props-declaration-multiline.js @@ -0,0 +1,46 @@ +module.exports = { + meta: { + type: "suggestion", + docs: { + description: "enforce multi-line declaration for Vue props", + category: "Stylistic Issues", + recommended: false, + }, + fixable: "code", + schema: [], // no options + }, + + create(context) { + return { + 'ExportDefaultDeclaration Property[key.name="props"] ObjectExpression'(node) { + node.properties.forEach(prop => { + // Ensuring we only target the top-level prop definitions + if (prop.value.type === "ObjectExpression") { + const propDefinition = prop.value; + + // Check if the prop definition is on a single line + if (propDefinition.loc.start.line === propDefinition.loc.end.line) { + context.report({ + node: prop, + message: "Props should be declared in multiple lines.", + + fix(fixer) { + const sourceCode = context.getSourceCode(); + const propText = sourceCode.getText(propDefinition); + + // Construct the multi-line version of the prop + const expandedProp = propText + .replace("{", "{\n") + .replace("}", "\n}") + .replace(/, /g, ",\n"); + + return fixer.replaceText(propDefinition, expandedProp); + } + }); + } + } + }); + } + }; + } +};