Skip to content

feat(parser): handle optional chaining in scope analysis #1169

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

Merged
merged 6 commits into from
Nov 11, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,71 @@ type Handler = (event: string) => any
`,
options: ['event'],
},
{
code: `
const a = foo?.bar?.name
`,
},
{
code: `
const a = foo?.bar?.name ?? "foobar"
`,
},
{
code: `
const a = foo()?.bar;
`,
},
{
code: `
const a = foo()?.bar ?? true;
`,
},
],
invalid: [
{
code: `
function onClick() {
console.log(event);
}

fdescribe("foo", function() {
});
`,
options: ['event'],
errors: [
{
message: "Unexpected use of 'event'.",
// the base rule doesn't use messageId
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
],
},
{
code: `
confirm("TEST");
`,
options: ['confirm'],
errors: [
{
message: "Unexpected use of 'confirm'.",
// the base rule doesn't use messageId
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
],
},
{
code: `
var a = confirm("TEST")?.a;
`,
options: ['confirm'],
errors: [
{
message: "Unexpected use of 'confirm'.",
// the base rule doesn't use messageId
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
],
},
],
invalid: [],
});
53 changes: 52 additions & 1 deletion packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,57 @@ function eachr(subject: Object | Array<Value>): typeof subject {
`
function eachr<Key, Value>(subject: Map<Key, Value>): typeof subject;
`,
`
var a = { b: 3 };
var c = a?.b;
`,
`
var a = { b: { c: 3 } };
var d = a?.["b"]?.c;
`,
`
var a = { b: 3 };
var c = { };
var d = (a || c)?.b;
`,
`
var a = { b: () => {} };
a?.b();
`,
],
invalid: [
{
code: 'a = 5;',
errors: [
{
messageId: 'undef',
data: {
name: 'a',
},
},
],
},
{
code: 'a?.b = 5;',
errors: [
{
messageId: 'undef',
data: {
name: 'a',
},
},
],
},
{
code: 'a()?.b = 5;',
errors: [
{
messageId: 'undef',
data: {
name: 'a',
},
},
],
},
],
invalid: [],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import rule from 'eslint/lib/rules/no-use-before-define';
import { RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
ecmaFeatures: {},
},
parser: '@typescript-eslint/parser',
});

ruleTester.run('no-use-before-define', rule, {
valid: [
`
const updatedAt = data?.updatedAt;
`,
`
function f() {
return function t() {};
}
f()?.();
`,
`
var a = { b: 5 };
alert(a?.b);
`,
],
invalid: [
{
code: `
f();
function f() {}
`,
errors: [
{
message: "'f' was used before it was defined.",
// the base rule doesn't use messageId
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
],
},
{
code: `
alert(a);
var a = 10;
`,
errors: [
{
message: "'a' was used before it was defined.",
// the base rule doesn't use messageId
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
],
},
{
code: `
f()?.();
function f() {
return function t() {};
}
`,
errors: [
{
message: "'f' was used before it was defined.",
// the base rule doesn't use messageId
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
],
},
{
code: `
alert(a?.b);
var a = { b: 5 };
`,
errors: [
{
message: "'a' was used before it was defined.",
// the base rule doesn't use messageId
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
],
},
],
});
23 changes: 23 additions & 0 deletions packages/parser/src/analyze-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,29 @@ class Referencer extends TSESLintScope.Referencer<ScopeManager> {
node.arguments.forEach(this.visit, this);
}

/**
* Visit optional member expression.
* @param node The OptionalMemberExpression node to visit.
*/
OptionalMemberExpression(node: TSESTree.OptionalMemberExpression): void {
this.visit(node.object);
if (node.computed) {
this.visit(node.property);
}
}

/**
* Visit optional call expression.
* @param node The OptionalMemberExpression node to visit.
*/
OptionalCallExpression(node: TSESTree.OptionalCallExpression): void {
this.visitTypeParameters(node);

this.visit(node.callee);

node.arguments.forEach(this.visit, this);
}

/**
* Define the variable of this function declaration only once.
* Because to avoid confusion of `no-redeclare` rule by overloading.
Expand Down
Loading