From 3f8b162ad1eea20e8b6b32c0cd761899e7e607f8 Mon Sep 17 00:00:00 2001 From: xeho91 Date: Tue, 18 Jun 2024 19:49:19 +0800 Subject: [PATCH] Resolve `FIXME`s related to `estree` --- src/compiler/transform/define-meta/index.ts | 12 ++++-------- src/compiler/transform/remove-export-default.ts | 8 +++----- src/compiler/transform/story/index.ts | 3 +-- src/estree.d.ts | 15 +++++++++++++++ src/parser/extract/compiled/nodes.ts | 5 +++-- 5 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 src/estree.d.ts diff --git a/src/compiler/transform/define-meta/index.ts b/src/compiler/transform/define-meta/index.ts index 552e68b..0891a1f 100644 --- a/src/compiler/transform/define-meta/index.ts +++ b/src/compiler/transform/define-meta/index.ts @@ -1,3 +1,4 @@ +import type { Program } from 'estree'; import { toJs } from 'estree-util-to-js'; import type MagicString from 'magic-string'; @@ -34,16 +35,11 @@ export function transformDefineMeta(params: Params): void { const { compiled } = nodes; const { defineMetaVariableDeclaration } = compiled; - // @ts-expect-error FIXME: These keys exists at runtime, perhaps I missed some type extension from `svelte/compiler`? const { start, end } = defineMetaVariableDeclaration; code.update( - start, - end, - toJs({ - type: 'Program', - sourceType: 'module', - body: [defineMetaVariableDeclaration], - }).value + start as number, + end as number, + toJs(defineMetaVariableDeclaration as unknown as Program).value ); } diff --git a/src/compiler/transform/remove-export-default.ts b/src/compiler/transform/remove-export-default.ts index c4ce2ba..9e35cbf 100644 --- a/src/compiler/transform/remove-export-default.ts +++ b/src/compiler/transform/remove-export-default.ts @@ -18,13 +18,12 @@ export function removeExportDefault(params: Params) { const { exportDefault, storiesFunctionDeclaration } = nodes; if (exportDefault.declaration.type === 'FunctionDeclaration') { - // @ts-expect-error FIXME: I couldn't find the right type (extension?) in the `estree`, but these exists at runtime const { start, end } = exportDefault; // NOTE: It updates code by removing `export default` from the stories function declaration. code.update( - start, - end, + start as number, + end as number, toJs({ type: 'Program', sourceType: 'module', @@ -34,7 +33,6 @@ export function removeExportDefault(params: Params) { } if (exportDefault.declaration.type === 'Identifier') { - // @ts-expect-error FIXME: I couldn't find the right type (extension?) in the `estree`, but these exists at runtime - code.remove(exportDefault.start, exportDefault.end); + code.remove(exportDefault.start as number, exportDefault.end as number); } } diff --git a/src/compiler/transform/story/index.ts b/src/compiler/transform/story/index.ts index 4155d5d..705bced 100644 --- a/src/compiler/transform/story/index.ts +++ b/src/compiler/transform/story/index.ts @@ -40,8 +40,7 @@ export function transformStory(params: Params): void { }); const { compiled } = component; - // @ts-expect-error FIXME: These keys exists at runtime, perhaps I missed some type extension from `svelte/compiler`? const { start, end } = compiled; - code.update(start, end, toJs(compiled as unknown as Program).value); + code.update(start as number, end as number, toJs(compiled as unknown as Program).value); } diff --git a/src/estree.d.ts b/src/estree.d.ts new file mode 100644 index 0000000..74ae286 --- /dev/null +++ b/src/estree.d.ts @@ -0,0 +1,15 @@ +// Copied & modified from: https://github.com/sveltejs/language-tools/blob/master/packages%2Fsvelte2tsx%2Fsrc%2Festree.d.ts +import 'estree'; + +// estree does not have start/end in their public Node interface, +// but the AST returned by svelte/compiler does. +// We add the those properties here and add Node as an interface +// to estree. + +declare module 'estree' { + export interface BaseNode { + start?: number; + end?: number; + [propName: string]: any; + } +} diff --git a/src/parser/extract/compiled/nodes.ts b/src/parser/extract/compiled/nodes.ts index b12283d..f30fd0c 100644 --- a/src/parser/extract/compiled/nodes.ts +++ b/src/parser/extract/compiled/nodes.ts @@ -9,6 +9,7 @@ import type { Program, VariableDeclaration, } from 'estree'; +import type { ProgramNode } from 'rollup'; import type { Visitors } from 'zimmerframe'; import { @@ -58,7 +59,7 @@ const AST_NODES_NAMES = { } as const; interface Params { - ast: Program; + ast: Program | ProgramNode; filename?: string; } @@ -147,7 +148,7 @@ export async function extractCompiledASTNodes(params: Params): Promise