Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [prefer-includes] escape special characters #7161

Merged
merged 3 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 27 additions & 6 deletions packages/eslint-plugin/src/rules/prefer-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ export default createRule({
);
}

function escapeString(str: string): string {
const EscapeMap = {
'\0': '\\0',
"'": "\\'",
'\\': '\\\\',
'\n': '\\n',
'\r': '\\r',
'\v': '\\v',
'\t': '\\t',
'\f': '\\f',
// "\b" cause unexpected replacements
// '\b': '\\b',
};
const replaceRegex = new RegExp(Object.values(EscapeMap).join('|'), 'g');

return str.replace(
replaceRegex,
char => EscapeMap[char as keyof typeof EscapeMap],
);
}

function checkArrayIndexOf(
node: TSESTree.MemberExpression,
allowFixing: boolean,
Expand Down Expand Up @@ -202,12 +223,11 @@ export default createRule({
},

// /bar/.test(foo)
'CallExpression > MemberExpression.callee[property.name="test"][computed=false]'(
node: TSESTree.MemberExpression,
'CallExpression[arguments.length=1] > MemberExpression.callee[property.name="test"][computed=false]'(
node: TSESTree.MemberExpression & { parent: TSESTree.CallExpression },
): void {
const callNode = node.parent as TSESTree.CallExpression;
const text =
callNode.arguments.length === 1 ? parseRegExp(node.object) : null;
const callNode = node.parent;
const text = parseRegExp(node.object);
if (text == null) {
return;
}
Expand Down Expand Up @@ -237,13 +257,14 @@ export default createRule({
argNode.type !== AST_NODE_TYPES.CallExpression;

yield fixer.removeRange([callNode.range[0], argNode.range[0]]);
yield fixer.removeRange([argNode.range[1], callNode.range[1]]);
if (needsParen) {
yield fixer.insertTextBefore(argNode, '(');
yield fixer.insertTextAfter(argNode, ')');
}
yield fixer.insertTextAfter(
argNode,
`${node.optional ? '?.' : '.'}includes('${text}'`,
`${node.optional ? '?.' : '.'}includes('${escapeString(text)}')`,
);
},
});
Expand Down
27 changes: 27 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-includes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,33 @@ ruleTester.run('prefer-includes', rule, {
`,
errors: [{ messageId: 'preferStringIncludes' }],
},
// test SequenceExpression
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-actionable / Praise] More and more do I wish JavaScript just didn't have SequenceExpression 🙃 it's so confusing... +1 to you for explicitly adding tests for it.

{
code: `
function f(a: string): void {
/bar/.test((1 + 1, a));
}
`,
output: `
function f(a: string): void {
(1 + 1, a).includes('bar');
}
`,
errors: [{ messageId: 'preferStringIncludes' }],
},
{
code: `
function f(a: string): void {
/\\0'\\\\\\n\\r\\v\\t\\f/.test(a);
}
`,
output: `
function f(a: string): void {
a.includes('\\0\\'\\\\\\n\\r\\v\\t\\f');
}
`,
errors: [{ messageId: 'preferStringIncludes' }],
},
{
code: `
const pattern = new RegExp('bar');
Expand Down