Skip to content

Commit

Permalink
feat: "Convert to ES6 template" suggestion for "no-template-curly-in-…
Browse files Browse the repository at this point in the history
…string"
  • Loading branch information
ajuanjojjj committed Feb 4, 2022
1 parent 389ff34 commit f49dde9
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/rules/no-template-curly-in-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,34 @@ module.exports = {
schema: [],

messages: {
unexpectedTemplateExpression: "Unexpected template string expression."
}
unexpectedTemplateExpression: "Unexpected template string expression.",
convertToTemplate: "Convert to ES6 `template`."
},
hasSuggestions: true
},

create(context) {
const regex = /\$\{[^}]+\}/u;
const sourceCode = context.getSourceCode();

return {
Literal(node) {
if (typeof node.value === "string" && regex.test(node.value)) {
context.report({
node,
messageId: "unexpectedTemplateExpression"
messageId: "unexpectedTemplateExpression",
suggest: [
{
messageId: "convertToTemplate",
fix: fixer => {
const text = sourceCode.getText(node);
const textNoQuotes = text.slice(1, -1);
const newText = `\`${textNoQuotes}\``;

return fixer.replaceText(node, newText);
}
}
]
});
}
}
Expand Down

0 comments on commit f49dde9

Please sign in to comment.