Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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 | ||
}, | ||
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; | ||
@@ -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: [ | ||
{ | ||
code: 'var a = 3; var a = 10;', | ||
parserOptions: { ecmaVersion: 6 }, | ||
errors: [ | ||
{ | ||
message: "'a' is already defined.", | ||
type: AST_NODE_TYPES.Identifier | ||
} as any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Uh oh!
There was an error while loading.Please reload this page.