Skip to content

Commit a5348af

Browse files
committed
feat: rewrite transform
1 parent b8e8eba commit a5348af

File tree

12 files changed

+341
-253
lines changed

12 files changed

+341
-253
lines changed

example/src/views/Home.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { PropType, ref, watch } from 'vue'
33
import Header from "../components/Header.vue"
44
import Tab from "../components/Tab.vue"
5-
export default ({
5+
export default {
66
name: 'Home',
77
components: {
88
Header,
@@ -30,7 +30,7 @@ export default ({
3030
})
3131
return { foo }
3232
}
33-
})
33+
}
3434
</script>
3535

3636
<template>

rome.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"linter": {
3+
"ignore": ["./node_modules/**"],
34
"enabled": true,
45
"rules": {
56
"recommended": true,

src/constants.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import type {
33
ArrowFunctionExpression,
44
MethodProperty,
55
} from "@swc/core";
6-
import type { Visitor } from "@swc/core/Visitor";
7-
import type MagicString from "magic-string";
86

97
export const enum FileType {
108
js,
@@ -42,11 +40,3 @@ export const parseOption = {
4240
} as ParseOptions;
4341

4442
export type SetupAst = ArrowFunctionExpression | MethodProperty;
45-
46-
export const USE_ATTRS = "useAttrs" as const;
47-
export const USE_SLOTS = "useSlots" as const;
48-
49-
export interface VisitorCb extends Partial<Visitor> {
50-
ms?: MagicString;
51-
[key: string]: any;
52-
}

src/transform/attrsAndSlots.ts

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { ImportSpecifier } from "@swc/core";
2-
import {
3-
Config,
4-
SetupAst,
5-
USE_ATTRS,
6-
USE_SLOTS,
7-
VisitorCb,
8-
} from "../constants";
1+
import type {
2+
ExportDefaultExpression,
3+
ImportDeclaration,
4+
ImportSpecifier,
5+
} from "@swc/core";
6+
import { Config, SetupAst } from "../constants";
97
import { getSetupSecondParams } from "../utils";
8+
import { Visitor } from "@swc/core/Visitor.js";
9+
import type MagicString from "magic-string";
1010

1111
function transformAttrsAndSlots(
1212
setupAst: SetupAst,
@@ -18,21 +18,30 @@ function transformAttrsAndSlots(
1818
return;
1919
}
2020

21-
const visitCb: VisitorCb = {
22-
visitImportDeclaration(n) {
21+
class MyVisitor extends Visitor {
22+
ms: MagicString;
23+
isHas = {
24+
attrs: false,
25+
slots: false,
26+
};
27+
constructor(ms: MagicString) {
28+
super();
29+
this.ms = ms;
30+
}
31+
visitImportDeclaration(n: ImportDeclaration) {
2332
if (n.source.value === "vue") {
2433
this.myVisitImportSpecifiers(n.specifiers);
2534
}
2635
return n;
27-
},
36+
}
2837
myVisitImportSpecifiers(n: ImportSpecifier[]) {
2938
this.isHas = n.reduce((p, ast) => {
3039
if (ast.type === "ImportSpecifier") {
31-
if (ast.local.value === USE_ATTRS) {
40+
if (ast.local.value === "useAttrs") {
3241
p.attrs = true;
3342
}
3443

35-
if (ast.local.value === USE_SLOTS) {
44+
if (ast.local.value === "useSlots") {
3645
p.slots = true;
3746
}
3847
}
@@ -43,55 +52,47 @@ function transformAttrsAndSlots(
4352
const {
4453
ms,
4554
isHas: { attrs, slots },
46-
declarationEnd,
4755
} = this;
4856

49-
const lastNode = n[n.length - 1];
57+
const firstNode = n[0];
5058

51-
if (lastNode) {
59+
if (firstNode) {
5260
const {
53-
span: { end },
54-
} = lastNode;
61+
span: { start },
62+
} = firstNode;
5563

56-
ms?.appendRight(
57-
end - offset,
58-
`${
59-
ms
60-
?.toString()
61-
.slice(end - offset, declarationEnd - offset)
62-
.includes(",")
63-
? ""
64-
: ", "
65-
}${!attrs ? `${USE_ATTRS}, ` : ""}${!slots ? `${USE_SLOTS}, ` : ""}`,
64+
ms.appendLeft(
65+
start - offset,
66+
`${!attrs ? "useAttrs, " : ""}${!slots ? "useSlots, " : ""}`,
6667
);
6768
}
6869

6970
return n;
70-
},
71-
visitExportDefaultExpression(node) {
71+
}
72+
visitExportDefaultExpression(node: ExportDefaultExpression) {
7273
const {
7374
ms,
7475
isHas: { attrs, slots },
7576
} = this;
76-
if (!(attrs && slots)) {
77+
if (attrs && slots) {
7778
return node;
7879
}
7980

8081
const {
8182
span: { start },
8283
} = node;
83-
ms?.appendLeft(
84+
ms.appendLeft(
8485
start - offset,
8586
`\n${!attrs ? `const ${attrsName} = useAttrs();\n` : ""}${
8687
!slots ? `const ${slotsName} = useSlots();\n` : ""
8788
}\n`,
8889
);
8990

9091
return node;
91-
},
92-
};
92+
}
93+
}
9394

94-
return visitCb;
95+
return MyVisitor;
9596
}
9697

9798
export default transformAttrsAndSlots;

src/transform/components.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import type { ArrayExpression, Identifier, ObjectExpression } from "@swc/core";
2-
import { Config, SetupAst, VisitorCb } from "../constants";
1+
import type {
2+
ArrayExpression,
3+
ExportDefaultExpression,
4+
Identifier,
5+
ObjectExpression,
6+
} from "@swc/core";
7+
import { Config, SetupAst } from "../constants";
8+
import { Visitor } from "@swc/core/Visitor.js";
9+
import type MagicString from "magic-string";
310

411
function transformComponents(
512
componentsAst: ArrayExpression | Identifier | ObjectExpression,
@@ -33,16 +40,22 @@ function transformComponents(
3340
if (!str) {
3441
return;
3542
} else {
36-
return {
37-
visitExportDefaultExpression(node) {
43+
class MyVisitor extends Visitor {
44+
ms: MagicString;
45+
constructor(ms: MagicString) {
46+
super();
47+
this.ms = ms;
48+
}
49+
visitExportDefaultExpression(node: ExportDefaultExpression) {
3850
const {
3951
span: { start },
4052
} = node;
41-
this.ms?.appendLeft(start - offset, str);
53+
this.ms.appendLeft(start - offset, str);
4254

4355
return node;
44-
},
45-
} as VisitorCb;
56+
}
57+
}
58+
return MyVisitor;
4659
}
4760
}
4861

src/transform/directives.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import type { Identifier, ObjectExpression } from "@swc/core";
2-
import { Config, SetupAst, VisitorCb } from "../constants";
1+
import type {
2+
ExportDefaultExpression,
3+
Identifier,
4+
ImportDefaultSpecifier,
5+
NamedImportSpecifier,
6+
ObjectExpression,
7+
} from "@swc/core";
8+
import { Config, SetupAst } from "../constants";
39
import { output } from "../utils";
10+
import { Visitor } from "@swc/core/Visitor.js";
11+
import type MagicString from "magic-string";
412

513
function transformDirectiveName(name: string) {
614
return `v${name.slice(0, 1).toLocaleUpperCase() + name.slice(1)}`;
@@ -49,58 +57,66 @@ function transformDirectives(
4957

5058
return p;
5159
}, "");
52-
53-
const visitCb: VisitorCb = {
54-
visitImportDefaultSpecifier(n) {
60+
class MyVisitor extends Visitor {
61+
ms: MagicString;
62+
constructor(ms: MagicString) {
63+
super();
64+
this.ms = ms;
65+
}
66+
visitImportDefaultSpecifier(n: ImportDefaultSpecifier) {
5567
const {
5668
value,
5769
span: { start, end },
5870
} = n.local;
5971
if (importDirective.includes(value)) {
60-
this.ms?.update(
72+
this.ms.update(
6173
start - offset,
6274
end - offset,
6375
transformDirectiveName(value),
6476
);
6577
}
6678
return n;
67-
},
68-
visitNamedImportSpecifier(n) {
79+
}
80+
visitNamedImportSpecifier(n: NamedImportSpecifier) {
6981
const {
7082
local: { value, span: { start, end } },
7183
imported,
7284
} = n;
7385
if (!imported) {
7486
if (importDirective.includes(value)) {
75-
this.ms?.appendRight(
87+
this.ms.appendRight(
7688
end - offset,
7789
` as ${transformDirectiveName(value)}`,
7890
);
7991
}
8092
} else {
8193
if (importDirective.includes(value)) {
82-
this.ms?.update(start, end, transformDirectiveName(value));
94+
this.ms.update(
95+
start - offset,
96+
end - offset,
97+
transformDirectiveName(value),
98+
);
8399
}
84100
}
85101
return n;
86-
},
87-
};
88-
89-
if (customDirective) {
90-
visitCb.visitExportDefaultExpression = function (node) {
102+
}
103+
visitExportDefaultExpression(n: ExportDefaultExpression) {
104+
if (!customDirective) {
105+
return n;
106+
}
91107
const {
92108
span: { start },
93-
} = node;
94-
this.ms?.appendLeft(
109+
} = n;
110+
this.ms.appendLeft(
95111
start - offset,
96112
`// custom directive \n${customDirective}`,
97113
);
98114

99-
return node;
100-
};
115+
return n;
116+
}
101117
}
102118

103-
return visitCb;
119+
return MyVisitor;
104120
}
105121

106122
export default transformDirectives;

src/transform/emits.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import type { Identifier, ArrayExpression, ObjectExpression } from "@swc/core";
1+
import type {
2+
Identifier,
3+
ArrayExpression,
4+
ObjectExpression,
5+
ExportDefaultExpression,
6+
} from "@swc/core";
27

3-
import { Config, SetupAst, VisitorCb } from "../constants";
8+
import { Config, SetupAst } from "../constants";
49
import { GetCallExpressionFirstArg, getSetupSecondParams } from "../utils";
10+
import { Visitor } from "@swc/core/Visitor.js";
11+
import type MagicString from "magic-string";
512

613
function transformEmits(
714
emitsAst: ArrayExpression | ObjectExpression | Identifier,
@@ -16,16 +23,21 @@ function transformEmits(
1623

1724
const preCode = `const ${name} = `;
1825
let str = "";
19-
const visitStrCb: VisitorCb = {
20-
visitExportDefaultExpression(node) {
26+
class MyVisitor extends Visitor {
27+
ms: MagicString;
28+
constructor(ms: MagicString) {
29+
super();
30+
this.ms = ms;
31+
}
32+
visitExportDefaultExpression(node: ExportDefaultExpression) {
2133
const {
2234
span: { start },
2335
} = node;
24-
this.ms?.appendLeft(start - offset, str);
36+
this.ms.appendLeft(start - offset, str);
2537

2638
return node;
27-
},
28-
};
39+
}
40+
}
2941

3042
if (emitsAst.type === "ObjectExpression") {
3143
const {
@@ -34,14 +46,14 @@ function transformEmits(
3446
str = `${preCode}defineEmits(${script.slice(
3547
start - offset,
3648
end - offset,
37-
)});`;
49+
)});\n`;
3850

39-
return visitStrCb;
51+
return MyVisitor;
4052
}
4153

4254
if (emitsAst.type === "Identifier") {
4355
str = `${preCode}defineEmits(${emitsAst.value});`;
44-
return visitStrCb;
56+
return MyVisitor;
4557
}
4658

4759
let emitNames: string[] = [];
@@ -66,8 +78,8 @@ function transformEmits(
6678
return script.slice(start - offset, end - offset);
6779
});
6880

69-
str = `${preCode}defineEmits([${[...keys, ...emitNames].join(", ")}]);`;
70-
return visitStrCb;
81+
str = `${preCode}defineEmits([${[...keys, ...emitNames].join(", ")}]);\n`;
82+
return MyVisitor;
7183
}
7284

7385
export default transformEmits;

0 commit comments

Comments
 (0)