Skip to content

Commit 0c00db9

Browse files
committed
fix: match assemblyscript v22 enum identifiers to TitleCase
1 parent 6428983 commit 0c00db9

File tree

2 files changed

+67
-30
lines changed

2 files changed

+67
-30
lines changed

index.mjs

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
import assemblyscript from "assemblyscript";
4+
import asc from "assemblyscript/asc";
45
import prettier from "prettier";
56
import * as fs from "fs";
67
import { Command } from "commander";
@@ -10,6 +11,8 @@ import chalk from "chalk";
1011
import ignore from "ignore";
1112
import { SingleBar } from "cli-progress";
1213

14+
const ascMajorVersion = parseInt(asc.version.split(".")[0]);
15+
1316
const readFile = fs.promises.readFile;
1417
const writeFile = fs.promises.writeFile;
1518

@@ -31,35 +34,69 @@ function preProcess(code) {
3134
function visitDecorators(node) {
3235
let list = [];
3336
let _visit = (_node) => {
34-
switch (_node.kind) {
35-
case NodeKind.SOURCE: {
36-
_node.statements.forEach((statement) => {
37-
_visit(statement);
38-
});
39-
break;
40-
}
41-
case NodeKind.CLASSDECLARATION:
42-
case NodeKind.INTERFACEDECLARATION:
43-
case NodeKind.NAMESPACEDECLARATION: {
44-
_node.members.forEach((statement) => {
45-
_visit(statement);
46-
});
47-
break;
37+
if (ascMajorVersion < 22) {
38+
switch (_node.kind) {
39+
case NodeKind.SOURCE: {
40+
_node.statements.forEach((statement) => {
41+
_visit(statement);
42+
});
43+
break;
44+
}
45+
case NodeKind.CLASSDECLARATION:
46+
case NodeKind.INTERFACEDECLARATION:
47+
case NodeKind.NAMESPACEDECLARATION: {
48+
_node.members.forEach((statement) => {
49+
_visit(statement);
50+
});
51+
break;
52+
}
53+
case NodeKind.ENUMDECLARATION:
54+
case NodeKind.METHODDECLARATION:
55+
case NodeKind.FUNCTIONDECLARATION: {
56+
if (_node.decorators) {
57+
list.push(
58+
..._node.decorators.map((decorator) => {
59+
return {
60+
start: decorator.range.start,
61+
end: decorator.range.end,
62+
};
63+
})
64+
);
65+
}
66+
break;
67+
}
4868
}
49-
case NodeKind.ENUMDECLARATION:
50-
case NodeKind.METHODDECLARATION:
51-
case NodeKind.FUNCTIONDECLARATION: {
52-
if (_node.decorators) {
53-
list.push(
54-
..._node.decorators.map((decorator) => {
55-
return {
56-
start: decorator.range.start,
57-
end: decorator.range.end,
58-
};
59-
})
60-
);
69+
} else {
70+
switch (_node.kind) {
71+
case NodeKind.Source: {
72+
_node.statements.forEach((statement) => {
73+
_visit(statement);
74+
});
75+
break;
76+
}
77+
case NodeKind.ClassDeclaration:
78+
case NodeKind.InterfaceDeclaration:
79+
case NodeKind.NamespaceDeclaration: {
80+
_node.members.forEach((statement) => {
81+
_visit(statement);
82+
});
83+
break;
84+
}
85+
case NodeKind.EnumDeclaration:
86+
case NodeKind.MethodDeclaration:
87+
case NodeKind.FunctionDeclaration: {
88+
if (_node.decorators) {
89+
list.push(
90+
..._node.decorators.map((decorator) => {
91+
return {
92+
start: decorator.range.start,
93+
end: decorator.range.end,
94+
};
95+
})
96+
);
97+
}
98+
break;
6199
}
62-
break;
63100
}
64101
}
65102
};
@@ -103,8 +140,8 @@ async function format(path) {
103140
const tsCode = preProcess(code);
104141
let config = await resolveConfig(path);
105142
const formatTsCode = prettier.format(tsCode, config);
106-
const foramtCode = postProcess(formatTsCode);
107-
return foramtCode;
143+
const formatCode = postProcess(formatTsCode);
144+
return formatCode;
108145
}
109146
async function check(path) {
110147
const code = await readFile(path, { encoding: "utf8" });

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"url": "https://github.com/HerrCai0907/assemblyscript-prettier/issues"
2929
},
3030
"peerDependencies": {
31-
"assemblyscript": "^0.21.0",
31+
"assemblyscript": ">=0.20.0",
3232
"prettier": "^2.7.1"
3333
},
3434
"dependencies": {

0 commit comments

Comments
 (0)