Skip to content

Commit e5c330d

Browse files
authored
fix(processor): handle self-closing script tags to prevent null reference error (#601)
1 parent 9c2ab73 commit e5c330d

4 files changed

Lines changed: 56 additions & 0 deletions

File tree

.changeset/quick-radios-search.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-astro": patch
3+
---
4+
5+
Fix a `TypeError` that occurs during the preprocessing phase when `eslint-plugin-astro` encounters a self-closing `<script />` tag.

src/processor/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function preprocess(
6060
node.openingElement.name.type === "JSXIdentifier" &&
6161
node.openingElement.name.name === "script" &&
6262
node.children.length &&
63+
!node.openingElement.selfClosing &&
6364
!node.openingElement.attributes.some(
6465
(attr) =>
6566
attr.type === "JSXAttribute" &&

tests/src/integration/client-javascript.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,25 @@ describe("Integration test for client-side script", () => {
3737
],
3838
)
3939
})
40+
41+
it("should work with self-closing script", async () => {
42+
const eslint = new ESLint({
43+
overrideConfigFile: true,
44+
overrideConfig: [...astroPlugin.configs.base],
45+
})
46+
47+
const result = await eslint.lintText(
48+
`
49+
<script src="something.js" />
50+
`,
51+
{ filePath: "path/to/test.astro" },
52+
)
53+
54+
assert.deepStrictEqual(
55+
result
56+
.flatMap((r) => r.messages)
57+
.map((m) => ({ ruleId: m.ruleId, message: m.message })),
58+
[],
59+
)
60+
})
4061
})

tests/src/integration/client-typescript.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,33 @@ describe("Integration test for client-side ts", () => {
4444
],
4545
)
4646
})
47+
48+
it("should work with self-closing script", async () => {
49+
const eslint = new ESLint({
50+
overrideConfigFile: true,
51+
overrideConfig: [
52+
...astroPlugin.configs.base,
53+
{
54+
files: ["*.ts", "**/*.ts"],
55+
languageOptions: {
56+
parser: tsESLintParser,
57+
},
58+
},
59+
],
60+
})
61+
62+
const result = await eslint.lintText(
63+
`
64+
<script src="something.ts" />
65+
`,
66+
{ filePath: "path/to/test.astro" },
67+
)
68+
69+
assert.deepStrictEqual(
70+
result
71+
.flatMap((r) => r.messages)
72+
.map((m) => ({ ruleId: m.ruleId, message: m.message })),
73+
[],
74+
)
75+
})
4776
})

0 commit comments

Comments
 (0)