From 32484efb848e55f19b30dbbd4ff053160b037521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sajn=C3=B3g?= Date: Sun, 23 Sep 2018 23:20:51 +0100 Subject: [PATCH 1/2] Add "ignoreProperties" option to "no-multi-spaces" rule --- docs/rules/no-multi-spaces.md | 43 ++++++++++++++++++++++++++++-- lib/rules/no-multi-spaces.js | 23 ++++++++++++---- tests/lib/rules/no-multi-spaces.js | 43 ++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 7 deletions(-) diff --git a/docs/rules/no-multi-spaces.md b/docs/rules/no-multi-spaces.md index d6c648c7e..666d6e10d 100644 --- a/docs/rules/no-multi-spaces.md +++ b/docs/rules/no-multi-spaces.md @@ -16,6 +16,15 @@ Examples of **incorrect** code for this rule: :style="bar" /> ``` +```html + +``` + Examples of **correct** code for this rule: ```html @@ -25,6 +34,36 @@ Examples of **correct** code for this rule: /> ``` -### Options +```html + +``` + +## :wrench: Options + +This rule has an object option: -Nothing +`"ignoreProperties": false` (default) whether or not objects' properties should be ignored + +### Example: + +```json +"vue/no-multi-spaces": [2, { + "ignoreProperties": true +}] +``` + +:+1: Examples of **correct** code for this rule: + +```html + +``` diff --git a/lib/rules/no-multi-spaces.js b/lib/rules/no-multi-spaces.js index adc63ce0a..0d5f7eb86 100644 --- a/lib/rules/no-multi-spaces.js +++ b/lib/rules/no-multi-spaces.js @@ -8,6 +8,11 @@ // Rule Definition // ------------------------------------------------------------------------------ +const isProperty = (context, node) => { + const sourceCode = context.getSourceCode() + return node.type === 'Punctuator' && sourceCode.getText(node) === ':' +} + module.exports = { meta: { docs: { @@ -16,7 +21,15 @@ module.exports = { url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/no-multi-spaces.md' }, fixable: 'whitespace', // or "code" or "whitespace" - schema: [] + schema: [{ + type: 'object', + properties: { + ignoreProperties: { + type: 'boolean' + } + }, + additionalProperties: false + }] }, /** @@ -24,9 +37,8 @@ module.exports = { * @returns {Object} AST event handlers. */ create (context) { - // ---------------------------------------------------------------------- - // Public - // ---------------------------------------------------------------------- + const options = context.options[0] || {} + const ignoreProperties = options.ignoreProperties === true return { Program (node) { @@ -47,7 +59,8 @@ module.exports = { let prevToken = tokens.shift() for (const token of tokens) { const spaces = token.range[0] - prevToken.range[1] - if (spaces > 1 && token.loc.start.line === prevToken.loc.start.line) { + const shouldIgnore = ignoreProperties && isProperty(context, token) + if (spaces > 1 && token.loc.start.line === prevToken.loc.start.line && !shouldIgnore) { context.report({ node: token, loc: { diff --git a/tests/lib/rules/no-multi-spaces.js b/tests/lib/rules/no-multi-spaces.js index 5238db724..ec59e43ed 100644 --- a/tests/lib/rules/no-multi-spaces.js +++ b/tests/lib/rules/no-multi-spaces.js @@ -44,6 +44,21 @@ ruleTester.run('no-multi-spaces', rule, { { filename: 'test.js', code: 'export default { }' + }, + { + code: ` + + `, + options: [{ + ignoreProperties: true + }] } ], invalid: [ @@ -176,6 +191,34 @@ ruleTester.run('no-multi-spaces', rule, { type: 'Punctuator' } ] + }, + { + code: ` + + `, + output: ` + + `, + errors: [ + { + message: "Multiple spaces found before ':'.", + type: 'Punctuator' + } + ] } ] }) From 7b603d1556acbf7e0b6d117998652db7132a4e72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sajn=C3=B3g?= Date: Wed, 7 Nov 2018 15:06:13 +0100 Subject: [PATCH 2/2] Handle extra case --- lib/rules/no-multi-spaces.js | 4 ++- tests/lib/rules/no-multi-spaces.js | 43 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-multi-spaces.js b/lib/rules/no-multi-spaces.js index 0d5f7eb86..de91541c0 100644 --- a/lib/rules/no-multi-spaces.js +++ b/lib/rules/no-multi-spaces.js @@ -59,7 +59,9 @@ module.exports = { let prevToken = tokens.shift() for (const token of tokens) { const spaces = token.range[0] - prevToken.range[1] - const shouldIgnore = ignoreProperties && isProperty(context, token) + const shouldIgnore = ignoreProperties && ( + isProperty(context, token) || isProperty(context, prevToken) + ) if (spaces > 1 && token.loc.start.line === prevToken.loc.start.line && !shouldIgnore) { context.report({ node: token, diff --git a/tests/lib/rules/no-multi-spaces.js b/tests/lib/rules/no-multi-spaces.js index ec59e43ed..68433af44 100644 --- a/tests/lib/rules/no-multi-spaces.js +++ b/tests/lib/rules/no-multi-spaces.js @@ -59,6 +59,21 @@ ruleTester.run('no-multi-spaces', rule, { options: [{ ignoreProperties: true }] + }, + { + code: ` + + `, + options: [{ + ignoreProperties: true + }] } ], invalid: [ @@ -219,6 +234,34 @@ ruleTester.run('no-multi-spaces', rule, { type: 'Punctuator' } ] + }, + { + code: ` + + `, + output: ` + + `, + errors: [ + { + message: "Multiple spaces found before 'isExpanded'.", + type: 'Identifier' + } + ] } ] })