diff --git a/CHANGELOG.md b/CHANGELOG.md index 10c77d4e6..7ff3b8bce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ### Fixed - [`export`]/TypeScript: properly detect export specifiers as children of a TS module block ([#1889], thanks [@andreubotella]) - [`order`]: ignore non-module-level requires ([#1940], thanks [@golopot]) +- [`no-webpack-loader-syntax`]/TypeScript: avoid crash on missing name ([#1947], thanks @leonardodino) ## [2.22.1] - 2020-09-27 ### Fixed @@ -736,6 +737,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#1947]: https://github.com/benmosher/eslint-plugin-import/pull/1947 [#1940]: https://github.com/benmosher/eslint-plugin-import/pull/1940 [#1889]: https://github.com/benmosher/eslint-plugin-import/pull/1889 [#1878]: https://github.com/benmosher/eslint-plugin-import/pull/1878 diff --git a/src/rules/no-webpack-loader-syntax.js b/src/rules/no-webpack-loader-syntax.js index 8075a6f9e..a80bc112d 100644 --- a/src/rules/no-webpack-loader-syntax.js +++ b/src/rules/no-webpack-loader-syntax.js @@ -2,7 +2,7 @@ import isStaticRequire from '../core/staticRequire' import docsUrl from '../docsUrl' function reportIfNonStandard(context, node, name) { - if (name.indexOf('!') !== -1) { + if (name && name.indexOf('!') !== -1) { context.report(node, `Unexpected '!' in '${name}'. ` + 'Do not use import syntax to configure webpack loaders.' ) diff --git a/tests/src/rules/no-webpack-loader-syntax.js b/tests/src/rules/no-webpack-loader-syntax.js index 23a1190fb..a56e142e7 100644 --- a/tests/src/rules/no-webpack-loader-syntax.js +++ b/tests/src/rules/no-webpack-loader-syntax.js @@ -1,4 +1,4 @@ -import { test } from '../utils' +import { test, getTSParsers } from '../utils' import { RuleTester } from 'eslint' @@ -72,3 +72,26 @@ ruleTester.run('no-webpack-loader-syntax', rule, { }), ], }) + +context('TypeScript', function () { + getTSParsers().forEach((parser) => { + const parserConfig = { + parser: parser, + settings: { + 'import/parsers': { [parser]: ['.ts'] }, + 'import/resolver': { 'eslint-import-resolver-typescript': true }, + }, + } + ruleTester.run('no-webpack-loader-syntax', rule, { + valid: [ + test(Object.assign({ + code: 'import { foo } from\nalert()', + }, parserConfig)), + test(Object.assign({ + code: 'import foo from\nalert()', + }, parserConfig)), + ], + invalid: [], + }) + }) +})