1212const fs = require ( "fs" )
1313const path = require ( "path" )
1414const parser = require ( "../src" )
15- const escope = require ( "eslint-scope" )
1615const semver = require ( "semver" )
16+ const {
17+ scopeToJSON,
18+ analyze,
19+ replacer,
20+ getAllTokens,
21+ } = require ( "../test/test-utils" )
1722
1823//------------------------------------------------------------------------------
1924// Helpers
@@ -30,40 +35,6 @@ const PARSER_OPTIONS = {
3035 eslintScopeManager : true ,
3136}
3237
33- /**
34- * Remove `parent` proeprties from the given AST.
35- * @param {string } key The key.
36- * @param {any } value The value of the key.
37- * @returns {any } The value of the key to output.
38- */
39- function replacer ( key , value ) {
40- if ( key === "parent" ) {
41- return undefined
42- }
43- if ( key === "errors" && Array . isArray ( value ) ) {
44- return value . map ( ( e ) => ( {
45- message : e . message ,
46- index : e . index ,
47- lineNumber : e . lineNumber ,
48- column : e . column ,
49- } ) )
50- }
51- return value
52- }
53-
54- /**
55- * Get all tokens of the given AST.
56- * @param {ASTNode } ast The root node of AST.
57- * @returns {Token[] } Tokens.
58- */
59- function getAllTokens ( ast ) {
60- const tokenArrays = [ ast . tokens , ast . comments ]
61- if ( ast . templateBody != null ) {
62- tokenArrays . push ( ast . templateBody . tokens , ast . templateBody . comments )
63- }
64- return Array . prototype . concat . apply ( [ ] , tokenArrays )
65- }
66-
6738/**
6839 * Create simple tree.
6940 * @param {string } source The source code.
@@ -98,109 +69,6 @@ function getTree(source, ast) {
9869 return root . children
9970}
10071
101- function scopeToJSON ( scopeManager ) {
102- return JSON . stringify ( normalizeScope ( scopeManager . globalScope ) , replacer , 4 )
103-
104- function normalizeScope ( scope ) {
105- return {
106- type : scope . type ,
107- variables : scope . variables . map ( normalizeVar ) ,
108- references : scope . references . map ( normalizeReference ) ,
109- childScopes : scope . childScopes . map ( normalizeScope ) ,
110- through : scope . through . map ( normalizeReference ) ,
111- }
112- }
113-
114- function normalizeVar ( v ) {
115- return {
116- name : v . name ,
117- identifiers : v . identifiers . map ( normalizeId ) ,
118- defs : v . defs . map ( normalizeDef ) ,
119- references : v . references . map ( normalizeReference ) ,
120- }
121- }
122-
123- function normalizeReference ( reference ) {
124- return {
125- identifier : normalizeId ( reference . identifier ) ,
126- from : reference . from . type ,
127- resolved : normalizeId (
128- reference . resolved &&
129- reference . resolved . defs &&
130- reference . resolved . defs [ 0 ] &&
131- reference . resolved . defs [ 0 ] . name ,
132- ) ,
133- init : reference . init || null ,
134- vueUsedInTemplate : reference . vueUsedInTemplate
135- ? reference . vueUsedInTemplate
136- : undefined ,
137- }
138- }
139-
140- function normalizeDef ( def ) {
141- return {
142- type : def . type ,
143- node : normalizeDefNode ( def . node ) ,
144- name : def . name . name ,
145- }
146- }
147-
148- function normalizeId ( identifier ) {
149- return (
150- identifier && {
151- type : identifier . type ,
152- name : identifier . name ,
153- loc : identifier . loc ,
154- }
155- )
156- }
157-
158- function normalizeDefNode ( node ) {
159- return {
160- type : node . type ,
161- loc : node . loc ,
162- }
163- }
164- }
165-
166- /**
167- * Analyze scope
168- */
169- function analyze ( ast , parserOptions ) {
170- const ecmaVersion = parserOptions . ecmaVersion || 2017
171- const ecmaFeatures = parserOptions . ecmaFeatures || { }
172- const sourceType = parserOptions . sourceType || "script"
173- const result = escope . analyze ( ast , {
174- ignoreEval : true ,
175- nodejsScope : false ,
176- impliedStrict : ecmaFeatures . impliedStrict ,
177- ecmaVersion,
178- sourceType,
179- fallback : getFallbackKeys ,
180- } )
181-
182- return result
183-
184- function getFallbackKeys ( node ) {
185- return Object . keys ( node ) . filter ( fallbackKeysFilter , node )
186- }
187-
188- function fallbackKeysFilter ( key ) {
189- const value = null
190- return (
191- key !== "comments" &&
192- key !== "leadingComments" &&
193- key !== "loc" &&
194- key !== "parent" &&
195- key !== "range" &&
196- key !== "tokens" &&
197- key !== "trailingComments" &&
198- typeof value === "object" &&
199- ( typeof value . type === "string" || Array . isArray ( value ) )
200- )
201- }
202- }
203-
20472//------------------------------------------------------------------------------
20573// Main
20674//------------------------------------------------------------------------------
0 commit comments