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 ( "." ) [ 0 ] ) ;
15+
1316const readFile = fs . promises . readFile ;
1417const 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}
109146async function check ( path ) {
110147 const code = await readFile ( path , { encoding : "utf8" } ) ;
0 commit comments