11#!/usr/bin/env node
22
33import assemblyscript from "assemblyscript" ;
4+ import asc from "assemblyscript/asc" ;
45import prettier from "prettier" ;
56import * as fs from "fs" ;
67import { Command } from "commander" ;
@@ -10,6 +11,8 @@ import chalk from "chalk";
1011import ignore from "ignore" ;
1112import { SingleBar } from "cli-progress" ;
1213
14+ const ascMajorVersion = parseInt ( asc . version . split ( "." ) [ 1 ] ) ;
15+
1316const readFile = fs . promises . readFile ;
1417const writeFile = fs . promises . writeFile ;
1518
@@ -27,39 +30,72 @@ function preProcess(code) {
2730 let source = program . sources [ 0 ] ;
2831
2932 let NodeKind = assemblyscript . NodeKind ;
30-
3133 function visitDecorators ( node ) {
3234 let list = [ ] ;
3335 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 ;
36+ if ( ascMajorVersion < 22 ) {
37+ switch ( _node . kind ) {
38+ case NodeKind . SOURCE : {
39+ _node . statements . forEach ( ( statement ) => {
40+ _visit ( statement ) ;
41+ } ) ;
42+ break ;
43+ }
44+ case NodeKind . CLASSDECLARATION :
45+ case NodeKind . INTERFACEDECLARATION :
46+ case NodeKind . NAMESPACEDECLARATION : {
47+ _node . members . forEach ( ( statement ) => {
48+ _visit ( statement ) ;
49+ } ) ;
50+ break ;
51+ }
52+ case NodeKind . ENUMDECLARATION :
53+ case NodeKind . METHODDECLARATION :
54+ case NodeKind . FUNCTIONDECLARATION : {
55+ if ( _node . decorators ) {
56+ list . push (
57+ ..._node . decorators . map ( ( decorator ) => {
58+ return {
59+ start : decorator . range . start ,
60+ end : decorator . range . end ,
61+ } ;
62+ } )
63+ ) ;
64+ }
65+ break ;
66+ }
4867 }
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- ) ;
68+ } else {
69+ switch ( _node . kind ) {
70+ case NodeKind . Source : {
71+ _node . statements . forEach ( ( statement ) => {
72+ _visit ( statement ) ;
73+ } ) ;
74+ break ;
75+ }
76+ case NodeKind . ClassDeclaration :
77+ case NodeKind . InterfaceDeclaration :
78+ case NodeKind . NamespaceDeclaration : {
79+ _node . members . forEach ( ( statement ) => {
80+ _visit ( statement ) ;
81+ } ) ;
82+ break ;
83+ }
84+ case NodeKind . EnumDeclaration :
85+ case NodeKind . MethodDeclaration :
86+ case NodeKind . FunctionDeclaration : {
87+ if ( _node . decorators ) {
88+ list . push (
89+ ..._node . decorators . map ( ( decorator ) => {
90+ return {
91+ start : decorator . range . start ,
92+ end : decorator . range . end ,
93+ } ;
94+ } )
95+ ) ;
96+ }
97+ break ;
6198 }
62- break ;
6399 }
64100 }
65101 } ;
@@ -103,8 +139,8 @@ async function format(path) {
103139 const tsCode = preProcess ( code ) ;
104140 let config = await resolveConfig ( path ) ;
105141 const formatTsCode = prettier . format ( tsCode , config ) ;
106- const foramtCode = postProcess ( formatTsCode ) ;
107- return foramtCode ;
142+ const formatCode = postProcess ( formatTsCode ) ;
143+ return formatCode ;
108144}
109145async function check ( path ) {
110146 const code = await readFile ( path , { encoding : "utf8" } ) ;
0 commit comments