Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: Improve report location for template-curly-spacing (#12813)
  • Loading branch information
mdjermanovic committed Mar 28, 2020
1 parent 29f32db commit 362713c
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 117 deletions.
101 changes: 59 additions & 42 deletions lib/rules/template-curly-spacing.js
Expand Up @@ -11,13 +11,6 @@

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const OPEN_PAREN = /\$\{$/u;
const CLOSE_PAREN = /^\}/u;

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -49,33 +42,46 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();
const always = context.options[0] === "always";
const prefix = always ? "expected" : "unexpected";

/**
* Checks spacing before `}` of a given token.
* @param {Token} token A token to check. This is a Template token.
* @returns {void}
*/
function checkSpacingBefore(token) {
const prevToken = sourceCode.getTokenBefore(token, { includeComments: true });
if (!token.value.startsWith("}")) {
return; // starts with a backtick, this is the first template element in the template literal
}

const prevToken = sourceCode.getTokenBefore(token, { includeComments: true }),
hasSpace = sourceCode.isSpaceBetween(prevToken, token);

if (!astUtils.isTokenOnSameLine(prevToken, token)) {
return;
}

if (prevToken &&
CLOSE_PAREN.test(token.value) &&
astUtils.isTokenOnSameLine(prevToken, token) &&
sourceCode.isSpaceBetweenTokens(prevToken, token) !== always
) {
if (always && !hasSpace) {
context.report({
loc: token.loc.start,
messageId: `${prefix}Before`,
fix(fixer) {
if (always) {
return fixer.insertTextBefore(token, " ");
loc: {
start: token.loc.start,
end: {
line: token.loc.start.line,
column: token.loc.start.column + 1
}
return fixer.removeRange([
prevToken.range[1],
token.range[0]
]);
}
},
messageId: "expectedBefore",
fix: fixer => fixer.insertTextBefore(token, " ")
});
}

if (!always && hasSpace) {
context.report({
loc: {
start: prevToken.loc.end,
end: token.loc.start
},
messageId: "unexpectedBefore",
fix: fixer => fixer.removeRange([prevToken.range[1], token.range[0]])
});
}
}
Expand All @@ -86,28 +92,39 @@ module.exports = {
* @returns {void}
*/
function checkSpacingAfter(token) {
const nextToken = sourceCode.getTokenAfter(token, { includeComments: true });
if (!token.value.endsWith("${")) {
return; // ends with a backtick, this is the last template element in the template literal
}

const nextToken = sourceCode.getTokenAfter(token, { includeComments: true }),
hasSpace = sourceCode.isSpaceBetween(token, nextToken);

if (nextToken &&
OPEN_PAREN.test(token.value) &&
astUtils.isTokenOnSameLine(token, nextToken) &&
sourceCode.isSpaceBetweenTokens(token, nextToken) !== always
) {
if (!astUtils.isTokenOnSameLine(token, nextToken)) {
return;
}

if (always && !hasSpace) {
context.report({
loc: {
line: token.loc.end.line,
column: token.loc.end.column - 2
start: {
line: token.loc.end.line,
column: token.loc.end.column - 2
},
end: token.loc.end
},
messageId: `${prefix}After`,
fix(fixer) {
if (always) {
return fixer.insertTextAfter(token, " ");
}
return fixer.removeRange([
token.range[1],
nextToken.range[0]
]);
}
messageId: "expectedAfter",
fix: fixer => fixer.insertTextAfter(token, " ")
});
}

if (!always && hasSpace) {
context.report({
loc: {
start: token.loc.end,
end: nextToken.loc.start
},
messageId: "unexpectedAfter",
fix: fixer => fixer.removeRange([token.range[1], nextToken.range[0]])
});
}
}
Expand Down

0 comments on commit 362713c

Please sign in to comment.