Skip to content

Commit

Permalink
fix: support default
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 12, 2023
1 parent 98765f5 commit b2864a9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/rules/noRestrictedImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ export default createRule<Options, keyof typeof messages>({
}

for (const importName of importNames) {
if (importName === 'default') {
for (const nodeSpecifier of node.specifiers) {
if (
nodeSpecifier.type === AST_NODE_TYPES.ImportDefaultSpecifier
) {
context.report({
data: {
customMessage: importRule.message,
importName: 'default',
importSource,
},
messageId: 'importName',
node: nodeSpecifier,
});
}
}
}

if (
importName === '*' &&
node.specifiers[0].type === AST_NODE_TYPES.ImportNamespaceSpecifier
Expand Down
46 changes: 46 additions & 0 deletions tests/rules/noRestrictedImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,52 @@ ruleTester.run('no-restricted-imports', rule, {
},
],
},
{
code: `import { default as bar } from 'bar'`,
errors: [
{
data: {
customMessage: 'foo is restricted',
importName: 'default',
importSource: 'bar',
},
messageId: 'importName',
},
],
options: [
{
paths: [
{
importNames: ['default'],
message: 'foo is restricted',
name: 'bar',
},
],
},
],
},
{
code: `import bar from 'bar'`,
errors: [
{
data: {
customMessage: 'foo is restricted',
importSource: 'bar',
},
messageId: 'path',
},
],
options: [
{
paths: [
{
message: 'foo is restricted',
name: 'bar',
},
],
},
],
},
],
valid: [
{
Expand Down

0 comments on commit b2864a9

Please sign in to comment.