Skip to content

Commit

Permalink
feat: processing for props emits of the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
a145789 committed Dec 21, 2022
1 parent 6b09c1d commit e992f3e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
46 changes: 45 additions & 1 deletion src/transform/emits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type {
ArrayExpression,
ObjectExpression,
ExportDefaultExpression,
NamedImportSpecifier,
ImportDefaultSpecifier,
ImportSpecifier,
} from "@swc/core";

import { Config, SetupAst } from "../constants";
Expand All @@ -27,6 +30,7 @@ function transformEmits(

const preCode = `const ${name} = `;
let str = "";
let isSameEmitsName = false;
class MyVisitor extends Visitor {
ms: MagicString;
constructor(ms: MagicString) {
Expand All @@ -39,6 +43,39 @@ function transformEmits(

return node;
}

visitImportDefaultSpecifier(n: ImportDefaultSpecifier): ImportSpecifier {
if (!isSameEmitsName) {
return n;
}
const { value, span } = n.local;
const { start, end } = getRealSpan(span, offset);
if (value === name) {
this.ms.update(start, end, `$${name}`);
}
return n;
}
visitNamedImportSpecifier(n: NamedImportSpecifier) {
if (!isSameEmitsName) {
return n;
}
const {
local: { value, span },
imported,
} = n;
if (!imported) {
if (value === name) {
const { end } = getRealSpan(span, offset);
this.ms.appendRight(end, ` as $${name}`);
}
} else {
if (value === name) {
const { start, end } = getRealSpan(span, offset);
this.ms.update(start, end, `$${name}`);
}
}
return n;
}
}

if (emitsAst.type === "ObjectExpression") {
Expand All @@ -49,7 +86,14 @@ function transformEmits(
}

if (emitsAst.type === "Identifier") {
str = `${preCode}defineEmits(${emitsAst.value});`;
if (name !== emitsAst.value) {
str = `${preCode}defineEmits(${emitsAst.value});\n`;
} else {
str = `${preCode}defineEmits($${emitsAst.value});\n`;
isSameEmitsName = true;
}
console.log(str);

return MyVisitor;
}

Expand Down
48 changes: 45 additions & 3 deletions src/transform/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import type {
ExportDefaultExpression,
Identifier,
ImportDeclaration,
ImportDefaultSpecifier,
ImportSpecifier,
KeyValueProperty,
NamedImportSpecifier,
ObjectExpression,
} from "@swc/core";
import { Config, FileType, SetupAst } from "../constants";
Expand All @@ -24,18 +27,19 @@ function transformProps(
const { script, offset, fileType, propsNotOnlyTs } = config;

let preCode = "";
let propsName = "";
if (setupAst.params.length) {
const propsNameAst =
setupAst.type === "MethodProperty"
? setupAst.params[0].pat
: setupAst.params[0];

const propsName =
propsNameAst.type === "Identifier" ? propsNameAst.value : "";
propsName = propsNameAst.type === "Identifier" ? propsNameAst.value : "";
preCode = propsName ? `const ${propsName} = ` : "";
}

let isNormalProps = true;
let isSamePropsName = false;
let str = "";
class MyVisitor extends Visitor {
ms: MagicString;
Expand Down Expand Up @@ -66,14 +70,52 @@ function transformProps(

return n;
}
visitImportDefaultSpecifier(n: ImportDefaultSpecifier): ImportSpecifier {
if (!isSamePropsName) {
return n;
}
const { value, span } = n.local;
const { start, end } = getRealSpan(span, offset);
if (value === propsName) {
this.ms.update(start, end, `$${propsName}`);
}
return n;
}
visitNamedImportSpecifier(n: NamedImportSpecifier) {
if (!isSamePropsName) {
return n;
}
const {
local: { value, span },
imported,
} = n;
if (!imported) {
if (value === propsName) {
const { end } = getRealSpan(span, offset);
this.ms.appendRight(end, ` as $${propsName}`);
}
} else {
if (value === propsName) {
const { start, end } = getRealSpan(span, offset);
this.ms.update(start, end, `$${propsName}`);
}
}
return n;
}
}
if (propsAst.type === "ArrayExpression") {
const { start, end } = getRealSpan(propsAst.span, offset);
str = `${preCode}defineProps(${script.slice(start, end)});\n`;
return MyVisitor;
}

if (propsAst.type === "Identifier") {
str = `${preCode}defineProps(${propsAst.value});\n`;
if (propsName !== propsAst.value) {
str = `${preCode}defineProps(${propsAst.value});\n`;
} else {
str = `${preCode}defineProps($${propsAst.value});\n`;
isSamePropsName = true;
}
return MyVisitor;
}

Expand Down
9 changes: 5 additions & 4 deletions src/transform/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,34 +170,35 @@ function transformScript(config: Config) {
return null;
}
try {
const value = ast.type === "Identifier" ? ast : ast.value;
switch (key) {
case "props": {
transformOption.props = transformProps(
ast.value as ArrayExpression | Identifier | ObjectExpression,
value as ArrayExpression | Identifier | ObjectExpression,
setupFnAst,
config,
);
break;
}
case "emits": {
transformOption.emits = transformEmits(
ast.value as ArrayExpression | Identifier | ObjectExpression,
value as ArrayExpression | Identifier | ObjectExpression,
setupFnAst,
config,
);
break;
}
case "components": {
transformOption.components = transformComponents(
ast.value as ArrayExpression | Identifier | ObjectExpression,
value as ArrayExpression | Identifier | ObjectExpression,
setupFnAst,
config,
);
break;
}
case "directives": {
transformOption.directives = transformDirectives(
ast.value as Identifier | ObjectExpression,
value as Identifier | ObjectExpression,
setupFnAst,
config,
);
Expand Down

0 comments on commit e992f3e

Please sign in to comment.