Skip to content

Commit 71ed5f5

Browse files
committed
[Fix] first: prevent crash when parsing angular templates
Fixes import-js#2210
1 parent cad19cc commit 71ed5f5

File tree

7 files changed

+162
-6
lines changed

7 files changed

+162
-6
lines changed

.github/workflows/node-4+.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
latest:
2020
needs: [matrix]
21-
name: 'latest majors'
21+
name: 'majors'
2222
runs-on: ubuntu-latest
2323

2424
strategy:
@@ -96,6 +96,7 @@ jobs:
9696
continue-on-error: ${{ matrix.eslint == 4 && matrix.node-version == 4 }}
9797
name: 'nvm install ${{ matrix.node-version }} && npm install, with eslint ${{ matrix.eslint }}'
9898
env:
99+
NPM_CONFIG_LEGACY_PEER_DEPS: ${{ matrix.eslint < 7 && false || true }}
99100
ESLINT_VERSION: ${{ matrix.eslint }}
100101
TRAVIS_NODE_VERSION: ${{ matrix.node-version }}
101102
with:

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
88

99
### Fixed
1010
- `importType`: avoid crashing on a non-string' ([#2305], thanks [@ljharb])
11+
- [`first`]: prevent crash when parsing angular templates ([#2210], thanks [@ljharb])
1112

1213
### Changed
1314
- [`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.
12271228
[#164]: https://github.com/import-js/eslint-plugin-import/pull/164
12281229
[#157]: https://github.com/import-js/eslint-plugin-import/pull/157
12291230
[#2255]: https://github.com/import-js/eslint-plugin-import/issues/2255
1231+
[#2210]: https://github.com/import-js/eslint-plugin-import/issues/2210
12301232
[#2201]: https://github.com/import-js/eslint-plugin-import/issues/2201
12311233
[#2199]: https://github.com/import-js/eslint-plugin-import/issues/2199
12321234
[#2161]: https://github.com/import-js/eslint-plugin-import/issues/2161

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
},
5454
"homepage": "https://github.com/import-js/eslint-plugin-import",
5555
"devDependencies": {
56+
"@angular-eslint/template-parser": "^13.0.1",
5657
"@eslint/import-test-order-redirect-scoped": "file:./tests/files/order-redirect-scoped",
5758
"@test-scope/some-module": "file:./tests/files/symlinked-module",
5859
"@typescript-eslint/parser": "^2.23.0 || ^3.3.0 || ^4.29.3",

src/rules/first.js

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ module.exports = {
3131
return {
3232
'Program': function (n) {
3333
const body = n.body;
34+
if (!body) {
35+
return;
36+
}
3437
const absoluteFirst = context.options[0] === 'absolute-first';
3538
const message = 'Import in body of module; reorder to top.';
3639
const sourceCode = context.getSourceCode();

tests/dep-time-travel.sh

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ echo "installing ${ESLINT_VERSION} in node ${TRAVIS_NODE_VERSION} with TS parser
66

77
export NPM_CONFIG_LEGACY_PEER_DEPS=true
88

9-
npm install --no-save "eslint@${ESLINT_VERSION}" --ignore-scripts
10-
9+
if [[ "$ESLINT_VERSION" -lt "7" ]]; then
10+
echo "Removing @angular-eslint/template-parser..."
11+
npm uninstall --no-save @angular-eslint/template-parser
12+
fi
1113

14+
npm install --no-save "eslint@${ESLINT_VERSION}" --ignore-scripts
1215

1316
if [[ -n "$TS_PARSER" ]]; then # if TS parser is manually set, always use it
1417
echo "Downgrading @typescript-eslint/parser..."

tests/files/component.html

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<header class="flex">
2+
<svg width="40" height="40" viewBox="0 0 262 163">
3+
<polygon
4+
id="Path"
5+
fill="#ffffff"
6+
points="130.68 104.59 97.49 52.71 97.44 96.3 40.24 0 0 0 0 162.57 39.79 162.57 39.92 66.39 96.53 158.26"
7+
></polygon>
8+
<polygon
9+
id="Path"
10+
fill="#ffffff"
11+
points="97.5 41.79 137.24 41.79 137.33 41.33 137.33 0 97.54 0 97.49 41.33"
12+
></polygon>
13+
<path
14+
d="M198.66,86.86 C189.139872,86.6795216 180.538723,92.516445 177.19,101.43 C182.764789,93.0931021 193.379673,89.7432211 202.73,93.37 C207.05,95.13 212.73,97.97 217.23,96.45 C212.950306,90.4438814 206.034895,86.8725952 198.66,86.86 L198.66,86.86 Z"
15+
id="Path"
16+
fill="#96D8E9"
17+
></path>
18+
<path
19+
d="M243.75,106.42 C243.75,101.55 241.1,100.42 235.6,98.42 C231.52,97 226.89,95.4 223.52,91 C222.86,90.13 222.25,89.15 221.6,88.11 C220.14382,85.4164099 218.169266,83.037429 215.79,81.11 C212.58,78.75 208.37,77.6 202.91,77.6 C191.954261,77.6076705 182.084192,84.2206169 177.91,94.35 C183.186964,87.0278244 191.956716,83.0605026 200.940147,83.9314609 C209.923578,84.8024193 217.767888,90.3805017 221.54,98.58 C223.424615,101.689762 227.141337,103.174819 230.65,102.22 C236.02,101.07 235.65,106.15 243.76,107.87 L243.75,106.42 Z"
20+
id="Path"
21+
fill="#48C4E5"
22+
></path>
23+
<path
24+
d="M261.46,105.38 L261.46,105.27 C261.34,73.03 235.17,45.45 202.91,45.45 C183.207085,45.4363165 164.821777,55.3450614 154,71.81 L153.79,71.45 L137.23,45.45 L97.5,45.4499858 L135.25,104.57 L98.41,162.57 L137,162.57 L153.79,136.78 L170.88,162.57 L209.48,162.57 L174.48,107.49 C173.899005,106.416838 173.583536,105.220114 173.56,104 C173.557346,96.2203871 176.64661,88.7586448 182.147627,83.2576275 C187.648645,77.7566101 195.110387,74.6673462 202.89,74.67 C219.11,74.67 221.82,84.37 225.32,88.93 C232.23,97.93 246.03,93.99 246.03,105.73 L246.03,105.73 C246.071086,108.480945 247.576662,111.001004 249.979593,112.340896 C252.382524,113.680787 255.317747,113.636949 257.679593,112.225896 C260.041438,110.814842 261.471086,108.250945 261.43,105.5 L261.43,105.5 L261.43,105.38 L261.46,105.38 Z"
25+
id="Path"
26+
fill="#ffffff"
27+
></path>
28+
<path
29+
d="M261.5,113.68 C261.892278,116.421801 261.504116,119.218653 260.38,121.75 C258.18,126.84 254.51,125.14 254.51,125.14 C254.51,125.14 251.35,123.6 253.27,120.65 C255.4,117.36 259.61,117.74 261.5,113.68 Z"
30+
id="Path"
31+
fill="#022f56"
32+
></path>
33+
</svg>
34+
<h1>Welcome to {{ title }}!</h1>
35+
</header>
36+
<main>
37+
<h2>Resources &amp; Tools</h2>
38+
<p>Thank you for using and showing some ♥ for Nx.</p>
39+
<div class="flex github-star-container">
40+
<a
41+
href="https://github.com/nrwl/nx"
42+
target="_blank"
43+
rel="noopener noreferrer"
44+
>
45+
If you like Nx, please give it a star:
46+
<div class="github-star-badge">
47+
<svg
48+
class="material-icons"
49+
xmlns="http://www.w3.org/2000/svg"
50+
width="24"
51+
height="24"
52+
viewBox="0 0 24 24"
53+
>
54+
<path d="M0 0h24v24H0z" fill="none" />
55+
<path
56+
d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"
57+
/>
58+
</svg>
59+
Star
60+
</div>
61+
</a>
62+
</div>
63+
<p>Here are some links to help you get started.</p>
64+
<ul class="resources">
65+
<li class="col-span-2">
66+
<a class="resource flex" href="https://nxplaybook.com/p/nx-workspaces">
67+
Nx video course
68+
</a>
69+
</li>
70+
<li class="col-span-2">
71+
<a
72+
class="resource flex"
73+
href="https://nx.dev/latest/angular/getting-started/intro"
74+
>
75+
Nx video tutorial
76+
</a>
77+
</li>
78+
<li class="col-span-2">
79+
<a
80+
class="resource flex"
81+
href="https://nx.dev/latest/angular/tutorial/01-create-application"
82+
>
83+
Interactive tutorial
84+
</a>
85+
</li>
86+
<li class="col-span-2">
87+
<a class="resource flex" href="https://nx.app/">
88+
<svg
89+
width="36"
90+
height="36"
91+
viewBox="0 0 120 120"
92+
fill="none"
93+
xmlns="http://www.w3.org/2000/svg"
94+
>
95+
<path
96+
d="M120 15V30C103.44 30 90 43.44 90 60C90 76.56 76.56 90 60 90C43.44 90 30 103.44 30 120H15C6.72 120 0 113.28 0 105V15C0 6.72 6.72 0 15 0H105C113.28 0 120 6.72 120 15Z"
97+
fill="#0E2039"
98+
/>
99+
<path
100+
d="M120 30V105C120 113.28 113.28 120 105 120H30C30 103.44 43.44 90 60 90C76.56 90 90 76.56 90 60C90 43.44 103.44 30 120 30Z"
101+
fill="white"
102+
/>
103+
</svg>
104+
<span class="gutter-left">Nx Cloud</span>
105+
</a>
106+
</li>
107+
</ul>
108+
<h2>Next Steps</h2>
109+
<p>Here are some things you can do with Nx.</p>
110+
<details open>
111+
<summary>Add UI library</summary>
112+
<pre>
113+
# Generate UI lib
114+
nx g @nrwl/angular:lib ui
115+
116+
# Add a component
117+
nx g @nrwl/angular:component xyz --project ui</pre
118+
>
119+
</details>
120+
<details>
121+
<summary>View dependency graph</summary>
122+
<pre>nx dep-graph</pre>
123+
</details>
124+
<details>
125+
<summary>Run affected commands</summary>
126+
<pre>
127+
# see what's been affected by changes
128+
nx affected:dep-graph
129+
130+
# run tests for current changes
131+
nx affected:test
132+
133+
# run e2e tests for current changes
134+
nx affected:e2e
135+
</pre
136+
>
137+
</details>
138+
</main>
139+

tests/src/rules/first.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { test, getTSParsers } from '../utils';
1+
import { test, getTSParsers, testVersion } from '../utils';
2+
import fs from 'fs';
3+
import path from 'path';
24

35
import { RuleTester } from 'eslint';
46

57
const ruleTester = new RuleTester();
68
const rule = require('rules/first');
79

810
ruleTester.run('first', rule, {
9-
valid: [
11+
valid: [].concat(
1012
test({
1113
code: "import { x } from './foo'; import { y } from './bar';\
1214
export { x, y }",
@@ -21,7 +23,12 @@ ruleTester.run('first', rule, {
2123
code: "'use directive';\
2224
import { x } from 'foo';",
2325
}),
24-
],
26+
testVersion('>= 7', () => ({
27+
// issue #2210
28+
code: String(fs.readFileSync(path.join(__dirname, '../../files/component.html'))),
29+
parser: require.resolve('@angular-eslint/template-parser'),
30+
})),
31+
),
2532
invalid: [
2633
test({
2734
code: "import { x } from './foo';\

0 commit comments

Comments
 (0)