Skip to content

Commit

Permalink
fix: check if import source is a string
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Sep 20, 2021
1 parent de3be25 commit 25fafe5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/eslint-plugin/src/rules/no-restricted-imports.ts
Expand Up @@ -151,8 +151,11 @@ export default createRule<Options, MessageIds>({

return {
ImportDeclaration(node): void {
if (typeof node.source.value !== 'string') {
return;
}
if (node.importKind === 'type') {
const importSource = (node.source.value as string).trim();
const importSource = node.source.value.trim();
if (
!isAllowedTypeImportPath(importSource) &&
!isAllowedTypeImportPattern(importSource)
Expand All @@ -164,11 +167,14 @@ export default createRule<Options, MessageIds>({
}
},
ExportNamedDeclaration(node): void {
if (node.source?.type !== AST_NODE_TYPES.Literal) {
if (
node.source?.type !== AST_NODE_TYPES.Literal ||
typeof node.source.value !== 'string'
) {
return;
}
if (node.exportKind === 'type') {
const importSource = (node.source.value as string).trim();
const importSource = node.source.value.trim();
if (
!isAllowedTypeImportPath(importSource) &&
!isAllowedTypeImportPattern(importSource)
Expand Down
Expand Up @@ -220,6 +220,10 @@ ruleTester.run('no-restricted-imports', rule, {
code: 'export { foo } from foo;',
options: ['import1', 'import2'],
},
{
code: 'import { foo } from foo;',
options: ['import1', 'import2'],
},
],
invalid: [
{
Expand Down

0 comments on commit 25fafe5

Please sign in to comment.