Skip to content

Commit

Permalink
[Tests] no-named-as-default: add tests for import-js#1594
Browse files Browse the repository at this point in the history
  • Loading branch information
akwodkiewicz committed Sep 8, 2024
1 parent a9018a8 commit 7fb7450
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
4 changes: 4 additions & 0 deletions tests/files/no-named-as-default/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const variable = 1;

export { variable };
export default variable;
2 changes: 2 additions & 0 deletions tests/files/no-named-as-default/re-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { something as default } from "./something";
export { something } from "./something";
1 change: 1 addition & 0 deletions tests/files/no-named-as-default/something.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const something = 42;
79 changes: 69 additions & 10 deletions tests/src/rules/no-named-as-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,54 @@ ruleTester.run('no-named-as-default', rule, {
test({ code: 'import bar, { foo } from "./empty-folder";' }),

// es7
test({ code: 'export bar, { foo } from "./bar";',
parser: parsers.BABEL_OLD }),
test({ code: 'export bar from "./bar";',
parser: parsers.BABEL_OLD }),
test({
code: 'export bar, { foo } from "./bar";',
parser: parsers.BABEL_OLD,
}),
test({
code: 'export bar from "./bar";',
parser: parsers.BABEL_OLD,
}),

// #566: don't false-positive on `default` itself
test({ code: 'export default from "./bar";',
parser: parsers.BABEL_OLD }),
test({
code: 'export default from "./bar";',
parser: parsers.BABEL_OLD,
}),

// es2022: Arbitrary module namespae identifier names
testVersion('>= 8.7', () => ({
code: 'import bar, { foo } from "./export-default-string-and-named"',
parserOptions: { ecmaVersion: 2022 },
})),

// #1594: Allow importing as default if object is exported both as default and named
test({ code: 'import something from "./no-named-as-default/re-exports.js";' }),
test({
code: 'import { something } from "./no-named-as-default/re-exports.js";',
}),
test({
code: 'import myOwnNameForVariable from "./no-named-as-default/exports.js";',
}),
test({
code: 'import { variable } from "./no-named-as-default/exports.js";',
}),
// same tests, but for exports
test({
code: 'export something from "./no-named-as-default/re-exports.js";',
parser: parsers.BABEL_OLD,
}),
test({
code: 'export { something } from "./no-named-as-default/re-exports.js";',
}),
test({
code: 'export myOwnNameForVariable from "./no-named-as-default/exports.js";',
parser: parsers.BABEL_OLD,
}),
test({
code: 'export { variable } from "./no-named-as-default/exports.js";',
}),

...SYNTAX_CASES,
),

Expand All @@ -35,26 +68,34 @@ ruleTester.run('no-named-as-default', rule, {
code: 'import foo from "./bar";',
errors: [{
message: 'Using exported name \'foo\' as identifier for default export.',
type: 'ImportDefaultSpecifier' }] }),
type: 'ImportDefaultSpecifier',
}],
}),
test({
code: 'import foo, { foo as bar } from "./bar";',
errors: [{
message: 'Using exported name \'foo\' as identifier for default export.',
type: 'ImportDefaultSpecifier' }] }),
type: 'ImportDefaultSpecifier',
}],
}),

// es7
test({
code: 'export foo from "./bar";',
parser: parsers.BABEL_OLD,
errors: [{
message: 'Using exported name \'foo\' as identifier for default export.',
type: 'ExportDefaultSpecifier' }] }),
type: 'ExportDefaultSpecifier',
}],
}),
test({
code: 'export foo, { foo as bar } from "./bar";',
parser: parsers.BABEL_OLD,
errors: [{
message: 'Using exported name \'foo\' as identifier for default export.',
type: 'ExportDefaultSpecifier' }] }),
type: 'ExportDefaultSpecifier',
}],
}),

test({
code: 'import foo from "./malformed.js"',
Expand All @@ -81,5 +122,23 @@ ruleTester.run('no-named-as-default', rule, {
}],
parserOptions: { ecmaVersion: 2022 },
})),

// #1594: Allow importing as default if object is exported both as default and named
// The only cases that are not covered by the fix
test({
code: 'import variable from "./no-named-as-default/exports.js";',
errors: [{
message: 'Using exported name \'variable\' as identifier for default export.',
type: 'ImportDefaultSpecifier',
}],
}),
test({
code: 'export variable from "./no-named-as-default/exports.js";',
parser: parsers.BABEL_OLD,
errors: [{
message: 'Using exported name \'variable\' as identifier for default export.',
type: 'ExportDefaultSpecifier',
}],
}),
),
});

0 comments on commit 7fb7450

Please sign in to comment.