Skip to content

Commit 7864b63

Browse files
committed
fix: rust & js unicode length inconsistency
1 parent b4af4d2 commit 7864b63

File tree

9 files changed

+142
-127
lines changed

9 files changed

+142
-127
lines changed

example/src/views/Home.vue

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default {
99
Tab,
1010
},
1111
props: {
12+
// 源数据
1213
source: {
1314
type: String as PropType<"Main" | "visitor">
1415
},

src/transform/attrsAndSlots.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
ImportSpecifier,
55
} from "@swc/core";
66
import { Config, SetupAst } from "../constants";
7-
import { getSetupSecondParams } from "../utils";
7+
import { getRealSpan, getSetupSecondParams } from "../utils";
88
import { Visitor } from "@swc/core/Visitor.js";
99
import type MagicString from "magic-string";
1010

@@ -57,12 +57,9 @@ function transformAttrsAndSlots(
5757
const firstNode = n[0];
5858

5959
if (firstNode) {
60-
const {
61-
span: { start },
62-
} = firstNode;
63-
60+
const { start } = getRealSpan(firstNode.span, offset);
6461
ms.appendLeft(
65-
start - offset,
62+
start,
6663
`${!attrs ? "useAttrs, " : ""}${!slots ? "useSlots, " : ""}`,
6764
);
6865
}
@@ -78,11 +75,9 @@ function transformAttrsAndSlots(
7875
return node;
7976
}
8077

81-
const {
82-
span: { start },
83-
} = node;
78+
const { start } = getRealSpan(node.span, offset);
8479
ms.appendLeft(
85-
start - offset,
80+
start,
8681
`\n${!attrs ? `const ${attrsName} = useAttrs();\n` : ""}${
8782
!slots ? `const ${slotsName} = useSlots();\n` : ""
8883
}\n`,

src/transform/components.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
import { Config, SetupAst } from "../constants";
88
import { Visitor } from "@swc/core/Visitor.js";
99
import type MagicString from "magic-string";
10+
import { getRealSpan } from "../utils";
1011

1112
function transformComponents(
1213
componentsAst: ArrayExpression | Identifier | ObjectExpression,
@@ -27,11 +28,10 @@ function transformComponents(
2728
if (c.type === "KeyValueProperty" && c.key.type !== "Computed") {
2829
const key = c.key.value;
2930

30-
const {
31-
span: { start, end },
32-
} = c.value as Identifier;
31+
const { span } = c.value as Identifier;
3332

34-
p += `const ${key} = ${script.slice(start - offset, end - offset)};\n`;
33+
const { start, end } = getRealSpan(span, offset);
34+
p += `const ${key} = ${script.slice(start, end)};\n`;
3535
}
3636

3737
return p;
@@ -47,10 +47,8 @@ function transformComponents(
4747
this.ms = ms;
4848
}
4949
visitExportDefaultExpression(node: ExportDefaultExpression) {
50-
const {
51-
span: { start },
52-
} = node;
53-
this.ms.appendLeft(start - offset, str);
50+
const { start } = getRealSpan(node.span, offset);
51+
this.ms.appendLeft(start, str);
5452

5553
return node;
5654
}

src/transform/directives.ts

+15-31
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
ObjectExpression,
77
} from "@swc/core";
88
import { Config, SetupAst } from "../constants";
9-
import { output } from "../utils";
9+
import { getRealSpan, output } from "../utils";
1010
import { Visitor } from "@swc/core/Visitor.js";
1111
import type MagicString from "magic-string";
1212

@@ -46,13 +46,12 @@ function transformDirectives(
4646
if (c.type === "KeyValueProperty" && c.key.type !== "Computed") {
4747
const key = String(c.key.value);
4848

49-
const {
50-
span: { start, end },
51-
} = c.value as Identifier;
49+
const { span } = c.value as Identifier;
5250

51+
const { start, end } = getRealSpan(span, offset);
5352
p += `const v${
5453
key.slice(0, 1).toLocaleUpperCase() + key.slice(1)
55-
} = ${script.slice(start - offset, end - offset)};\n`;
54+
} = ${script.slice(start, end)};\n`;
5655
}
5756

5857
return p;
@@ -64,38 +63,27 @@ function transformDirectives(
6463
this.ms = ms;
6564
}
6665
visitImportDefaultSpecifier(n: ImportDefaultSpecifier) {
67-
const {
68-
value,
69-
span: { start, end },
70-
} = n.local;
66+
const { value, span } = n.local;
67+
const { start, end } = getRealSpan(span, offset);
7168
if (importDirective.includes(value)) {
72-
this.ms.update(
73-
start - offset,
74-
end - offset,
75-
transformDirectiveName(value),
76-
);
69+
this.ms.update(start, end, transformDirectiveName(value));
7770
}
7871
return n;
7972
}
8073
visitNamedImportSpecifier(n: NamedImportSpecifier) {
8174
const {
82-
local: { value, span: { start, end } },
75+
local: { value, span },
8376
imported,
8477
} = n;
8578
if (!imported) {
8679
if (importDirective.includes(value)) {
87-
this.ms.appendRight(
88-
end - offset,
89-
` as ${transformDirectiveName(value)}`,
90-
);
80+
const { end } = getRealSpan(span, offset);
81+
this.ms.appendRight(end, ` as ${transformDirectiveName(value)}`);
9182
}
9283
} else {
9384
if (importDirective.includes(value)) {
94-
this.ms.update(
95-
start - offset,
96-
end - offset,
97-
transformDirectiveName(value),
98-
);
85+
const { start, end } = getRealSpan(span, offset);
86+
this.ms.update(start, end, transformDirectiveName(value));
9987
}
10088
}
10189
return n;
@@ -104,13 +92,9 @@ function transformDirectives(
10492
if (!customDirective) {
10593
return n;
10694
}
107-
const {
108-
span: { start },
109-
} = n;
110-
this.ms.appendLeft(
111-
start - offset,
112-
`// custom directive \n${customDirective}`,
113-
);
95+
96+
const { start } = getRealSpan(n.span, offset);
97+
this.ms.appendLeft(start, `// custom directive \n${customDirective}`);
11498

11599
return n;
116100
}

src/transform/emits.ts

+12-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import type {
66
} from "@swc/core";
77

88
import { Config, SetupAst } from "../constants";
9-
import { GetCallExpressionFirstArg, getSetupSecondParams } from "../utils";
9+
import {
10+
GetCallExpressionFirstArg,
11+
getRealSpan,
12+
getSetupSecondParams,
13+
} from "../utils";
1014
import { Visitor } from "@swc/core/Visitor.js";
1115
import type MagicString from "magic-string";
1216

@@ -30,23 +34,16 @@ function transformEmits(
3034
this.ms = ms;
3135
}
3236
visitExportDefaultExpression(node: ExportDefaultExpression) {
33-
const {
34-
span: { start },
35-
} = node;
36-
this.ms.appendLeft(start - offset, str);
37+
const { start } = getRealSpan(node.span, offset);
38+
this.ms.appendLeft(start, str);
3739

3840
return node;
3941
}
4042
}
4143

4244
if (emitsAst.type === "ObjectExpression") {
43-
const {
44-
span: { start, end },
45-
} = emitsAst;
46-
str = `${preCode}defineEmits(${script.slice(
47-
start - offset,
48-
end - offset,
49-
)});\n`;
45+
const { start, end } = getRealSpan(emitsAst.span, offset);
46+
str = `${preCode}defineEmits(${script.slice(start, end)});\n`;
5047

5148
return MyVisitor;
5249
}
@@ -72,10 +69,9 @@ function transformEmits(
7269
}
7370

7471
const keys = emitsAst.elements.map((ast) => {
75-
const {
76-
span: { start, end },
77-
} = ast!.expression as Identifier;
78-
return script.slice(start - offset, end - offset);
72+
const { span } = ast!.expression as Identifier;
73+
const { start, end } = getRealSpan(span, offset);
74+
return script.slice(start, end);
7975
});
8076

8177
str = `${preCode}defineEmits([${[...keys, ...emitNames].join(", ")}]);\n`;

src/transform/expose.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { Config, SetupAst } from "../constants";
2-
import { GetCallExpressionFirstArg, getSetupSecondParams } from "../utils";
2+
import {
3+
GetCallExpressionFirstArg,
4+
getRealSpan,
5+
getSetupSecondParams,
6+
} from "../utils";
37
import {
48
ExportDefaultExpression,
59
KeyValueProperty,
@@ -74,19 +78,15 @@ function transformExpose(setupAst: SetupAst, config: Config) {
7478
stmt.expression.callee.type === "Identifier" &&
7579
stmt.expression.callee.value === name
7680
) {
77-
this.ms.remove(stmt.span.start - offset, stmt.span.end - offset);
81+
const { start, end } = getRealSpan(stmt.span, offset);
82+
this.ms.remove(start, end);
7883
}
7984
}
8085
return stmts;
8186
}
8287
visitExportDefaultExpression(node: ExportDefaultExpression) {
83-
const {
84-
span: { end },
85-
} = node;
86-
this.ms.appendRight(
87-
end - offset,
88-
`defineExpose({${exposeArg.join(",")}});\n`,
89-
);
88+
const { end } = getRealSpan(node.span, offset);
89+
this.ms.appendRight(end, `defineExpose({${exposeArg.join(",")}});\n`);
9090

9191
return node;
9292
}

src/transform/props.ts

+15-26
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import type {
88
ObjectExpression,
99
} from "@swc/core";
1010
import { Config, FileType, SetupAst } from "../constants";
11-
import { getPropsValueIdentifier, getSpecifierOffset } from "../utils";
11+
import {
12+
getPropsValueIdentifier,
13+
getRealSpan,
14+
getSpecifierOffset,
15+
} from "../utils";
1216
import { Visitor } from "@swc/core/Visitor.js";
1317
import type MagicString from "magic-string";
1418

@@ -40,10 +44,8 @@ function transformProps(
4044
this.ms = ms;
4145
}
4246
visitExportDefaultExpression(node: ExportDefaultExpression) {
43-
const {
44-
span: { start },
45-
} = node;
46-
this.ms.appendLeft(start - offset, str);
47+
const { start } = getRealSpan(node.span, offset);
48+
this.ms.appendLeft(start, str);
4749

4850
return node;
4951
}
@@ -57,23 +59,16 @@ function transformProps(
5759
);
5860
if (index !== -1) {
5961
const { start, end } = getSpecifierOffset(n, index, script, offset);
60-
61-
this.ms.remove(start - offset, end - offset);
62+
this.ms.remove(start, end);
6263
}
6364
}
6465

6566
return n;
6667
}
6768
}
6869
if (propsAst.type === "ArrayExpression") {
69-
const {
70-
span: { start, end },
71-
} = propsAst;
72-
73-
str = `${preCode}defineProps(${script.slice(
74-
start - offset,
75-
end - offset,
76-
)});\n`;
70+
const { start, end } = getRealSpan(propsAst.span, offset);
71+
str = `${preCode}defineProps(${script.slice(start, end)});\n`;
7772
return MyVisitor;
7873
}
7974
if (propsAst.type === "Identifier") {
@@ -115,13 +110,8 @@ function transformProps(
115110
);
116111

117112
if (isNormalProps) {
118-
const {
119-
span: { start, end },
120-
} = propsAst;
121-
str = `${preCode}defineProps(${script.slice(
122-
start - offset,
123-
end - offset,
124-
)});\n`;
113+
const { start, end } = getRealSpan(propsAst.span, offset);
114+
str = `${preCode}defineProps(${script.slice(start, end)});\n`;
125115
return MyVisitor;
126116
}
127117

@@ -168,10 +158,9 @@ function transformProps(
168158
}
169159

170160
if (typeKeyValue === "default") {
171-
const {
172-
span: { start, end },
173-
} = c.value as Identifier;
174-
p.defaultProp = script.slice(start - offset, end - offset);
161+
const { span } = c.value as Identifier;
162+
const { start, end } = getRealSpan(span, offset);
163+
p.defaultProp = script.slice(start, end);
175164
}
176165
return p;
177166
}, {});

0 commit comments

Comments
 (0)