Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: keyword-spacing conflict with space-infix-ops on > (fixes #14712) #15172

Merged
merged 5 commits into from Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/rules/keyword-spacing.js
Expand Up @@ -109,6 +109,8 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();

const tokensToIgnore = new WeakSet();

/**
* Reports a given token if there are not space(s) before the token.
* @param {Token} token A token to report.
Expand All @@ -121,6 +123,7 @@ module.exports = {
if (prevToken &&
(CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) &&
!isOpenParenOfTemplate(prevToken) &&
!tokensToIgnore.has(prevToken) &&
astUtils.isTokenOnSameLine(prevToken, token) &&
!sourceCode.isSpaceBetweenTokens(prevToken, token)
) {
Expand All @@ -147,6 +150,7 @@ module.exports = {
if (prevToken &&
(CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) &&
!isOpenParenOfTemplate(prevToken) &&
!tokensToIgnore.has(prevToken) &&
astUtils.isTokenOnSameLine(prevToken, token) &&
sourceCode.isSpaceBetweenTokens(prevToken, token)
) {
Expand All @@ -173,6 +177,7 @@ module.exports = {
if (nextToken &&
(CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) &&
!isCloseParenOfTemplate(nextToken) &&
!tokensToIgnore.has(nextToken) &&
astUtils.isTokenOnSameLine(token, nextToken) &&
!sourceCode.isSpaceBetweenTokens(token, nextToken)
) {
Expand All @@ -199,6 +204,7 @@ module.exports = {
if (nextToken &&
(CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) &&
!isCloseParenOfTemplate(nextToken) &&
!tokensToIgnore.has(nextToken) &&
astUtils.isTokenOnSameLine(token, nextToken) &&
sourceCode.isSpaceBetweenTokens(token, nextToken)
) {
Expand Down Expand Up @@ -584,7 +590,14 @@ module.exports = {
ImportNamespaceSpecifier: checkSpacingForImportNamespaceSpecifier,
MethodDefinition: checkSpacingForProperty,
PropertyDefinition: checkSpacingForProperty,
Property: checkSpacingForProperty
Property: checkSpacingForProperty,

// To avoid conflicts with `space-infix-ops`, e.g. `a > this.b`
"BinaryExpression[operator='>']"(node) {
const operatorToken = sourceCode.getTokenBefore(node.right, astUtils.isNotOpeningParenToken);

tokensToIgnore.add(operatorToken);
}
};
}
};
@@ -0,0 +1,125 @@
"use strict";

/**
* Parser: @typescript-eslint/parser@5.0.0
*
* Source code:
* <Thing>this.blah
*/

exports.parse = () => ({
type: "Program",
body: [
{
type: "ExpressionStatement",
expression: {
type: "TSTypeAssertion",
typeAnnotation: {
type: "TSTypeReference",
typeName: {
type: "Identifier",
name: "Thing",
range: [1, 6],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 6 },
},
},
range: [1, 6],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 6 },
},
},
expression: {
type: "MemberExpression",
object: {
type: "ThisExpression",
range: [7, 11],
loc: {
start: { line: 1, column: 7 },
end: { line: 1, column: 11 },
},
},
property: {
type: "Identifier",
name: "blah",
range: [12, 16],
loc: {
start: { line: 1, column: 12 },
end: { line: 1, column: 16 },
},
},
computed: false,
optional: false,
range: [7, 16],
loc: {
start: { line: 1, column: 7 },
end: { line: 1, column: 16 },
},
},
range: [0, 16],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 16 },
},
},
range: [0, 16],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 16 },
},
},
],
sourceType: "script",
range: [0, 16],
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 16 } },
tokens: [
{
type: "Punctuator",
value: "<",
range: [0, 1],
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 1 } },
},
{
type: "Identifier",
value: "Thing",
range: [1, 6],
loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 6 } },
},
{
type: "Punctuator",
value: ">",
range: [6, 7],
loc: { start: { line: 1, column: 6 }, end: { line: 1, column: 7 } },
},
{
type: "Keyword",
value: "this",
range: [7, 11],
loc: {
start: { line: 1, column: 7 },
end: { line: 1, column: 11 },
},
},
{
type: "Punctuator",
value: ".",
range: [11, 12],
loc: {
start: { line: 1, column: 11 },
end: { line: 1, column: 12 },
},
},
{
type: "Identifier",
value: "blah",
range: [12, 16],
loc: {
start: { line: 1, column: 12 },
end: { line: 1, column: 16 },
},
},
],
comments: [],
});
125 changes: 125 additions & 0 deletions tests/fixtures/parsers/keyword-spacing/prefix-cast-operator-space.js
@@ -0,0 +1,125 @@
"use strict";

/**
* Parser: @typescript-eslint/parser@5.0.0
*
* Source code:
* <Thing> this.blah
*/

exports.parse = () => ({
type: "Program",
body: [
{
type: "ExpressionStatement",
expression: {
type: "TSTypeAssertion",
typeAnnotation: {
type: "TSTypeReference",
typeName: {
type: "Identifier",
name: "Thing",
range: [1, 6],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 6 },
},
},
range: [1, 6],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 6 },
},
},
expression: {
type: "MemberExpression",
object: {
type: "ThisExpression",
range: [8, 12],
loc: {
start: { line: 1, column: 8 },
end: { line: 1, column: 12 },
},
},
property: {
type: "Identifier",
name: "blah",
range: [13, 17],
loc: {
start: { line: 1, column: 13 },
end: { line: 1, column: 17 },
},
},
computed: false,
optional: false,
range: [8, 17],
loc: {
start: { line: 1, column: 8 },
end: { line: 1, column: 17 },
},
},
range: [0, 17],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 17 },
},
},
range: [0, 17],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 17 },
},
},
],
sourceType: "script",
range: [0, 17],
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 17 } },
tokens: [
{
type: "Punctuator",
value: "<",
range: [0, 1],
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 1 } },
},
{
type: "Identifier",
value: "Thing",
range: [1, 6],
loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 6 } },
},
{
type: "Punctuator",
value: ">",
range: [6, 7],
loc: { start: { line: 1, column: 6 }, end: { line: 1, column: 7 } },
},
{
type: "Keyword",
value: "this",
range: [8, 12],
loc: {
start: { line: 1, column: 8 },
end: { line: 1, column: 12 },
},
},
{
type: "Punctuator",
value: ".",
range: [12, 13],
loc: {
start: { line: 1, column: 12 },
end: { line: 1, column: 13 },
},
},
{
type: "Identifier",
value: "blah",
range: [13, 17],
loc: {
start: { line: 1, column: 13 },
end: { line: 1, column: 17 },
},
},
],
comments: [],
});