Skip to content

Commit 0b49ab4

Browse files
committed
refactor: simplify AST node type conversion logic
1 parent 9ae4c8a commit 0b49ab4

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

src/typstToTextlintAst.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,6 @@ export const convertRawTypstAstStringToObject = (rawTypstAstString: string) => {
6969
return parsed.ast as TypstAstNode;
7070
};
7171

72-
/**
73-
* Convert a Typst AST node type to a textlint AST node type.
74-
* @param typstAstNodeType The Typst AST node type.
75-
* @returns The textlint AST node type.
76-
**/
77-
export const convertTypstAstNodeTypeToTextlintNodeType = (
78-
typstAstNodeType: string,
79-
): string => {
80-
const nodeTypeMap = new Map<RegExp, string>([
81-
[/^Marked::Heading$/, ASTNodeTypes.Header],
82-
[/^Marked::Text/, ASTNodeTypes.Str],
83-
[/^Marked::Parbreak/, ASTNodeTypes.Break],
84-
[/^Escape::Linebreak/, ASTNodeTypes.Break],
85-
]);
86-
87-
for (const [pattern, nodeType] of nodeTypeMap) {
88-
if (pattern.test(typstAstNodeType)) {
89-
return nodeType;
90-
}
91-
}
92-
93-
return typstAstNodeType;
94-
};
95-
9672
interface TypstAstNode {
9773
s: string;
9874
c?: TypstAstNode[];
@@ -296,13 +272,22 @@ export const convertRawTypstAstObjectToTextlintAstObject = (
296272

297273
const endOffset = currentOffset + nodeLength;
298274

299-
node.type = convertTypstAstNodeTypeToTextlintNodeType(
300-
extractNodeType(node.s),
301-
);
302275
node.raw = extractRawSourceByLocation(typstSource, location);
303276
node.range = [startOffset, endOffset];
304277
node.loc = location;
305-
278+
node.type = extractNodeType(node.s);
279+
if (/^Marked::Heading$/.test(node.type)) {
280+
node.type = ASTNodeTypes.Header;
281+
}
282+
if (/^Marked::Text/.test(node.type)) {
283+
node.type = ASTNodeTypes.Str;
284+
}
285+
if (/^Marked::Parbreak/.test(node.type)) {
286+
node.type = ASTNodeTypes.Break;
287+
}
288+
if (/^Escape::Linebreak/.test(node.type)) {
289+
node.type = ASTNodeTypes.Break;
290+
}
306291
if (node.type === "Marked::Raw") {
307292
if (node.loc.start.line === node.loc.end.line) {
308293
// If Code

0 commit comments

Comments
 (0)