From 71ed5f5dd9771368c2dfb7d6052033af5d37e6b4 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 22 Nov 2021 17:08:54 -0800 Subject: [PATCH] [Fix] `first`: prevent crash when parsing angular templates Fixes #2210 --- .github/workflows/node-4+.yml | 3 +- CHANGELOG.md | 2 + package.json | 1 + src/rules/first.js | 3 + tests/dep-time-travel.sh | 7 +- tests/files/component.html | 139 ++++++++++++++++++++++++++++++++++ tests/src/rules/first.js | 13 +++- 7 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 tests/files/component.html diff --git a/.github/workflows/node-4+.yml b/.github/workflows/node-4+.yml index 427edc6cc0..f100d01d2a 100644 --- a/.github/workflows/node-4+.yml +++ b/.github/workflows/node-4+.yml @@ -18,7 +18,7 @@ jobs: latest: needs: [matrix] - name: 'latest majors' + name: 'majors' runs-on: ubuntu-latest strategy: @@ -96,6 +96,7 @@ jobs: continue-on-error: ${{ matrix.eslint == 4 && matrix.node-version == 4 }} name: 'nvm install ${{ matrix.node-version }} && npm install, with eslint ${{ matrix.eslint }}' env: + NPM_CONFIG_LEGACY_PEER_DEPS: ${{ matrix.eslint < 7 && false || true }} ESLINT_VERSION: ${{ matrix.eslint }} TRAVIS_NODE_VERSION: ${{ matrix.node-version }} with: diff --git a/CHANGELOG.md b/CHANGELOG.md index 3897a3b81b..bac6b00404 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ### Fixed - `importType`: avoid crashing on a non-string' ([#2305], thanks [@ljharb]) +- [`first`]: prevent crash when parsing angular templates ([#2210], thanks [@ljharb]) ### Changed - [`no-default-import`]: report on the token "default" instead of the entire node ([#2299], thanks [@pmcelhaney]) @@ -1227,6 +1228,7 @@ for info on changes for earlier releases. [#164]: https://github.com/import-js/eslint-plugin-import/pull/164 [#157]: https://github.com/import-js/eslint-plugin-import/pull/157 [#2255]: https://github.com/import-js/eslint-plugin-import/issues/2255 +[#2210]: https://github.com/import-js/eslint-plugin-import/issues/2210 [#2201]: https://github.com/import-js/eslint-plugin-import/issues/2201 [#2199]: https://github.com/import-js/eslint-plugin-import/issues/2199 [#2161]: https://github.com/import-js/eslint-plugin-import/issues/2161 diff --git a/package.json b/package.json index 98816bd227..a48f86b225 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ }, "homepage": "https://github.com/import-js/eslint-plugin-import", "devDependencies": { + "@angular-eslint/template-parser": "^13.0.1", "@eslint/import-test-order-redirect-scoped": "file:./tests/files/order-redirect-scoped", "@test-scope/some-module": "file:./tests/files/symlinked-module", "@typescript-eslint/parser": "^2.23.0 || ^3.3.0 || ^4.29.3", diff --git a/src/rules/first.js b/src/rules/first.js index 087b840cb8..285a377f27 100644 --- a/src/rules/first.js +++ b/src/rules/first.js @@ -31,6 +31,9 @@ module.exports = { return { 'Program': function (n) { const body = n.body; + if (!body) { + return; + } const absoluteFirst = context.options[0] === 'absolute-first'; const message = 'Import in body of module; reorder to top.'; const sourceCode = context.getSourceCode(); diff --git a/tests/dep-time-travel.sh b/tests/dep-time-travel.sh index 116a4bfd4f..82681b38f3 100755 --- a/tests/dep-time-travel.sh +++ b/tests/dep-time-travel.sh @@ -6,9 +6,12 @@ echo "installing ${ESLINT_VERSION} in node ${TRAVIS_NODE_VERSION} with TS parser export NPM_CONFIG_LEGACY_PEER_DEPS=true -npm install --no-save "eslint@${ESLINT_VERSION}" --ignore-scripts - +if [[ "$ESLINT_VERSION" -lt "7" ]]; then + echo "Removing @angular-eslint/template-parser..." + npm uninstall --no-save @angular-eslint/template-parser +fi +npm install --no-save "eslint@${ESLINT_VERSION}" --ignore-scripts if [[ -n "$TS_PARSER" ]]; then # if TS parser is manually set, always use it echo "Downgrading @typescript-eslint/parser..." diff --git a/tests/files/component.html b/tests/files/component.html new file mode 100644 index 0000000000..b63f55e0b2 --- /dev/null +++ b/tests/files/component.html @@ -0,0 +1,139 @@ +
+ + + + + + + + +

Welcome to {{ title }}!

+
+
+

Resources & Tools

+

Thank you for using and showing some ♥ for Nx.

+ +

Here are some links to help you get started.

+ +

Next Steps

+

Here are some things you can do with Nx.

+
+ Add UI library +
+  # Generate UI lib
+  nx g @nrwl/angular:lib ui
+
+  # Add a component
+  nx g @nrwl/angular:component xyz --project ui
+
+
+ View dependency graph +
nx dep-graph
+
+
+ Run affected commands +
+  # see what's been affected by changes
+  nx affected:dep-graph
+
+  # run tests for current changes
+  nx affected:test
+
+  # run e2e tests for current changes
+  nx affected:e2e
+  
+
+
+ diff --git a/tests/src/rules/first.js b/tests/src/rules/first.js index 05328e51e9..8892ff3d62 100644 --- a/tests/src/rules/first.js +++ b/tests/src/rules/first.js @@ -1,4 +1,6 @@ -import { test, getTSParsers } from '../utils'; +import { test, getTSParsers, testVersion } from '../utils'; +import fs from 'fs'; +import path from 'path'; import { RuleTester } from 'eslint'; @@ -6,7 +8,7 @@ const ruleTester = new RuleTester(); const rule = require('rules/first'); ruleTester.run('first', rule, { - valid: [ + valid: [].concat( test({ code: "import { x } from './foo'; import { y } from './bar';\ export { x, y }", @@ -21,7 +23,12 @@ ruleTester.run('first', rule, { code: "'use directive';\ import { x } from 'foo';", }), - ], + testVersion('>= 7', () => ({ + // issue #2210 + code: String(fs.readFileSync(path.join(__dirname, '../../files/component.html'))), + parser: require.resolve('@angular-eslint/template-parser'), + })), + ), invalid: [ test({ code: "import { x } from './foo';\