Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 3 commits intotypescript-eslint:mainfromMax10240:main
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletionspackages/eslint-plugin/src/rules/prefer-includes.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionspackages/eslint-plugin/tests/rules/prefer-includes.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -234,6 +234,33 @@ ruleTester.run('prefer-includes', rule, {
`,
errors: [{ messageId: 'preferStringIncludes' }],
},
// test SequenceExpression
Copy link
Member

@JoshuaKGoldbergJoshuaKGoldbergJul 8, 2023
edited
Loading

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.

Max10240 reacted with laugh emoji
{
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

[8]ページ先頭

©2009-2025 Movatter.jp