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-regexp-exec] factor in union types #3434

Merged
Show file tree
Hide file tree
Changes from 6 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
122 changes: 77 additions & 45 deletions packages/eslint-plugin/src/rules/prefer-regexp-exec.ts
Expand Up @@ -2,6 +2,8 @@ import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import {
createRule,
getParserServices,
Expand All @@ -10,6 +12,13 @@ import {
getWrappingFixer,
} from '../util';

enum ArgumentType {
Both,
Other,
RegExp,
String,
}

export default createRule({
name: 'prefer-regexp-exec',
defaultOptions: [],
Expand Down Expand Up @@ -37,25 +46,41 @@ export default createRule({
const sourceCode = context.getSourceCode();

/**
* Check if a given node is a string.
* @param node The node to check.
* Check if a given node type is a string.
* @param node The node type to check.
*/
function isStringType(node: TSESTree.Node): boolean {
const objectType = typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(node),
);
return getTypeName(typeChecker, objectType) === 'string';
function isStringType(type: ts.Type): boolean {
return getTypeName(typeChecker, type) === 'string';
}

/**
* Check if a given node is a RegExp.
* @param node The node to check.
* Check if a given node type is a RegExp.
* @param node The node type to check.
*/
function isRegExpType(node: TSESTree.Node): boolean {
const objectType = typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(node),
);
return getTypeName(typeChecker, objectType) === 'RegExp';
function isRegExpType(type: ts.Type): boolean {
return getTypeName(typeChecker, type) === 'RegExp';
}

function collectArgumentTypes(types: ts.Type[]): ArgumentType {
let result = ArgumentType.Other;

for (const type of types) {
if (isRegExpType(type)) {
if (result === ArgumentType.Other) {
result = ArgumentType.RegExp;
} else if (result === ArgumentType.String) {
result = ArgumentType.Both;
}
} else if (isStringType(type)) {
if (result === ArgumentType.Other) {
result = ArgumentType.String;
} else if (result === ArgumentType.RegExp) {
result = ArgumentType.Both;
}
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
}
}

return result;
}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

return {
Expand All @@ -67,7 +92,13 @@ export default createRule({
const argumentNode = callNode.arguments[0];
const argumentValue = getStaticValue(argumentNode, globalScope);

if (!isStringType(objectNode)) {
if (
!isStringType(
typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(objectNode),
),
)
) {
return;
}

Expand Down Expand Up @@ -97,38 +128,39 @@ export default createRule({
});
}

if (isRegExpType(argumentNode)) {
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
fix: getWrappingFixer({
sourceCode,
node: callNode,
innerNode: [objectNode, argumentNode],
wrap: (objectCode, argumentCode) =>
`${argumentCode}.exec(${objectCode})`,
}),
});
}
const argumentType = typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(argumentNode),
);
const argumentTypes = collectArgumentTypes(
tsutils.unionTypeParts(argumentType),
);
switch (argumentTypes) {
case ArgumentType.RegExp:
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
fix: getWrappingFixer({
sourceCode,
node: callNode,
innerNode: [objectNode, argumentNode],
wrap: (objectCode, argumentCode) =>
`${argumentCode}.exec(${objectCode})`,
}),
});

if (isStringType(argumentNode)) {
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
fix: getWrappingFixer({
sourceCode,
node: callNode,
innerNode: [objectNode, argumentNode],
wrap: (objectCode, argumentCode) =>
`RegExp(${argumentCode}).exec(${objectCode})`,
}),
});
case ArgumentType.String:
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
fix: getWrappingFixer({
sourceCode,
node: callNode,
innerNode: [objectNode, argumentNode],
wrap: (objectCode, argumentCode) =>
`RegExp(${argumentCode}).exec(${objectCode})`,
}),
});
}

return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
});
},
};
},
Expand Down
93 changes: 23 additions & 70 deletions packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts
Expand Up @@ -33,6 +33,29 @@ function f(s: string | string[]) {
s.match(/e/);
}
`,
"(Math.random() > 0.5 ? 'abc' : 123).match(2);",
"'212'.match(2);",
"'212'.match(+2);",
"'oNaNo'.match(NaN);",
"'Infinity contains -Infinity and +Infinity in JavaScript.'.match(Infinity);",
"'Infinity contains -Infinity and +Infinity in JavaScript.'.match(+Infinity);",
"'Infinity contains -Infinity and +Infinity in JavaScript.'.match(-Infinity);",
"'void and null'.match(null);",
`
const matchers = ['package-lock.json', /regexp/];
const file = '';
matchers.some(matcher => !!file.match(matcher));
`,
`
const matchers = [/regexp/, 'package-lock.json'];
const file = '';
matchers.some(matcher => !!file.match(matcher));
`,
`
const matchers = [{ match: (s: RegExp) => false }];
const file = '';
matchers.some(matcher => !!file.match(matcher));
`,
],
invalid: [
{
Expand Down Expand Up @@ -95,76 +118,6 @@ const search = 'thing';
RegExp(search).exec(text);
`,
},
{
code: "'212'.match(2);",
errors: [
{
messageId: 'regExpExecOverStringMatch',
line: 1,
column: 7,
},
],
},
{
code: "'212'.match(+2);",
errors: [
{
messageId: 'regExpExecOverStringMatch',
line: 1,
column: 7,
},
],
},
{
code: "'oNaNo'.match(NaN);",
errors: [
{
messageId: 'regExpExecOverStringMatch',
line: 1,
column: 9,
},
],
},
{
code: "'Infinity contains -Infinity and +Infinity in JavaScript.'.match(Infinity);",
errors: [
{
messageId: 'regExpExecOverStringMatch',
line: 1,
column: 60,
},
],
},
{
code: "'Infinity contains -Infinity and +Infinity in JavaScript.'.match(+Infinity);",
errors: [
{
messageId: 'regExpExecOverStringMatch',
line: 1,
column: 60,
},
],
},
{
code: "'Infinity contains -Infinity and +Infinity in JavaScript.'.match(-Infinity);",
errors: [
{
messageId: 'regExpExecOverStringMatch',
line: 1,
column: 60,
},
],
},
{
code: "'void and null'.match(null);",
errors: [
{
messageId: 'regExpExecOverStringMatch',
line: 1,
column: 17,
},
],
},
{
code: `
function f(s: 'a' | 'b') {
Expand Down