-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New]
no-namespace
: Make rule fixable
- Add guards to avoid crashing older versions of ESLint - Note that no-namespace's --fix requires ESLint 5+ - Prevent no-namespace --fix tests from running under ESLint < 5
- Loading branch information
Showing
5 changed files
with
228 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,113 @@ | ||
import { RuleTester } from 'eslint' | ||
import eslintPkg from 'eslint/package.json' | ||
import semver from 'semver' | ||
import { test } from '../utils' | ||
|
||
const ERROR_MESSAGE = 'Unexpected namespace import.' | ||
|
||
const ruleTester = new RuleTester() | ||
|
||
// --fix functionality requires ESLint 5+ | ||
const FIX_TESTS = semver.satisfies(eslintPkg.version, '>5.0.0') ? [ | ||
test({ | ||
code: ` | ||
import * as foo from './foo'; | ||
florp(foo.bar); | ||
florp(foo['baz']); | ||
`.trim(), | ||
output: ` | ||
import { bar, baz } from './foo'; | ||
florp(bar); | ||
florp(baz); | ||
`.trim(), | ||
errors: [ { | ||
line: 1, | ||
column: 8, | ||
message: ERROR_MESSAGE, | ||
}], | ||
}), | ||
test({ | ||
code: ` | ||
import * as foo from './foo'; | ||
const bar = 'name conflict'; | ||
const baz = 'name conflict'; | ||
const foo_baz = 'name conflict'; | ||
florp(foo.bar); | ||
florp(foo['baz']); | ||
`.trim(), | ||
output: ` | ||
import { bar as foo_bar, baz as foo_baz_1 } from './foo'; | ||
const bar = 'name conflict'; | ||
const baz = 'name conflict'; | ||
const foo_baz = 'name conflict'; | ||
florp(foo_bar); | ||
florp(foo_baz_1); | ||
`.trim(), | ||
errors: [ { | ||
line: 1, | ||
column: 8, | ||
message: ERROR_MESSAGE, | ||
}], | ||
}), | ||
test({ | ||
code: ` | ||
import * as foo from './foo'; | ||
function func(arg) { | ||
florp(foo.func); | ||
florp(foo['arg']); | ||
} | ||
`.trim(), | ||
output: ` | ||
import { func as foo_func, arg as foo_arg } from './foo'; | ||
function func(arg) { | ||
florp(foo_func); | ||
florp(foo_arg); | ||
} | ||
`.trim(), | ||
errors: [ { | ||
line: 1, | ||
column: 8, | ||
message: ERROR_MESSAGE, | ||
}], | ||
}), | ||
] : [] | ||
|
||
ruleTester.run('no-namespace', require('rules/no-namespace'), { | ||
valid: [ | ||
{ code: "import { a, b } from 'foo';", parserOptions: { ecmaVersion: 2015, sourceType: 'module' } }, | ||
{ code: "import { a, b } from './foo';", parserOptions: { ecmaVersion: 2015, sourceType: 'module' } }, | ||
{ code: "import bar from 'bar';", parserOptions: { ecmaVersion: 2015, sourceType: 'module' } }, | ||
{ code: "import bar from './bar';", parserOptions: { ecmaVersion: 2015, sourceType: 'module' } }, | ||
{ code: 'import { a, b } from \'foo\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } }, | ||
{ code: 'import { a, b } from \'./foo\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } }, | ||
{ code: 'import bar from \'bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } }, | ||
{ code: 'import bar from \'./bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } }, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "import * as foo from 'foo';", | ||
test({ | ||
code: 'import * as foo from \'foo\';', | ||
output: 'import * as foo from \'foo\';', | ||
errors: [ { | ||
line: 1, | ||
column: 8, | ||
message: ERROR_MESSAGE, | ||
} ], | ||
parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, | ||
}, | ||
{ | ||
code: "import defaultExport, * as foo from 'foo';", | ||
}), | ||
test({ | ||
code: 'import defaultExport, * as foo from \'foo\';', | ||
output: 'import defaultExport, * as foo from \'foo\';', | ||
errors: [ { | ||
line: 1, | ||
column: 23, | ||
message: ERROR_MESSAGE, | ||
} ], | ||
parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, | ||
}, | ||
{ | ||
code: "import * as foo from './foo';", | ||
}), | ||
test({ | ||
code: 'import * as foo from \'./foo\';', | ||
output: 'import * as foo from \'./foo\';', | ||
errors: [ { | ||
line: 1, | ||
column: 8, | ||
message: ERROR_MESSAGE, | ||
} ], | ||
parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, | ||
}, | ||
}), | ||
...FIX_TESTS, | ||
], | ||
}) |