Skip to content

Commit 446e1ae

Browse files
committed
refactor: code clean up
1 parent 2821479 commit 446e1ae

File tree

4 files changed

+7
-87
lines changed

4 files changed

+7
-87
lines changed

src/plugins/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { convertInject } from "./vue-property-decorator/Inject";
2222
import { convertProvide } from "./vue-property-decorator/Provide";
2323
import { convertTemplateRef } from "./vue-class-component/TemplateRef";
2424
import { TsHelper } from "../helpers/TsHelper";
25-
import { option } from "commander";
2625

2726
export function getDefaultPlugins(tsModule: typeof ts): ASTConvertPlugins {
2827
return {
@@ -99,7 +98,7 @@ export function getASTResults(
9998
}
10099
}
101100
if (!converted) {
102-
astResults.push(convertNodeToASTResult($t.module, property));
101+
astResults.push(convertNodeToASTResult($t, property));
103102
}
104103
}
105104
});

src/plugins/vue-class-component/TemplateRef.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ASTConverter, ASTResultKind, ReferenceKind } from "../types";
22
import ts from "typescript";
3-
import { UncouthOptions } from "../../options";
43
import { TsHelper } from "../../helpers/TsHelper";
54

65
export const convertTemplateRef: ASTConverter<ts.PropertyDeclaration> = (node, options) => {

src/plugins/vue-property-decorator/Inject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const convertInject: ASTConverter<ts.PropertyDeclaration> = (node, option
3838

3939
const injectArgs = [injectKeyExpr, ...(defaultValueExpr ? [defaultValueExpr] : [])];
4040
const injectType = node.type
41-
? [$t.factory.createKeywordTypeNode(node.type.kind as any)]
41+
? [$t.factory.createKeywordTypeNode(node.type.kind as ts.KeywordTypeSyntaxKind)]
4242
: undefined;
4343
const injectExpression = $t.createCallExpression("inject", injectType, injectArgs);
4444
const injectConstStatement = $t.createConstStatement(node.name.getText(), injectExpression);

src/utils.ts

Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type vueTemplateParser from "vue-template-compiler";
22
import type ts from "typescript";
33
import { ASTResult, ASTResultKind, ReferenceKind } from "./plugins/types";
4+
import { TsHelper } from "./helpers/TsHelper";
5+
46
export function isVueFile(path: string): boolean {
57
return path.endsWith(".vue");
68
}
@@ -85,80 +87,8 @@ export function isPrimitiveType(tsModule: typeof ts, returnType: ts.Type): boole
8587
);
8688
}
8789

88-
export function copySyntheticComments<T extends ts.Node>(
89-
tsModule: typeof ts,
90-
node: T,
91-
copyNode: ts.Node
92-
): T {
93-
const leadingComments =
94-
tsModule.getLeadingCommentRanges(copyNode.getSourceFile().getFullText(), copyNode.pos) || [];
95-
const trailingComments =
96-
tsModule.getTrailingCommentRanges(copyNode.getSourceFile().getFullText(), copyNode.end) || [];
97-
98-
const getCommentText = (comment: ts.CommentRange) => {
99-
return copyNode
100-
.getSourceFile()
101-
.getFullText()
102-
.slice(comment.pos, comment.end)
103-
.replace(/\/\//g, "")
104-
.replace(/\/\*/g, "")
105-
.replace(/\*\//g, "")
106-
.replace(/ {2}\* ?/g, "* ")
107-
.replace(/ \*\//g, "*/")
108-
.replace(/ {2}$/g, "");
109-
};
110-
111-
let result = node;
112-
for (const comment of leadingComments) {
113-
const text = getCommentText(comment);
114-
result = tsModule.addSyntheticLeadingComment(
115-
result,
116-
comment.kind,
117-
text,
118-
comment.hasTrailingNewLine
119-
);
120-
}
121-
122-
for (const comment of trailingComments) {
123-
const text = getCommentText(comment);
124-
result = tsModule.addSyntheticTrailingComment(
125-
result,
126-
comment.kind,
127-
text,
128-
comment.hasTrailingNewLine
129-
);
130-
}
131-
132-
return node;
133-
}
134-
135-
export function removeComments<T extends ts.Node>(
136-
tsModule: typeof ts,
137-
node: T
138-
): T | ts.StringLiteral {
139-
if (tsModule.isStringLiteral(node)) {
140-
return tsModule.createStringLiteral(node.text);
141-
}
142-
return node;
143-
}
144-
145-
export function addTodoComment<T extends ts.Node>(
146-
tsModule: typeof ts,
147-
node: T,
148-
text: string,
149-
multiline: boolean
150-
): T {
151-
return tsModule.addSyntheticLeadingComment(
152-
node,
153-
multiline
154-
? tsModule.SyntaxKind.MultiLineCommentTrivia
155-
: tsModule.SyntaxKind.SingleLineCommentTrivia,
156-
` TODO: ${text}`
157-
);
158-
}
159-
16090
export function convertNodeToASTResult<T extends ts.Node>(
161-
tsModule: typeof ts,
91+
tsHelper: TsHelper,
16292
node: T
16393
): ASTResult<T> {
16494
return {
@@ -167,18 +97,10 @@ export function convertNodeToASTResult<T extends ts.Node>(
16797
reference: ReferenceKind.NONE,
16898
attributes: [],
16999
tag: "IheritObjProperty",
170-
nodes: [addTodoComment(tsModule, node, "Can't convert this object property.", false)],
100+
nodes: [tsHelper.addTodoComment(node, "Can't convert this object property.", false)],
171101
};
172102
}
173103

174-
// ts.createIdentifier() cannot call getText function, it's a hack.
175-
export function createIdentifier(tsModule: typeof ts, text: string): ts.Identifier {
176-
const temp = tsModule.factory.createIdentifier(text);
177-
// eslint-disable-next-line @typescript-eslint/unbound-method
178-
temp.getText = () => text;
179-
return temp;
180-
}
181-
182-
export function isString(val: any): val is string {
104+
export function isString(val: unknown): val is string {
183105
return typeof val === "string";
184106
}

0 commit comments

Comments
 (0)