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: add ignoreTypes option to define-flow-type rules #509

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion src/rules/defineFlowType.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
const schema = [];
const schema = [
{
properties: {
ignoreTypes: {
items: {
type: 'string',
},
type: 'array',
},
},
type: 'object',
},
];

const create = (context) => {
let globalScope;

const [firstOption] = context.options;
const ignoreTypes = firstOption ? firstOption.ignoreTypes : [];

// do nearly the same thing that eslint does for config globals
// https://github.com/eslint/eslint/blob/v2.0.0/lib/eslint.js#L118-L194
const makeDefined = (ident) => {
Expand All @@ -12,6 +27,10 @@ const create = (context) => {
for (ii = globalScope.through.length - 1; ii >= 0; ii--) {
const ref = globalScope.through[ii];

if (ignoreTypes.includes(ident.name)) {
return;
}

if (ref.identifier.name === ident.name) {
// use "__defineGeneric" since we don't have a reference to "escope.Variable"

Expand Down
47 changes: 47 additions & 0 deletions tests/rules/assertions/defineFlowType.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
RuleTester,
} from 'eslint';
import noUndefRule from 'eslint/lib/rules/no-undef';
import defineFlowType from '../../../src/rules/defineFlowType';

const VALID_WITH_DEFINE_FLOW_TYPE = [
{
Expand Down Expand Up @@ -146,6 +147,26 @@ type Foo = $ReadOnly<{}>`,
},
},
},

/**
* There is unobvious behavior.
* We used Foo type and it became global variable.
* noUndefRule will not pay attention to Foo variable with defineFlowType.
* It can be fixed with ignoreTypes option
*/
{
code: `
type A = {foo: Foo};

Foo()
`,

// There are two errors one for type another for global var
errors: [
'\'Foo\' is not defined.',
'\'Foo\' is not defined.',
],
},
];

const ALWAYS_INVALID = [
Expand Down Expand Up @@ -221,6 +242,32 @@ const ALWAYS_VALID = [
});
}

{
const ruleTester = new RuleTester({
parser: require.resolve('babel-eslint'),
rules: {
'define-flow-type': [1, {ignoreTypes: ['Foo']}],
},
});

ruleTester.defineRule('define-flow-type', defineFlowType);
ruleTester.run('no-undef must trigger an error when define-flow-type is used with ignoreTypes option', noUndefRule, {
invalid: [
{
code: `
type A = {foo: Foo};
Foo();
`,
errors: [
'\'Foo\' is not defined.',
'\'Foo\' is not defined.',
],
},
],
valid: [],
});
}

export default {
invalid: [],
valid: [
Expand Down