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

feat(scope-manager): add support for JSX scope analysis #2498

Merged
merged 1 commit into from Sep 6, 2020
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
2 changes: 2 additions & 0 deletions .cspell.json
Expand Up @@ -75,6 +75,7 @@
"pluggable",
"postprocess",
"postprocessor",
"preact",
"Premade",
"prettier's",
"recurse",
Expand All @@ -88,6 +89,7 @@
"rulesets",
"serializers",
"superset",
"transpiling",
"thenables",
"transpiles",
"tsconfigs",
Expand Down
161 changes: 161 additions & 0 deletions packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts
Expand Up @@ -140,6 +140,65 @@ function predicate(arg: any): asserts arg is T {
}
}
`,
{
code: `
function Foo() {}
<Foo />;
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
code: `
type T = 1;
function Foo() {}
<Foo<T> />;
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
code: `
const x = 1;
function Foo() {}
<Foo attr={x} />;
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
code: `
const x = {};
function Foo() {}
<Foo {...x} />;
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
code: `
const x = {};
function Foo() {}
<Foo>{x}</Foo>;
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
],
invalid: [
{
Expand Down Expand Up @@ -175,5 +234,107 @@ function predicate(arg: any): asserts arg is T {
},
],
},
{
code: '<Foo />;',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'undef',
data: {
name: 'Foo',
},
line: 1,
column: 2,
},
],
},
{
code: `
function Foo() {}
<Foo attr={x} />;
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'undef',
data: {
name: 'x',
},
line: 3,
column: 12,
},
],
},
{
code: `
function Foo() {}
<Foo {...x} />;
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'undef',
data: {
name: 'x',
},
line: 3,
column: 10,
},
],
},
{
code: `
function Foo() {}
<Foo<T> />;
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'undef',
data: {
name: 'T',
},
line: 3,
column: 6,
},
],
},
{
code: `
function Foo() {}
<Foo>{x}</Foo>;
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'undef',
data: {
name: 'x',
},
line: 3,
column: 7,
},
],
},
],
});
45 changes: 45 additions & 0 deletions packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts
Expand Up @@ -180,6 +180,51 @@ ruleTester.run('consistent-type-imports', rule, {
`,
options: [{ prefer: 'no-type-imports' }],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2455
{
code: `
import React from 'react';

export const ComponentFoo: React.FC = () => {
return <div>Foo Foo</div>;
};
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
code: `
import { h } from 'some-other-jsx-lib';

export const ComponentFoo: h.FC = () => {
return <div>Foo Foo</div>;
};
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
jsxPragma: 'h',
},
},
{
code: `
import { Fragment } from 'react';

export const ComponentFoo: Fragment = () => {
return <>Foo Foo</>;
};
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
jsxFragmentName: 'Fragment',
},
},
],
invalid: [
{
Expand Down
99 changes: 99 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unused-vars.test.ts
Expand Up @@ -803,6 +803,51 @@ export type Test<U> = U extends (arg: {
? I
: never;
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/2455
{
code: `
import React from 'react';

export const ComponentFoo: React.FC = () => {
return <div>Foo Foo</div>;
};
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
code: `
import { h } from 'some-other-jsx-lib';

export const ComponentFoo: h.FC = () => {
return <div>Foo Foo</div>;
};
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
jsxPragma: 'h',
},
},
{
code: `
import { Fragment } from 'react';

export const ComponentFoo: Fragment = () => {
return <>Foo Foo</>;
};
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
jsxFragmentName: 'Fragment',
},
},
],

invalid: [
Expand Down Expand Up @@ -1325,5 +1370,59 @@ type Foo = Array<Foo>;
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2455
{
code: `
import React from 'react';
import { Fragment } from 'react';

export const ComponentFoo = () => {
return <div>Foo Foo</div>;
};
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'unusedVar',
line: 3,
data: {
varName: 'Fragment',
action: 'defined',
additional: '',
},
},
],
},
{
code: `
import React from 'react';
import { h } from 'some-other-jsx-lib';

export const ComponentFoo = () => {
return <div>Foo Foo</div>;
};
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
jsxPragma: 'h',
},
errors: [
{
messageId: 'unusedVar',
line: 2,
data: {
varName: 'React',
action: 'defined',
additional: '',
},
},
],
},
],
});