Skip to content

Commit

Permalink
fix: emit generates the name of the error
Browse files Browse the repository at this point in the history
  • Loading branch information
a145789 authored Dec 26, 2022
1 parent 5281eb6 commit a699991
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export interface Config {
offset: number;
fileAbsolutePath: string;
propsNotOnlyTs?: boolean;
setupScript?: string;
}

export const parseOption = {
Expand Down
11 changes: 6 additions & 5 deletions src/transform/emits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
NamedImportSpecifier,
ImportDefaultSpecifier,
ImportSpecifier,
BlockStatement,
} from "@swc/core";

import { Config, SetupAst } from "../constants";
Expand All @@ -22,7 +23,7 @@ function transformEmits(
setupAst: SetupAst,
config: Config,
) {
const { script, offset, setupScript, fileAbsolutePath } = config;
const { script, offset, fileAbsolutePath } = config;
const name = getSetupSecondParams("emit", setupAst, fileAbsolutePath);
if (!name) {
return;
Expand Down Expand Up @@ -106,15 +107,15 @@ function transformEmits(
}

let emitNames: string[] = [];
if (setupScript) {
if ((setupAst.body as BlockStatement)?.stmts?.length) {
const visitor = new GetCallExpressionFirstArg(name);
visitor.visitFn(setupAst);

const setupOffset = setupAst.span.start;
emitNames = (visitor.firstArgAst as Identifier[]).map((ast) => {
const { start, end } = getRealSpan(ast.span, setupOffset);
return setupScript.slice(start, end);
const { start, end } = getRealSpan(ast.span, offset);
return script.slice(start, end);
});
console.log(emitNames);
}

str = `${preCode}defineEmits([${[...new Set([...keys, ...emitNames])].join(
Expand Down
10 changes: 5 additions & 5 deletions src/transform/expose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getSetupSecondParams,
} from "../utils";
import {
BlockStatement,
ExportDefaultExpression,
KeyValueProperty,
MethodProperty,
Expand All @@ -14,27 +15,26 @@ import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";

function transformExpose(setupAst: SetupAst, config: Config) {
const { setupScript, offset, fileAbsolutePath } = config;
const { script, offset, fileAbsolutePath } = config;
const name = getSetupSecondParams("expose", setupAst, fileAbsolutePath);
if (!name) {
return;
}

let exposeArg: string[] = [];
if (setupScript) {
if ((setupAst.body as BlockStatement)?.stmts?.length) {
const visitor = new GetCallExpressionFirstArg(name);
visitor.visitFn(setupAst);

const setupOffset = setupAst.span.start;
exposeArg = visitor.firstArgAst
.flatMap((ast) => {
if (ast.type !== "ObjectExpression") {
return "";
}

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

return setupScript.slice(start, end).replace(/{|}/g, "").split(",");
return script.slice(start, end).replace(/{|}/g, "").split(",");
})
.filter((s) => Boolean(s.trim()));
}
Expand Down
6 changes: 0 additions & 6 deletions src/transform/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ function transformScript(config: Config) {
? setupAst
: (setupAst.value as ArrowFunctionExpression);

const setupFnAstSpan = getRealSpan(setupFnAst.span, config.offset);
config.setupScript = config.script.slice(
setupFnAstSpan.start,
setupFnAstSpan.end,
);

const transformOption: TransformOption = {};
for (const ast of optionAst.properties) {
if (ast.type === "SpreadElement") {
Expand Down
1 change: 0 additions & 1 deletion src/transform/sfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export function transformSfc(path: string, option: CommandsOption) {
script: script.content.trim(),
offset: 0,
fileAbsolutePath: path,
setupScript: "",
});
} catch (error) {
output.error(`transform script failed in the ${path}`);
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ export class GetCallExpressionFirstArg extends Visitor {
) {
this.firstArgAst.push(n.arguments[0].expression);
}

super.visitCallExpression(n);
return n;
}

Expand Down
3 changes: 0 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ describe("test transform script", () => {
script: testScript1.code.trim(),
offset: 0,
fileAbsolutePath: "",
setupScript: "",
}),
),
).toBe(transformToSingeLine(testScript1.transform.trim()));
Expand All @@ -98,7 +97,6 @@ describe("test transform script", () => {
script: testScript2.code.trim(),
offset: 0,
fileAbsolutePath: "",
setupScript: "",
}),
),
).toBe(transformToSingeLine(testScript2.transform.trim()));
Expand All @@ -110,7 +108,6 @@ describe("test transform script", () => {
script: testScript3.code.trim(),
offset: 0,
fileAbsolutePath: "",
setupScript: "",
}),
),
).toBe(transformToSingeLine(testScript3.transform.trim()));
Expand Down

0 comments on commit a699991

Please sign in to comment.