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-optional-chain] collect MetaProperty type #6083

Merged
merged 4 commits into from Nov 29, 2022
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
20 changes: 16 additions & 4 deletions packages/eslint-plugin/src/rules/prefer-optional-chain.ts
Expand Up @@ -11,7 +11,8 @@ type ValidChainTarget =
| TSESTree.ChainExpression
| TSESTree.Identifier
| TSESTree.MemberExpression
| TSESTree.ThisExpression;
| TSESTree.ThisExpression
| TSESTree.MetaProperty;

/*
The AST is always constructed such the first element is always the deepest element.
Expand Down Expand Up @@ -115,10 +116,12 @@ export default util.createRule({
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > Identifier',
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > MemberExpression',
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > ChainExpression > MemberExpression',
'LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > MetaProperty',
].join(',')](
initialIdentifierOrNotEqualsExpr:
| TSESTree.Identifier
| TSESTree.MemberExpression,
| TSESTree.MemberExpression
| TSESTree.MetaProperty,
): void {
// selector guarantees this cast
const initialExpression = (
Expand Down Expand Up @@ -190,13 +193,15 @@ export default util.createRule({
'LogicalExpression[operator="&&"] > Identifier',
'LogicalExpression[operator="&&"] > MemberExpression',
'LogicalExpression[operator="&&"] > ChainExpression > MemberExpression',
'LogicalExpression[operator="&&"] > MetaProperty',
'LogicalExpression[operator="&&"] > BinaryExpression[operator="!=="]',
'LogicalExpression[operator="&&"] > BinaryExpression[operator="!="]',
].join(',')](
initialIdentifierOrNotEqualsExpr:
| TSESTree.BinaryExpression
| TSESTree.Identifier
| TSESTree.MemberExpression,
| TSESTree.MemberExpression
| TSESTree.MetaProperty,
): void {
// selector guarantees this cast
const initialExpression = (
Expand Down Expand Up @@ -342,6 +347,10 @@ export default util.createRule({
return node.name;
}

if (node.type === AST_NODE_TYPES.MetaProperty) {
return `${node.meta.name}.${node.property.name}`;
}

if (node.type === AST_NODE_TYPES.ThisExpression) {
return 'this';
}
Expand Down Expand Up @@ -381,6 +390,7 @@ export default util.createRule({
objectText = getMemberExpressionText(node.object);
break;

case AST_NODE_TYPES.MetaProperty:
case AST_NODE_TYPES.ThisExpression:
objectText = getText(node.object);
break;
Expand Down Expand Up @@ -441,6 +451,7 @@ const ALLOWED_MEMBER_OBJECT_TYPES: ReadonlySet<AST_NODE_TYPES> = new Set([
AST_NODE_TYPES.Identifier,
AST_NODE_TYPES.MemberExpression,
AST_NODE_TYPES.ThisExpression,
AST_NODE_TYPES.MetaProperty,
]);
const ALLOWED_COMPUTED_PROP_TYPES: ReadonlySet<AST_NODE_TYPES> = new Set([
AST_NODE_TYPES.Identifier,
Expand Down Expand Up @@ -609,7 +620,8 @@ function isValidChainTarget(
if (
allowIdentifier &&
(node.type === AST_NODE_TYPES.Identifier ||
node.type === AST_NODE_TYPES.ThisExpression)
node.type === AST_NODE_TYPES.ThisExpression ||
node.type === AST_NODE_TYPES.MetaProperty)
) {
return true;
}
Expand Down
79 changes: 79 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts
Expand Up @@ -211,6 +211,12 @@ ruleTester.run('prefer-optional-chain', rule, {
'!foo!.bar || !foo!.bar.baz;',
'!foo!.bar!.baz || !foo!.bar!.baz!.paz;',
'!foo.bar!.baz || !foo.bar!.baz!.paz;',
'import.meta || true;',
'import.meta || import.meta.foo;',
'!import.meta && false;',
'!import.meta && !import.meta.foo;',
'new.target || new.target.length;',
'!new.target || true;',
],
invalid: [
...baseCases,
Expand Down Expand Up @@ -1321,5 +1327,78 @@ foo?.bar(/* comment */a,
},
],
},
{
code: `
class Foo {
constructor() {
new.target && new.target.length;
}
}
`,
output: null,
errors: [
{
messageId: 'preferOptionalChain',
suggestions: [
{
messageId: 'optionalChainSuggest',
output: `
class Foo {
constructor() {
new.target?.length;
}
}
`,
},
],
},
],
},
{
code: noFormat`import.meta && import.meta?.baz;`,
output: null,
errors: [
{
messageId: 'preferOptionalChain',
suggestions: [
{
messageId: 'optionalChainSuggest',
output: noFormat`import.meta?.baz;`,
},
],
},
],
},
{
code: noFormat`!import.meta || !import.meta?.baz;`,
output: null,
errors: [
{
messageId: 'preferOptionalChain',
suggestions: [
{
messageId: 'optionalChainSuggest',
output: noFormat`!import.meta?.baz;`,
},
],
},
],
},

{
code: noFormat`import.meta && import.meta?.() && import.meta?.().baz;`,
output: null,
errors: [
{
messageId: 'preferOptionalChain',
suggestions: [
{
messageId: 'optionalChainSuggest',
output: noFormat`import.meta?.()?.baz;`,
},
],
},
],
},
],
});