From 33b0a3f4a6c59e934e6716afb638f08c748c52f4 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Fri, 19 Jan 2024 09:16:08 +0900 Subject: [PATCH] fix: crash for `set:html` with tseslint (#283) * fix: crash for `set:html` with tseslint * Create new-fans-tease.md --- .changeset/new-fans-tease.md | 5 ++ src/parser/process-template.ts | 68 ++++++++----------- .../ts-no-unsafe-assignment-input.astro | 7 ++ .../ts-no-unsafe-assignment-output.json | 1 + .../ts-no-unsafe-assignment-setup.ts | 24 +++++++ 5 files changed, 65 insertions(+), 40 deletions(-) create mode 100644 .changeset/new-fans-tease.md create mode 100644 tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-input.astro create mode 100644 tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-output.json create mode 100644 tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-setup.ts diff --git a/.changeset/new-fans-tease.md b/.changeset/new-fans-tease.md new file mode 100644 index 0000000..4498d90 --- /dev/null +++ b/.changeset/new-fans-tease.md @@ -0,0 +1,5 @@ +--- +"astro-eslint-parser": patch +--- + +fix: crash for `set:html` with tseslint diff --git a/src/parser/process-template.ts b/src/parser/process-template.ts index fc1afbc..ab642cb 100644 --- a/src/parser/process-template.ts +++ b/src/parser/process-template.ts @@ -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; diff --git a/tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-input.astro b/tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-input.astro new file mode 100644 index 0000000..627a159 --- /dev/null +++ b/tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-input.astro @@ -0,0 +1,7 @@ +--- + +--- + +
+ +
diff --git a/tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-output.json b/tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-output.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-output.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-setup.ts b/tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-setup.ts new file mode 100644 index 0000000..2917b2f --- /dev/null +++ b/tests/fixtures/integrations/ts-no-unsafe-assignment/ts-no-unsafe-assignment-setup.ts @@ -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, + }, + }; +}