Skip to content

Commit a699991

Browse files
authored
fix: emit generates the name of the error
1 parent 5281eb6 commit a699991

File tree

7 files changed

+13
-21
lines changed

7 files changed

+13
-21
lines changed

src/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export interface Config {
3030
offset: number;
3131
fileAbsolutePath: string;
3232
propsNotOnlyTs?: boolean;
33-
setupScript?: string;
3433
}
3534

3635
export const parseOption = {

src/transform/emits.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
NamedImportSpecifier,
77
ImportDefaultSpecifier,
88
ImportSpecifier,
9+
BlockStatement,
910
} from "@swc/core";
1011

1112
import { Config, SetupAst } from "../constants";
@@ -22,7 +23,7 @@ function transformEmits(
2223
setupAst: SetupAst,
2324
config: Config,
2425
) {
25-
const { script, offset, setupScript, fileAbsolutePath } = config;
26+
const { script, offset, fileAbsolutePath } = config;
2627
const name = getSetupSecondParams("emit", setupAst, fileAbsolutePath);
2728
if (!name) {
2829
return;
@@ -106,15 +107,15 @@ function transformEmits(
106107
}
107108

108109
let emitNames: string[] = [];
109-
if (setupScript) {
110+
if ((setupAst.body as BlockStatement)?.stmts?.length) {
110111
const visitor = new GetCallExpressionFirstArg(name);
111112
visitor.visitFn(setupAst);
112113

113-
const setupOffset = setupAst.span.start;
114114
emitNames = (visitor.firstArgAst as Identifier[]).map((ast) => {
115-
const { start, end } = getRealSpan(ast.span, setupOffset);
116-
return setupScript.slice(start, end);
115+
const { start, end } = getRealSpan(ast.span, offset);
116+
return script.slice(start, end);
117117
});
118+
console.log(emitNames);
118119
}
119120

120121
str = `${preCode}defineEmits([${[...new Set([...keys, ...emitNames])].join(

src/transform/expose.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getSetupSecondParams,
66
} from "../utils";
77
import {
8+
BlockStatement,
89
ExportDefaultExpression,
910
KeyValueProperty,
1011
MethodProperty,
@@ -14,27 +15,26 @@ import { Visitor } from "@swc/core/Visitor.js";
1415
import type MagicString from "magic-string";
1516

1617
function transformExpose(setupAst: SetupAst, config: Config) {
17-
const { setupScript, offset, fileAbsolutePath } = config;
18+
const { script, offset, fileAbsolutePath } = config;
1819
const name = getSetupSecondParams("expose", setupAst, fileAbsolutePath);
1920
if (!name) {
2021
return;
2122
}
2223

2324
let exposeArg: string[] = [];
24-
if (setupScript) {
25+
if ((setupAst.body as BlockStatement)?.stmts?.length) {
2526
const visitor = new GetCallExpressionFirstArg(name);
2627
visitor.visitFn(setupAst);
2728

28-
const setupOffset = setupAst.span.start;
2929
exposeArg = visitor.firstArgAst
3030
.flatMap((ast) => {
3131
if (ast.type !== "ObjectExpression") {
3232
return "";
3333
}
3434

35-
const { start, end } = getRealSpan(ast.span, setupOffset);
35+
const { start, end } = getRealSpan(ast.span, offset);
3636

37-
return setupScript.slice(start, end).replace(/{|}/g, "").split(",");
37+
return script.slice(start, end).replace(/{|}/g, "").split(",");
3838
})
3939
.filter((s) => Boolean(s.trim()));
4040
}

src/transform/script.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,6 @@ function transformScript(config: Config) {
138138
? setupAst
139139
: (setupAst.value as ArrowFunctionExpression);
140140

141-
const setupFnAstSpan = getRealSpan(setupFnAst.span, config.offset);
142-
config.setupScript = config.script.slice(
143-
setupFnAstSpan.start,
144-
setupFnAstSpan.end,
145-
);
146-
147141
const transformOption: TransformOption = {};
148142
for (const ast of optionAst.properties) {
149143
if (ast.type === "SpreadElement") {

src/transform/sfc.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export function transformSfc(path: string, option: CommandsOption) {
2323
script: script.content.trim(),
2424
offset: 0,
2525
fileAbsolutePath: path,
26-
setupScript: "",
2726
});
2827
} catch (error) {
2928
output.error(`transform script failed in the ${path}`);

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ export class GetCallExpressionFirstArg extends Visitor {
223223
) {
224224
this.firstArgAst.push(n.arguments[0].expression);
225225
}
226+
227+
super.visitCallExpression(n);
226228
return n;
227229
}
228230

test/index.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ describe("test transform script", () => {
8686
script: testScript1.code.trim(),
8787
offset: 0,
8888
fileAbsolutePath: "",
89-
setupScript: "",
9089
}),
9190
),
9291
).toBe(transformToSingeLine(testScript1.transform.trim()));
@@ -98,7 +97,6 @@ describe("test transform script", () => {
9897
script: testScript2.code.trim(),
9998
offset: 0,
10099
fileAbsolutePath: "",
101-
setupScript: "",
102100
}),
103101
),
104102
).toBe(transformToSingeLine(testScript2.transform.trim()));
@@ -110,7 +108,6 @@ describe("test transform script", () => {
110108
script: testScript3.code.trim(),
111109
offset: 0,
112110
fileAbsolutePath: "",
113-
setupScript: "",
114111
}),
115112
),
116113
).toBe(transformToSingeLine(testScript3.transform.trim()));

0 commit comments

Comments
 (0)