From 62c7038a493d89e4a7b14ac673a063d09d04057b Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Sat, 23 Nov 2019 02:20:33 +0900 Subject: [PATCH] Fix: invalid token checking in computed-property-spacing (fixes #12198) (#12533) * Fix: invalid token checking in computed-property-spacing (fixes #12198) * Add more test cases --- lib/rules/computed-property-spacing.js | 8 +- tests/lib/rules/computed-property-spacing.js | 393 +++++++++++++++++++ 2 files changed, 397 insertions(+), 4 deletions(-) diff --git a/lib/rules/computed-property-spacing.js b/lib/rules/computed-property-spacing.js index bc8be964f4f..a0bd7f48ced 100644 --- a/lib/rules/computed-property-spacing.js +++ b/lib/rules/computed-property-spacing.js @@ -153,10 +153,10 @@ module.exports = { const property = node[propertyName]; - const before = sourceCode.getTokenBefore(property), - first = sourceCode.getFirstToken(property), - last = sourceCode.getLastToken(property), - after = sourceCode.getTokenAfter(property); + const before = sourceCode.getTokenBefore(property, astUtils.isOpeningBracketToken), + first = sourceCode.getTokenAfter(before, { includeComments: true }), + after = sourceCode.getTokenAfter(property, astUtils.isClosingBracketToken), + last = sourceCode.getTokenBefore(after, { includeComments: true }); if (astUtils.isTokenOnSameLine(before, first)) { if (propertyNameMustBeSpaced) { diff --git a/tests/lib/rules/computed-property-spacing.js b/tests/lib/rules/computed-property-spacing.js index 3901d8c3fb3..3a0bd9e0e0b 100644 --- a/tests/lib/rules/computed-property-spacing.js +++ b/tests/lib/rules/computed-property-spacing.js @@ -183,6 +183,87 @@ ruleTester.run("computed-property-spacing", rule, { code: "A = class {a(){}get b(){}set b(foo){}static c(){}static get d(){}static set d(bar){}}", options: ["always", { enforceForClassMembers: true }], parserOptions: { ecmaVersion: 6 } + }, + { + code: [ + "const foo = {", + " [ (a) ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: [ + "const foo = {", + " [ ( a ) ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: [ + "const foo = {", + " [( a )]: 1", + "}" + ].join("\n"), + options: ["never"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: [ + "const foo = {", + " [ /**/ a /**/ ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: [ + "const foo = {", + " [/**/ a /**/]: 1", + "}" + ].join("\n"), + options: ["never"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: [ + "const foo = {", + " [ a[ b ] ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: [ + "const foo = {", + " [a[b]]: 1", + "}" + ].join("\n"), + options: ["never"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: [ + "const foo = {", + " [ a[ /**/ b ]/**/ ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: [ + "const foo = {", + " [/**/a[b /**/] /**/]: 1", + "}" + ].join("\n"), + options: ["never"], + parserOptions: { ecmaVersion: 6 } } ], @@ -757,6 +838,318 @@ ruleTester.run("computed-property-spacing", rule, { line: 1 } ] + }, + { + code: [ + "const foo = {", + " [(a)]: 1", + "}" + ].join("\n"), + output: [ + "const foo = {", + " [ (a) ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "missingSpaceAfter", + data: { tokenValue: "[" }, + type: "Property", + column: 3, + line: 2 + }, + { + messageId: "missingSpaceBefore", + data: { tokenValue: "]" }, + type: "Property", + column: 7, + line: 2 + } + ] + }, + { + code: [ + "const foo = {", + " [( a )]: 1", + "}" + ].join("\n"), + output: [ + "const foo = {", + " [ ( a ) ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "missingSpaceAfter", + data: { tokenValue: "[" }, + type: "Property", + column: 3, + line: 2 + }, + { + messageId: "missingSpaceBefore", + data: { tokenValue: "]" }, + type: "Property", + column: 9, + line: 2 + } + ] + }, + { + code: [ + "const foo = {", + " [ ( a ) ]: 1", + "}" + ].join("\n"), + output: [ + "const foo = {", + " [( a )]: 1", + "}" + ].join("\n"), + options: ["never"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedSpaceAfter", + data: { tokenValue: "[" }, + type: "Property", + column: 3, + line: 2 + }, + { + messageId: "unexpectedSpaceBefore", + data: { tokenValue: "]" }, + type: "Property", + column: 11, + line: 2 + } + ] + }, + { + code: [ + "const foo = {", + " [/**/ a /**/]: 1", + "}" + ].join("\n"), + output: [ + "const foo = {", + " [ /**/ a /**/ ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "missingSpaceAfter", + data: { tokenValue: "[" }, + type: "Property", + column: 3, + line: 2 + }, + { + messageId: "missingSpaceBefore", + data: { tokenValue: "]" }, + type: "Property", + column: 15, + line: 2 + } + ] + }, + { + code: [ + "const foo = {", + " [ /**/ a /**/ ]: 1", + "}" + ].join("\n"), + output: [ + "const foo = {", + " [/**/ a /**/]: 1", + "}" + ].join("\n"), + options: ["never"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedSpaceAfter", + data: { tokenValue: "[" }, + type: "Property", + column: 3, + line: 2 + }, + { + messageId: "unexpectedSpaceBefore", + data: { tokenValue: "]" }, + type: "Property", + column: 17, + line: 2 + } + ] + }, + { + code: [ + "const foo = {", + " [a[b]]: 1", + "}" + ].join("\n"), + output: [ + "const foo = {", + " [ a[ b ] ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "missingSpaceAfter", + data: { tokenValue: "[" }, + type: "Property", + column: 3, + line: 2 + }, + { + messageId: "missingSpaceAfter", + data: { tokenValue: "[" }, + type: "MemberExpression", + column: 5, + line: 2 + }, + { + messageId: "missingSpaceBefore", + data: { tokenValue: "]" }, + type: "MemberExpression", + column: 7, + line: 2 + }, + { + messageId: "missingSpaceBefore", + data: { tokenValue: "]" }, + type: "Property", + column: 8, + line: 2 + } + ] + }, + { + code: [ + "const foo = {", + " [ a[ b ] ]: 1", + "}" + ].join("\n"), + output: [ + "const foo = {", + " [a[b]]: 1", + "}" + ].join("\n"), + options: ["never"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedSpaceAfter", + data: { tokenValue: "[" }, + type: "Property", + column: 3, + line: 2 + }, + { + messageId: "unexpectedSpaceAfter", + data: { tokenValue: "[" }, + type: "MemberExpression", + column: 6, + line: 2 + }, + { + messageId: "unexpectedSpaceBefore", + data: { tokenValue: "]" }, + type: "MemberExpression", + column: 10, + line: 2 + }, + { + messageId: "unexpectedSpaceBefore", + data: { tokenValue: "]" }, + type: "Property", + column: 12, + line: 2 + } + ] + }, + { + code: [ + "const foo = {", + " [a[/**/ b ]/**/]: 1", + "}" + ].join("\n"), + output: [ + "const foo = {", + " [ a[ /**/ b ]/**/ ]: 1", + "}" + ].join("\n"), + options: ["always"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "missingSpaceAfter", + data: { tokenValue: "[" }, + type: "Property", + column: 3, + line: 2 + }, + { + messageId: "missingSpaceAfter", + data: { tokenValue: "[" }, + type: "MemberExpression", + column: 5, + line: 2 + }, + { + messageId: "missingSpaceBefore", + data: { tokenValue: "]" }, + type: "Property", + column: 18, + line: 2 + } + ] + }, + { + code: [ + "const foo = {", + " [ /**/a[ b /**/ ] /**/]: 1", + "}" + ].join("\n"), + output: [ + "const foo = {", + " [/**/a[b /**/] /**/]: 1", + "}" + ].join("\n"), + options: ["never"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedSpaceAfter", + data: { tokenValue: "[" }, + type: "Property", + column: 3, + line: 2 + }, + { + messageId: "unexpectedSpaceAfter", + data: { tokenValue: "[" }, + type: "MemberExpression", + column: 10, + line: 2 + }, + { + messageId: "unexpectedSpaceBefore", + data: { tokenValue: "]" }, + type: "MemberExpression", + column: 19, + line: 2 + } + ] } ] });