Skip to content

Commit

Permalink
fix: rust & js unicode length inconsistency
Browse files Browse the repository at this point in the history
  • Loading branch information
a145789 committed Dec 20, 2022
1 parent b4af4d2 commit 7864b63
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 127 deletions.
1 change: 1 addition & 0 deletions example/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default {
Tab,
},
props: {
// 源数据
source: {
type: String as PropType<"Main" | "visitor">
},
Expand Down
15 changes: 5 additions & 10 deletions src/transform/attrsAndSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
ImportSpecifier,
} from "@swc/core";
import { Config, SetupAst } from "../constants";
import { getSetupSecondParams } from "../utils";
import { getRealSpan, getSetupSecondParams } from "../utils";
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";

Expand Down Expand Up @@ -57,12 +57,9 @@ function transformAttrsAndSlots(
const firstNode = n[0];

if (firstNode) {
const {
span: { start },
} = firstNode;

const { start } = getRealSpan(firstNode.span, offset);
ms.appendLeft(
start - offset,
start,
`${!attrs ? "useAttrs, " : ""}${!slots ? "useSlots, " : ""}`,
);
}
Expand All @@ -78,11 +75,9 @@ function transformAttrsAndSlots(
return node;
}

const {
span: { start },
} = node;
const { start } = getRealSpan(node.span, offset);
ms.appendLeft(
start - offset,
start,
`\n${!attrs ? `const ${attrsName} = useAttrs();\n` : ""}${
!slots ? `const ${slotsName} = useSlots();\n` : ""
}\n`,
Expand Down
14 changes: 6 additions & 8 deletions src/transform/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
import { Config, SetupAst } from "../constants";
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";
import { getRealSpan } from "../utils";

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

const {
span: { start, end },
} = c.value as Identifier;
const { span } = c.value as Identifier;

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

return p;
Expand All @@ -47,10 +47,8 @@ function transformComponents(
this.ms = ms;
}
visitExportDefaultExpression(node: ExportDefaultExpression) {
const {
span: { start },
} = node;
this.ms.appendLeft(start - offset, str);
const { start } = getRealSpan(node.span, offset);
this.ms.appendLeft(start, str);

return node;
}
Expand Down
46 changes: 15 additions & 31 deletions src/transform/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
ObjectExpression,
} from "@swc/core";
import { Config, SetupAst } from "../constants";
import { output } from "../utils";
import { getRealSpan, output } from "../utils";
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";

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

const {
span: { start, end },
} = c.value as Identifier;
const { span } = c.value as Identifier;

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

return p;
Expand All @@ -64,38 +63,27 @@ function transformDirectives(
this.ms = ms;
}
visitImportDefaultSpecifier(n: ImportDefaultSpecifier) {
const {
value,
span: { start, end },
} = n.local;
const { value, span } = n.local;
const { start, end } = getRealSpan(span, offset);
if (importDirective.includes(value)) {
this.ms.update(
start - offset,
end - offset,
transformDirectiveName(value),
);
this.ms.update(start, end, transformDirectiveName(value));
}
return n;
}
visitNamedImportSpecifier(n: NamedImportSpecifier) {
const {
local: { value, span: { start, end } },
local: { value, span },
imported,
} = n;
if (!imported) {
if (importDirective.includes(value)) {
this.ms.appendRight(
end - offset,
` as ${transformDirectiveName(value)}`,
);
const { end } = getRealSpan(span, offset);
this.ms.appendRight(end, ` as ${transformDirectiveName(value)}`);
}
} else {
if (importDirective.includes(value)) {
this.ms.update(
start - offset,
end - offset,
transformDirectiveName(value),
);
const { start, end } = getRealSpan(span, offset);
this.ms.update(start, end, transformDirectiveName(value));
}
}
return n;
Expand All @@ -104,13 +92,9 @@ function transformDirectives(
if (!customDirective) {
return n;
}
const {
span: { start },
} = n;
this.ms.appendLeft(
start - offset,
`// custom directive \n${customDirective}`,
);

const { start } = getRealSpan(n.span, offset);
this.ms.appendLeft(start, `// custom directive \n${customDirective}`);

return n;
}
Expand Down
28 changes: 12 additions & 16 deletions src/transform/emits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import type {
} from "@swc/core";

import { Config, SetupAst } from "../constants";
import { GetCallExpressionFirstArg, getSetupSecondParams } from "../utils";
import {
GetCallExpressionFirstArg,
getRealSpan,
getSetupSecondParams,
} from "../utils";
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";

Expand All @@ -30,23 +34,16 @@ function transformEmits(
this.ms = ms;
}
visitExportDefaultExpression(node: ExportDefaultExpression) {
const {
span: { start },
} = node;
this.ms.appendLeft(start - offset, str);
const { start } = getRealSpan(node.span, offset);
this.ms.appendLeft(start, str);

return node;
}
}

if (emitsAst.type === "ObjectExpression") {
const {
span: { start, end },
} = emitsAst;
str = `${preCode}defineEmits(${script.slice(
start - offset,
end - offset,
)});\n`;
const { start, end } = getRealSpan(emitsAst.span, offset);
str = `${preCode}defineEmits(${script.slice(start, end)});\n`;

return MyVisitor;
}
Expand All @@ -72,10 +69,9 @@ function transformEmits(
}

const keys = emitsAst.elements.map((ast) => {
const {
span: { start, end },
} = ast!.expression as Identifier;
return script.slice(start - offset, end - offset);
const { span } = ast!.expression as Identifier;
const { start, end } = getRealSpan(span, offset);
return script.slice(start, end);
});

str = `${preCode}defineEmits([${[...keys, ...emitNames].join(", ")}]);\n`;
Expand Down
18 changes: 9 additions & 9 deletions src/transform/expose.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Config, SetupAst } from "../constants";
import { GetCallExpressionFirstArg, getSetupSecondParams } from "../utils";
import {
GetCallExpressionFirstArg,
getRealSpan,
getSetupSecondParams,
} from "../utils";
import {
ExportDefaultExpression,
KeyValueProperty,
Expand Down Expand Up @@ -74,19 +78,15 @@ function transformExpose(setupAst: SetupAst, config: Config) {
stmt.expression.callee.type === "Identifier" &&
stmt.expression.callee.value === name
) {
this.ms.remove(stmt.span.start - offset, stmt.span.end - offset);
const { start, end } = getRealSpan(stmt.span, offset);
this.ms.remove(start, end);
}
}
return stmts;
}
visitExportDefaultExpression(node: ExportDefaultExpression) {
const {
span: { end },
} = node;
this.ms.appendRight(
end - offset,
`defineExpose({${exposeArg.join(",")}});\n`,
);
const { end } = getRealSpan(node.span, offset);
this.ms.appendRight(end, `defineExpose({${exposeArg.join(",")}});\n`);

return node;
}
Expand Down
41 changes: 15 additions & 26 deletions src/transform/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import type {
ObjectExpression,
} from "@swc/core";
import { Config, FileType, SetupAst } from "../constants";
import { getPropsValueIdentifier, getSpecifierOffset } from "../utils";
import {
getPropsValueIdentifier,
getRealSpan,
getSpecifierOffset,
} from "../utils";
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";

Expand Down Expand Up @@ -40,10 +44,8 @@ function transformProps(
this.ms = ms;
}
visitExportDefaultExpression(node: ExportDefaultExpression) {
const {
span: { start },
} = node;
this.ms.appendLeft(start - offset, str);
const { start } = getRealSpan(node.span, offset);
this.ms.appendLeft(start, str);

return node;
}
Expand All @@ -57,23 +59,16 @@ function transformProps(
);
if (index !== -1) {
const { start, end } = getSpecifierOffset(n, index, script, offset);

this.ms.remove(start - offset, end - offset);
this.ms.remove(start, end);
}
}

return n;
}
}
if (propsAst.type === "ArrayExpression") {
const {
span: { start, end },
} = propsAst;

str = `${preCode}defineProps(${script.slice(
start - offset,
end - offset,
)});\n`;
const { start, end } = getRealSpan(propsAst.span, offset);
str = `${preCode}defineProps(${script.slice(start, end)});\n`;
return MyVisitor;
}
if (propsAst.type === "Identifier") {
Expand Down Expand Up @@ -115,13 +110,8 @@ function transformProps(
);

if (isNormalProps) {
const {
span: { start, end },
} = propsAst;
str = `${preCode}defineProps(${script.slice(
start - offset,
end - offset,
)});\n`;
const { start, end } = getRealSpan(propsAst.span, offset);
str = `${preCode}defineProps(${script.slice(start, end)});\n`;
return MyVisitor;
}

Expand Down Expand Up @@ -168,10 +158,9 @@ function transformProps(
}

if (typeKeyValue === "default") {
const {
span: { start, end },
} = c.value as Identifier;
p.defaultProp = script.slice(start - offset, end - offset);
const { span } = c.value as Identifier;
const { start, end } = getRealSpan(span, offset);
p.defaultProp = script.slice(start, end);
}
return p;
}, {});
Expand Down
Loading

0 comments on commit 7864b63

Please sign in to comment.