Skip to content

Commit

Permalink
fix: crash for set:html with tseslint (#283)
Browse files Browse the repository at this point in the history
* fix: crash for `set:html` with tseslint

* Create new-fans-tease.md
  • Loading branch information
ota-meshi authored Jan 19, 2024
1 parent 38de962 commit 33b0a3f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-fans-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-eslint-parser": patch
---

fix: crash for `set:html` with tseslint
68 changes: 28 additions & 40 deletions src/parser/process-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,54 +503,42 @@ export function processTemplate(
) {
const baseNameNode = scriptNode.name;
if (colonOffset != null) {
const nameNode: TSESTree.JSXNamespacedName = {
...baseNameNode,
type: AST_NODE_TYPES.JSXNamespacedName,
namespace: {
type: AST_NODE_TYPES.JSXIdentifier,
name: attr.name.slice(0, colonOffset),
...ctx.getLocations(
baseNameNode.range[0],
baseNameNode.range[0] + colonOffset,
),
},
name: {
type: AST_NODE_TYPES.JSXIdentifier,
name: attr.name.slice(colonOffset + 1),
...ctx.getLocations(
baseNameNode.range[0] + colonOffset + 1,
baseNameNode.range[1],
),
},
const nameNode = baseNameNode as TSESTree.JSXNamespacedName;
nameNode.type = AST_NODE_TYPES.JSXNamespacedName;
nameNode.namespace = {
type: AST_NODE_TYPES.JSXIdentifier,
name: attr.name.slice(0, colonOffset),
...ctx.getLocations(
baseNameNode.range[0],
baseNameNode.range[0] + colonOffset,
),
};
nameNode.name = {
type: AST_NODE_TYPES.JSXIdentifier,
name: attr.name.slice(colonOffset + 1),
...ctx.getLocations(
baseNameNode.range[0] + colonOffset + 1,
baseNameNode.range[1],
),
};
scriptNode.name = nameNode;
nameNode.namespace.parent = nameNode;
nameNode.name.parent = nameNode;
} else {
if (baseNameNode.type === AST_NODE_TYPES.JSXIdentifier) {
const nameNode: TSESTree.JSXIdentifier = {
...baseNameNode,
name: attr.name,
};
const nameNode = baseNameNode;
nameNode.name = attr.name;
scriptNode.name = nameNode;
} else {
const nameNode: TSESTree.JSXNamespacedName = {
...baseNameNode,
namespace: {
...baseNameNode.namespace,
name: attr.name.slice(
baseNameNode.namespace.range[0] - start,
baseNameNode.namespace.range[1] - start,
),
},
name: {
...baseNameNode.name,
name: attr.name.slice(
baseNameNode.name.range[0] - start,
baseNameNode.name.range[1] - start,
),
},
};
const nameNode = baseNameNode;
nameNode.namespace.name = attr.name.slice(
baseNameNode.namespace.range[0] - start,
baseNameNode.namespace.range[1] - start,
);
nameNode.name.name = attr.name.slice(
baseNameNode.name.range[0] - start,
baseNameNode.name.range[1] - start,
);
scriptNode.name = nameNode;
nameNode.namespace.parent = nameNode;
nameNode.name.parent = nameNode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
---

<div>
<Fragment set:html={'content'} />
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint eslint-comments/require-description: 0, @typescript-eslint/explicit-module-boundary-types: 0 */
import type { Linter } from "eslint";
import { getBasicParserOptions } from "../../../src/parser/test-utils";
import { rules } from "@typescript-eslint/eslint-plugin";
export function setupLinter(linter: Linter) {
linter.defineRule(
"@typescript-eslint/no-unsafe-assignment",
rules["no-unsafe-assignment"] as never,
);
}

export function getConfig() {
return {
parser: "astro-eslint-parser",
parserOptions: getBasicParserOptions(),
rules: {
"@typescript-eslint/no-unsafe-assignment": "error",
},
env: {
browser: true,
es2021: true,
},
};
}

0 comments on commit 33b0a3f

Please sign in to comment.