Skip to content

Commit

Permalink
Fix escape-case fixer bug on TemplateElement (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 12, 2020
1 parent bd79401 commit cb24d44
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
14 changes: 6 additions & 8 deletions rules/escape-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
} = require('regexpp');

const getDocumentationUrl = require('./utils/get-documentation-url');
const replaceTemplateElement = require('./utils/replace-template-element');

const escapeWithLowercase = /(?<before>(?:^|[^\\])(?:\\\\)*)\\(?<data>x[\da-f]{2}|u[\da-f]{4}|u{[\da-f]+})/;
const escapePatternWithLowercase = /(?<before>(?:^|[^\\])(?:\\\\)*)\\(?<data>x[\da-f]{2}|u[\da-f]{4}|u{[\da-f]+}|c[a-z])/;
Expand Down Expand Up @@ -113,20 +114,17 @@ const create = context => {
}
},
TemplateElement(node) {
if (typeof node.value.raw !== 'string') {
return;
}

const matches = node.value.raw.match(escapeWithLowercase);

if (matches && matches[2].slice(1).match(hasLowercaseCharacter)) {
// Move cursor inside the head and tail apostrophe
const start = node.range[0] + 1;
const end = node.range[1] - 1;
context.report({
node,
message,
fix: fixer => fixer.replaceTextRange([start, end], fix(node.value.raw, escapeWithLowercase))
fix: fixer => replaceTemplateElement(
fixer,
node,
fix(node.value.raw, escapeWithLowercase)
)
});
}
}
Expand Down
15 changes: 5 additions & 10 deletions rules/no-hex-escape.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const getDocumentationUrl = require('./utils/get-documentation-url');
const replaceTemplateElement = require('./utils/replace-template-element');

function checkEscape(context, node, value) {
const fixedValue = typeof value === 'string' ?
Expand All @@ -10,16 +11,10 @@ function checkEscape(context, node, value) {
context.report({
node,
message: 'Use Unicode escapes instead of hexadecimal escapes.',
fix: fixer => {
let {range: [start, end], type, tail} = node;

if (type === 'TemplateElement') {
start += 1;
end -= tail ? 1 : 2;
}

return fixer.replaceTextRange([start, end], fixedValue);
}
fix: fixer =>
node.type === 'TemplateElement' ?
replaceTemplateElement(fixer, node, fixedValue) :
fixer.replaceText(node, fixedValue)
});
}
}
Expand Down
9 changes: 9 additions & 0 deletions rules/utils/replace-template-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = (fixer, node, replacement) => {
const {range: [start, end], tail} = node;
return fixer.replaceTextRange(
[start + 1, end - (tail ? 1 : 2)],
replacement
);
};
5 changes: 5 additions & 0 deletions test/escape-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ ruleTester.run('escape-case', rule, {
errors,
output: 'const foo = `${"\uD834 foo"} \\uD834`;'
},
{
code: 'const foo = `\\ud834${foo}\\ud834${foo}\\ud834`;',
errors: Array.from({length: 3}, () => errors[0]),
output: 'const foo = `\\uD834${foo}\\uD834${foo}\\uD834`;'
},
{
code: 'const foo = "\\ud834foo";',
errors,
Expand Down

0 comments on commit cb24d44

Please sign in to comment.