@@ -82,19 +82,21 @@ function parseScriptNode(scriptNode, scriptParser, tokenGenerator) {
8282
8383 // Needs the tokens of start/end tags for `lines-around-*` rules to work
8484 // correctly.
85- const startTag = tokenGenerator . createToken (
86- "Punctuator" ,
87- startLoc . startOffset ,
88- startLoc . endOffset
89- )
90- const endTag = tokenGenerator . createToken (
91- "Punctuator" ,
92- endLoc . startOffset ,
93- endLoc . endOffset
94- )
95- ast . start = startTag . end
96- ast . tokens . unshift ( startTag )
97- ast . tokens . push ( endTag )
85+ if ( ast . tokens ) {
86+ const startTag = tokenGenerator . createToken (
87+ "Punctuator" ,
88+ startLoc . startOffset ,
89+ startLoc . endOffset
90+ )
91+ const endTag = tokenGenerator . createToken (
92+ "Punctuator" ,
93+ endLoc . startOffset ,
94+ endLoc . endOffset
95+ )
96+ ast . start = startTag . end
97+ ast . tokens . unshift ( startTag )
98+ ast . tokens . push ( endTag )
99+ }
98100
99101 return ast
100102}
@@ -137,33 +139,37 @@ function parse(code, options) {
137139 const templateOptions = options . template || { }
138140 const isVue = path . extname ( filePath || "unknown.js" ) === ".vue"
139141 const scriptParser = new ScriptParser ( code , scriptOptions )
140-
141- if ( ! isVue ) {
142- return {
143- ast : scriptParser . _parseScript ( code ) ,
144- services : { registerTemplateBodyVisitor} ,
145- }
142+ let ast = null
143+
144+ if ( isVue ) {
145+ const tokenGenerator = new TokenGenerator ( code )
146+ const info = parseComponent ( code )
147+
148+ // <script>
149+ ast = ( info . script != null )
150+ ? parseScriptNode ( info . script , scriptParser , tokenGenerator )
151+ : scriptParser . _parseScript ( "" )
152+
153+ // <template>
154+ ast . templateBody = info . template && parseTemplateNode (
155+ info . template ,
156+ scriptParser ,
157+ tokenGenerator ,
158+ templateOptions
159+ )
146160 }
147- const info = parseComponent ( code )
148- if ( ! info . script ) {
149- return {
150- ast : scriptParser . _parseScript ( "" ) ,
151- services : { registerTemplateBodyVisitor} ,
152- }
161+ else {
162+ ast = scriptParser . _parseScript ( code )
163+ ast . templateBody = null
153164 }
154165
155- const tokenGenerator = new TokenGenerator ( code )
156- const ast = parseScriptNode (
157- info . script ,
158- scriptParser ,
159- tokenGenerator
160- )
161- ast . templateBody = info . template && parseTemplateNode (
162- info . template ,
163- scriptParser ,
164- tokenGenerator ,
165- templateOptions
166- )
166+ // Ensure ast.comments and ast.tokens.
167+ if ( ast . comments == null ) {
168+ ast . comments = [ ]
169+ }
170+ if ( ast . tokens == null ) {
171+ ast . tokens = [ ]
172+ }
167173
168174 return {
169175 ast,
0 commit comments