@@ -8,6 +8,7 @@ import {Stats} from 'fs';
88const parserOption = {
99 ecmaVersion : 'latest' ,
1010 sourceType : 'module' ,
11+ parser : '@typescript-eslint/parser' ,
1112} ;
1213
1314export class VueComponent {
@@ -27,30 +28,44 @@ export class VueComponent {
2728
2829 private _importDeclaration : ESLintImportDeclaration [ ] = [ ] ;
2930
31+ private _srcAttribute : string ;
32+
3033 constructor ( filename : string , contents : string , stats ?: Stats ) {
3134 this . _filename = filename ;
3235 this . _lastModifiedTime = stats ?. mtimeMs || 0 ;
3336 this . _size = stats ?. size || 0 ;
3437
3538 // get each part from text of file.
3639 const templateBody = contents . match ( / (?< template > < t e m p l a t e > [ \s \S ] * < \/ t e m p l a t e > ) / u) ;
37- const scriptBody = contents . match ( / (?< script > < s c r i p t > [ \s \S ] * < \/ s c r i p t > ) / u) ;
40+
41+ // support src, lang, setup attribute.
42+ const scriptBody = contents . match ( / (?< script > < s c r i p t [ \s \S ] * > [ \s \S ] * < \/ s c r i p t > ) / u) ;
3843 const styleBody = contents . match ( / (?< style > < s t y l e > [ \s \S ] * < \/ s t y l e > ) / u) ;
3944
4045 this . _template = templateBody ?. groups ?. template || '' ;
41- this . _style = styleBody ?. groups ?. template || '' ;
46+ this . _style = styleBody ?. groups ?. style || '' ;
4247
4348 const scriptString = scriptBody ?. groups ?. script || '' ;
4449
45- // using vue-eslint-parser package.
46- const esLintProgram : ESLintProgram = parse ( scriptString , parserOption ) ;
50+ try {
51+ // using vue-eslint-parser package.
52+ const esLintProgram : ESLintProgram = parse ( scriptString , parserOption ) ;
53+
54+ // get props from parser results.
55+ if ( esLintProgram . tokens ) {
56+ this . _props = this . getProps ( esLintProgram . tokens ) ;
57+ }
58+
59+ this . _importDeclaration = getImportDeclaration ( esLintProgram . body ) ;
60+ } catch ( err : unknown ) {
61+ console . error ( 'something went wrong at pars.' ) ;
4762
48- // get props from parser results.
49- if ( esLintProgram . tokens ) {
50- this . _props = this . getProps ( esLintProgram . tokens ) ;
63+ throw err ;
5164 }
5265
53- this . _importDeclaration = getImportDeclaration ( esLintProgram . body ) ;
66+ const scriptSrc = scriptString . match ( / < s c r i p t s r c = " (?< src > [ \s \S ] * ) " > / u) ;
67+
68+ this . _srcAttribute = scriptSrc ?. groups ?. src || '' ;
5469 }
5570
5671 private getProps ( tokens : Token [ ] ) : string {
@@ -61,10 +76,22 @@ export class VueComponent {
6176 return propsDeclaration . props ;
6277 }
6378
79+ const definePropsDeclaration = getDeclarationSyntax ( tokens , 'defineProps' ) ;
80+
81+ const definePropsDeclarationJSON = JSON . parse ( definePropsDeclaration ) ;
82+
83+ if ( definePropsDeclarationJSON && definePropsDeclarationJSON . defineProps ) {
84+ return definePropsDeclarationJSON . defineProps ;
85+ }
86+
6487 return '' ;
6588 } catch ( err ) {
6689 console . warn ( 'failed to analyze props.' ) ;
6790
91+ if ( err instanceof Error ) {
92+ console . error ( err . message ) ;
93+ }
94+
6895 return '' ;
6996 }
7097 }
@@ -77,6 +104,10 @@ export class VueComponent {
77104 return this . _importDeclaration ;
78105 }
79106
107+ get srcAttribute ( ) : string {
108+ return this . _srcAttribute ;
109+ }
110+
80111 public getFileReport ( isTest : boolean ) : FileReport {
81112 return {
82113 name : this . _filename ,
0 commit comments