Skip to content

Commit

Permalink
Fix: keyword-spacing conflict with space-infix-ops on > (fixes #14712
Browse files Browse the repository at this point in the history
…) (#15172)

* Add regression tests

* Add passing tests

* Add more passing tests

* Add failing tests

* Fix the issue
  • Loading branch information
mdjermanovic committed Oct 22, 2021
1 parent a1f7ad7 commit d9d84a0
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 2 deletions.
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: [],
});

0 comments on commit d9d84a0

Please sign in to comment.