2020-03-07 11:45:40 +08:00
|
|
|
/**
|
|
|
|
* @fileoverview Rule to enforce spacing around embedded expressions of template strings
|
|
|
|
* @author Toru Nagashima
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Requirements
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
const astUtils = require("./utils/ast-utils");
|
2020-03-07 11:45:40 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
type: "layout",
|
|
|
|
|
|
|
|
docs: {
|
|
|
|
description: "require or disallow spacing around embedded expressions of template strings",
|
|
|
|
category: "ECMAScript 6",
|
|
|
|
recommended: false,
|
|
|
|
url: "https://eslint.org/docs/rules/template-curly-spacing"
|
|
|
|
},
|
|
|
|
|
|
|
|
fixable: "whitespace",
|
|
|
|
|
|
|
|
schema: [
|
|
|
|
{ enum: ["always", "never"] }
|
|
|
|
],
|
|
|
|
messages: {
|
|
|
|
expectedBefore: "Expected space(s) before '}'.",
|
|
|
|
expectedAfter: "Expected space(s) after '${'.",
|
|
|
|
unexpectedBefore: "Unexpected space(s) before '}'.",
|
|
|
|
unexpectedAfter: "Unexpected space(s) after '${'."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
|
|
|
const sourceCode = context.getSourceCode();
|
|
|
|
const always = context.options[0] === "always";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks spacing before `}` of a given token.
|
2020-03-31 20:40:00 +08:00
|
|
|
* @param {Token} token A token to check. This is a Template token.
|
2020-03-07 11:45:40 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function checkSpacingBefore(token) {
|
2020-05-15 05:33:08 +08:00
|
|
|
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;
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-05-15 05:33:08 +08:00
|
|
|
if (always && !hasSpace) {
|
2020-03-07 11:45:40 +08:00
|
|
|
context.report({
|
2020-05-15 05:33:08 +08:00
|
|
|
loc: {
|
|
|
|
start: token.loc.start,
|
|
|
|
end: {
|
|
|
|
line: token.loc.start.line,
|
|
|
|
column: token.loc.start.column + 1
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
2020-05-15 05:33:08 +08:00
|
|
|
},
|
|
|
|
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]])
|
2020-03-07 11:45:40 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks spacing after `${` of a given token.
|
2020-03-31 20:40:00 +08:00
|
|
|
* @param {Token} token A token to check. This is a Template token.
|
2020-03-07 11:45:40 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function checkSpacingAfter(token) {
|
2020-05-15 05:33:08 +08:00
|
|
|
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);
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-05-15 05:33:08 +08:00
|
|
|
if (!astUtils.isTokenOnSameLine(token, nextToken)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (always && !hasSpace) {
|
2020-03-07 11:45:40 +08:00
|
|
|
context.report({
|
|
|
|
loc: {
|
2020-05-15 05:33:08 +08:00
|
|
|
start: {
|
|
|
|
line: token.loc.end.line,
|
|
|
|
column: token.loc.end.column - 2
|
|
|
|
},
|
|
|
|
end: token.loc.end
|
2020-03-07 11:45:40 +08:00
|
|
|
},
|
2020-05-15 05:33:08 +08:00
|
|
|
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]])
|
2020-03-07 11:45:40 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
TemplateElement(node) {
|
|
|
|
const token = sourceCode.getFirstToken(node);
|
|
|
|
|
|
|
|
checkSpacingBefore(token);
|
|
|
|
checkSpacingAfter(token);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|