Skip to content

fix: match assemblyscript v22 enum identifiers to TitleCase #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 66 additions & 30 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import assemblyscript from "assemblyscript";
import asc from "assemblyscript/asc";
import prettier from "prettier";
import * as fs from "fs";
import { Command } from "commander";
Expand All @@ -10,6 +11,8 @@ import chalk from "chalk";
import ignore from "ignore";
import { SingleBar } from "cli-progress";

const ascMajorVersion = parseInt(asc.version.split(".")[1]);

const readFile = fs.promises.readFile;
const writeFile = fs.promises.writeFile;

Expand All @@ -27,39 +30,72 @@ function preProcess(code) {
let source = program.sources[0];

let NodeKind = assemblyscript.NodeKind;

function visitDecorators(node) {
let list = [];
let _visit = (_node) => {
switch (_node.kind) {
case NodeKind.SOURCE: {
_node.statements.forEach((statement) => {
_visit(statement);
});
break;
}
case NodeKind.CLASSDECLARATION:
case NodeKind.INTERFACEDECLARATION:
case NodeKind.NAMESPACEDECLARATION: {
_node.members.forEach((statement) => {
_visit(statement);
});
break;
if (ascMajorVersion < 22) {
switch (_node.kind) {
case NodeKind.SOURCE: {
_node.statements.forEach((statement) => {
_visit(statement);
});
break;
}
case NodeKind.CLASSDECLARATION:
case NodeKind.INTERFACEDECLARATION:
case NodeKind.NAMESPACEDECLARATION: {
_node.members.forEach((statement) => {
_visit(statement);
});
break;
}
case NodeKind.ENUMDECLARATION:
case NodeKind.METHODDECLARATION:
case NodeKind.FUNCTIONDECLARATION: {
if (_node.decorators) {
list.push(
..._node.decorators.map((decorator) => {
return {
start: decorator.range.start,
end: decorator.range.end,
};
})
);
}
break;
}
}
case NodeKind.ENUMDECLARATION:
case NodeKind.METHODDECLARATION:
case NodeKind.FUNCTIONDECLARATION: {
if (_node.decorators) {
list.push(
..._node.decorators.map((decorator) => {
return {
start: decorator.range.start,
end: decorator.range.end,
};
})
);
} else {
switch (_node.kind) {
case NodeKind.Source: {
_node.statements.forEach((statement) => {
_visit(statement);
});
break;
}
case NodeKind.ClassDeclaration:
case NodeKind.InterfaceDeclaration:
case NodeKind.NamespaceDeclaration: {
_node.members.forEach((statement) => {
_visit(statement);
});
break;
}
case NodeKind.EnumDeclaration:
case NodeKind.MethodDeclaration:
case NodeKind.FunctionDeclaration: {
if (_node.decorators) {
list.push(
..._node.decorators.map((decorator) => {
return {
start: decorator.range.start,
end: decorator.range.end,
};
})
);
}
break;
}
break;
}
}
};
Expand Down Expand Up @@ -103,8 +139,8 @@ async function format(path) {
const tsCode = preProcess(code);
let config = await resolveConfig(path);
const formatTsCode = prettier.format(tsCode, config);
const foramtCode = postProcess(formatTsCode);
return foramtCode;
const formatCode = postProcess(formatTsCode);
return formatCode;
}
async function check(path) {
const code = await readFile(path, { encoding: "utf8" });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"url": "https://github.com/HerrCai0907/assemblyscript-prettier/issues"
},
"peerDependencies": {
"assemblyscript": "^0.21.0",
"assemblyscript": ">=0.20.0",
"prettier": "^2.7.1"
},
"dependencies": {
Expand Down
6 changes: 4 additions & 2 deletions tests/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execSync } from "child_process";
import { fileURLToPath } from "url";
import { dirname, relative, resolve } from "path";
import { copyFileSync, mkdirSync } from "fs";
import { copyFileSync, existsSync, mkdirSync, statSync } from "fs";
import chalk from "chalk";
import { argv, cwd } from "process";
const __filename = fileURLToPath(import.meta.url);
Expand All @@ -15,7 +15,9 @@ const targetFile = relative(cwd(), resolve(__dirname, "example.ts"));
const as_version = argv[2] ?? 20;

console.log(chalk.green(`start test prettier for as v0.${as_version}.x`));
mkdirSync(testFolder);
if (!existsSync(testFolder)) {
mkdirSync(testFolder);
}
execSync(`cd ${testFolder} && npm init -y && npm i [email protected]`);
execSync(`cd ${testFolder} && npm i assemblyscript@0.${as_version}`);
console.log(chalk.green("init node_modules done"));
Expand Down