Skip to content

test(eslint-plugin): add test cases for eslint rules #240

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 4 commits into from
Feb 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
3 changes: 3 additions & 0 deletions packages/eslint-plugin/tests/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ interface ValidTestCase<TOptions extends Readonly<any[]>> {
settings?: Record<string, any>;
parser?: string;
globals?: Record<string, boolean>;
env?: {
browser?: boolean;
};
}

interface InvalidTestCase<
Expand Down
250 changes: 246 additions & 4 deletions packages/eslint-plugin/tests/eslint-rules/no-redeclare.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
import rule from 'eslint/lib/rules/no-redeclare';
import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
import { RuleTester } from '../RuleTester';

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

ruleTester.run('no-redeclare', rule, {
valid: [
'var a = 3; var b = function() { var a = 10; };',
'var a = 3; a = 10;',
{
code: 'if (true) {\n let b = 2;\n} else { \nlet b = 3;\n}',
parserOptions: {
ecmaVersion: 6
}
},
'var Object = 0;',
{ code: 'var Object = 0;', options: [{ builtinGlobals: false }] },
{
code: 'var Object = 0;',
options: [{ builtinGlobals: true }],
parserOptions: { sourceType: 'module' }
},
{
code: 'var Object = 0;',
options: [{ builtinGlobals: true }],
parserOptions: { ecmaFeatures: { globalReturn: true } }
},
{ code: 'var top = 0;', env: { browser: true } },
{ code: 'var top = 0;', options: [{ builtinGlobals: true }] },
{
code: 'var top = 0;',
options: [{ builtinGlobals: true }],
parserOptions: { ecmaFeatures: { globalReturn: true } },
env: { browser: true }
},
{
code: 'var top = 0;',
options: [{ builtinGlobals: true }],
parserOptions: { sourceType: 'module' },
env: { browser: true }
},
{
code: 'var self = 1',
options: [{ builtinGlobals: true }],
env: { browser: false }
},
// https://github.com/eslint/typescript-eslint-parser/issues/443
`
const Foo = 1;
Expand All @@ -22,7 +60,211 @@ type Foo = 1;
function foo({ bar }: { bar: string }) {
console.log(bar);
}
`,
`
type AST<T extends ParserOptions> = TSESTree.Program &
(T['range'] extends true ? { range: [number, number] } : {}) &
(T['tokens'] extends true ? { tokens: TSESTree.Token[] } : {}) &
(T['comment'] extends true ? { comments: TSESTree.Comment[] } : {});
interface ParseAndGenerateServicesResult<T extends ParserOptions> {
ast: AST<T>;
services: ParserServices;
}
`,
`
function A<T>() {}
interface B<T> {}
type C<T> = Array<T>
class D<T> {}
`
],
invalid: []
invalid: [
{
code: 'var a = 3; var a = 10;',
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh, we should consider PRing the base eslint to migrate their rules to messageId

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup we should :)

]
},
{
code: 'switch(foo) { case a: var b = 3;\ncase b: var b = 4}',
errors: [
{
message: "'b' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a = 3; var a = 10;',
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a = {}; var a = [];',
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a; function a() {}',
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'function a() {} function a() {}',
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a = function() { }; var a = function() { }',
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a = function() { }; var a = new Date();',
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a = 3; var a = 10; var a = 15;',
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any,
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a; var a;',
parserOptions: { sourceType: 'module' },
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'export var a; var a;',
parserOptions: { sourceType: 'module' },
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var Object = 0;',
options: [{ builtinGlobals: true }],
errors: [
{
message: "'Object' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var top = 0;',
options: [{ builtinGlobals: true }],
errors: [
{
message: "'top' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
],
env: { browser: true }
},
{
code: 'var a; var {a = 0, b: Object = 0} = {};',
options: [{ builtinGlobals: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any,
{
message: "'Object' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a; var {a = 0, b: Object = 0} = {};',
options: [{ builtinGlobals: true }],
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a; var {a = 0, b: Object = 0} = {};',
options: [{ builtinGlobals: true }],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { globalReturn: true } },
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},
{
code: 'var a; var {a = 0, b: Object = 0} = {};',
options: [{ builtinGlobals: false }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "'a' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
},

// Notifications of readonly are moved from no-undef: https://github.com/eslint/eslint/issues/4504
{
code: '/*global b:false*/ var b = 1;',
options: [{ builtinGlobals: true }],
errors: [
{
message: "'b' is already defined.",
type: AST_NODE_TYPES.Identifier
} as any
]
}
]
});

This file was deleted.

19 changes: 17 additions & 2 deletions packages/eslint-plugin/tests/rules/no-use-before-define.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,22 @@ export namespace Third {
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
parser: '@typescript-eslint/parser'
}
},
// https://github.com/eslint/typescript-eslint-parser/issues/550
`
function test(file: Blob) {
const slice: typeof file.slice =
file.slice || (file as any).webkitSlice || (file as any).mozSlice
return slice
}
`,
// https://github.com/eslint/typescript-eslint-parser/issues/435
`
interface Foo {
bar: string
}
const bar = 'blah'
`
],
invalid: [
{
Expand Down Expand Up @@ -813,7 +828,7 @@ function foo() {
var bar = 1;
}
var bar;
`,
`,
parserOptions,
options: [{ variables: false }],
errors: [
Expand Down