Skip to content

Commit

Permalink
Resolve FIXMEs related to estree
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed Jun 18, 2024
1 parent ddac3c8 commit 3f8b162
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
12 changes: 4 additions & 8 deletions src/compiler/transform/define-meta/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Program } from 'estree';
import { toJs } from 'estree-util-to-js';
import type MagicString from 'magic-string';

Expand Down Expand Up @@ -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
);
}
8 changes: 3 additions & 5 deletions src/compiler/transform/remove-export-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
}
}
3 changes: 1 addition & 2 deletions src/compiler/transform/story/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
15 changes: 15 additions & 0 deletions src/estree.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 3 additions & 2 deletions src/parser/extract/compiled/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
Program,
VariableDeclaration,
} from 'estree';
import type { ProgramNode } from 'rollup';
import type { Visitors } from 'zimmerframe';

import {
Expand Down Expand Up @@ -58,7 +59,7 @@ const AST_NODES_NAMES = {
} as const;

interface Params {
ast: Program;
ast: Program | ProgramNode;
filename?: string;
}

Expand Down Expand Up @@ -147,7 +148,7 @@ export async function extractCompiledASTNodes(params: Params): Promise<CompiledA
},
};

walk(ast, state, visitors);
walk(ast as Program, state, visitors);

const {
defineMetaImport,
Expand Down

0 comments on commit 3f8b162

Please sign in to comment.