Skip to content

Commit

Permalink
highlight escape sequence rather than quasi (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j authored Jun 7, 2021
1 parent 27a6e8b commit 51c53c7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
30 changes: 22 additions & 8 deletions src/rules/no-invalid-escape-sequences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ const rule: Rule.RuleModule = {
url:
'https://github.com/43081j/eslint-plugin-lit/blob/master/docs/rules/no-invalid-escape-sequences.md'
},
fixable: 'code',
messages: {
invalid:
'Some escape sequences are invalid in template strings. ' +
'They should either be escaped again (e.g. "\\02c") or interpolated'
'They should either be escaped again (e.g. "\\\\02c") or interpolated'
}
},

create(context): Rule.RuleListener {
// variables should be defined here
const escapePattern = /(^|[^\\])\\([1-7][0-7]*|[0-7]{2,})+/;
const source = context.getSourceCode();
const escapePattern = /(^|[^\\](?:\\\\)*)(\\([1-7][0-7]*|[0-7]{2,}))/g;

return {
TaggedTemplateExpression: (node: ESTree.Node): void => {
Expand All @@ -37,11 +38,24 @@ const rule: Rule.RuleModule = {
node.tag.name === 'html'
) {
for (const quasi of node.quasi.quasis) {
if (escapePattern.test(quasi.value.raw)) {
context.report({
node: quasi,
messageId: 'invalid'
});
if (quasi.range) {
const results = quasi.value.raw.matchAll(escapePattern);

for (const match of results) {
if (match.index !== undefined) {
const rangeStart =
quasi.range[0] + 1 + match.index + match[1].length;
const rangeEnd = rangeStart + match[2].length;
const start = source.getLocFromIndex(rangeStart);
const end = source.getLocFromIndex(rangeEnd);
context.report({
loc: {start, end},
messageId: 'invalid',
fix: (fixer) =>
fixer.insertTextBeforeRange([rangeStart, rangeEnd], '\\')
});
}
}
}
}
}
Expand Down
35 changes: 31 additions & 4 deletions src/test/rules/no-invalid-escape-sequences_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ ruleTester.run('no-invalid-escape-sequences', rule, {
{
messageId: 'invalid',
line: 1,
column: 5
column: 10,
endLine: 1,
endColumn: 15
}
]
],
output: 'html`foo \\\\0123 bar`'
},
{
code: 'html`foo \\3c bar`',
Expand All @@ -51,9 +54,33 @@ ruleTester.run('no-invalid-escape-sequences', rule, {
{
messageId: 'invalid',
line: 1,
column: 5
column: 10,
endLine: 1,
endColumn: 12
}
]
],
output: 'html`foo \\\\3c bar`'
},
{
code: 'html`foo \\3c bar \\33`',
parserOptions: {ecmaVersion: 2018},
errors: [
{
messageId: 'invalid',
line: 1,
column: 10,
endLine: 1,
endColumn: 12
},
{
messageId: 'invalid',
line: 1,
column: 18,
endLine: 1,
endColumn: 21
}
],
output: 'html`foo \\\\3c bar \\\\33`'
}
]
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2017",
"target": "es2020",
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
Expand Down

0 comments on commit 51c53c7

Please sign in to comment.