Skip to content

Commit 5281eb6

Browse files
authored
feat: optimize the Only-Ts display when the props type is an array
1 parent e74994b commit 5281eb6

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

src/transform/props.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ import type {
1111
ObjectExpression,
1212
} from "@swc/core";
1313
import { Config, FileType, SetupAst } from "../constants";
14-
import {
15-
getPropsValueIdentifier,
16-
getRealSpan,
17-
getSpecifierOffset,
18-
} from "../utils";
14+
import { getPropsValue, getRealSpan, getSpecifierOffset } from "../utils";
1915
import { Visitor } from "@swc/core/Visitor.js";
2016
import type MagicString from "magic-string";
2117

@@ -162,12 +158,7 @@ function transformProps(
162158
const propsType = (propsAst.properties as KeyValueProperty[]).map(
163159
({ key, value }) => {
164160
const keyValue = (key as Identifier).value;
165-
const valueIdentifier = getPropsValueIdentifier(
166-
value,
167-
keyValue,
168-
script,
169-
offset,
170-
);
161+
const valueIdentifier = getPropsValue(value, keyValue, script, offset);
171162
if (valueIdentifier) {
172163
return valueIdentifier;
173164
}
@@ -191,7 +182,7 @@ function transformProps(
191182
}>((p, c) => {
192183
const typeKeyValue = (c.key as Identifier).value;
193184
if (typeKeyValue === "type") {
194-
p.propType = getPropsValueIdentifier(
185+
p.propType = getPropsValue(
195186
c.value,
196187
keyValue,
197188
script,

src/utils.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,36 @@ function getFgVueFile(paths: string[]) {
8080
return fg.sync(paths).filter((p) => p.endsWith(".vue"));
8181
}
8282

83-
export function getPropsValueIdentifier(
83+
function getPropsValueIdentifier(identifier: string) {
84+
let value = "";
85+
switch (identifier) {
86+
case "Function":
87+
case "Date": {
88+
value = identifier;
89+
break;
90+
}
91+
case "Array": {
92+
value = "any[]";
93+
break;
94+
}
95+
default: {
96+
value = identifier.toLocaleLowerCase();
97+
break;
98+
}
99+
}
100+
return value;
101+
}
102+
export function getPropsValue(
84103
ast: Expression,
85104
keyValue: string,
86105
script: string,
87106
offset: number,
88107
required = false,
89108
) {
90109
if (ast.type === "Identifier") {
91-
let value = "";
92-
switch (ast.value) {
93-
case "Function":
94-
case "Date": {
95-
value = ast.value;
96-
break;
97-
}
98-
case "Array": {
99-
value = "any[]";
100-
break;
101-
}
102-
default:
103-
value = ast.value.toLocaleLowerCase();
104-
break;
105-
}
106-
return `${keyValue}${required ? "" : "?"}: ${value}; `;
110+
return `${keyValue}${required ? "" : "?"}: ${getPropsValueIdentifier(
111+
ast.value,
112+
)}; `;
107113
}
108114

109115
if (ast.type === "TsAsExpression") {
@@ -114,9 +120,11 @@ export function getPropsValueIdentifier(
114120
}
115121

116122
if (ast.type === "ArrayExpression") {
117-
const { span } = ast;
118-
const { start, end } = getRealSpan(span, offset);
119-
return `${keyValue}${required ? "" : "?"}: ${script.slice(start, end)}; `;
123+
return `${keyValue}${required ? "" : "?"}: ${ast.elements
124+
.map((element) =>
125+
getPropsValueIdentifier((element!.expression as Identifier).value),
126+
)
127+
.join(" | ")}; `;
120128
}
121129
return "";
122130
}

0 commit comments

Comments
 (0)