11
11
const astNodeTypes = require ( "./lib/ast-node-types" ) ,
12
12
ts = require ( "typescript" ) ,
13
13
convert = require ( "./lib/ast-converter" ) ,
14
- semver = require ( "semver" ) ;
14
+ semver = require ( "semver" ) ,
15
+ nodeUtils = require ( "./lib/node-utils" ) ;
15
16
16
17
const SUPPORTED_TYPESCRIPT_VERSIONS = require ( "./package.json" ) . devDependencies . typescript ;
17
18
const ACTIVE_TYPESCRIPT_VERSION = ts . version ;
@@ -44,12 +45,14 @@ function resetExtra() {
44
45
// Parser
45
46
//------------------------------------------------------------------------------
46
47
48
+ /** @typedef {{ ast: Program, program: ts.Program } } Result */
49
+
47
50
/**
48
51
* Parses the given source code to produce a valid AST
49
52
* @param {mixed } code TypeScript code
50
53
* @param {Object } options configuration object for the parser
51
54
* @param {Object } additionalParsingContext additional internal configuration
52
- * @returns {Object } the AST
55
+ * @returns {Result } the result
53
56
*/
54
57
function generateAST ( code , options , additionalParsingContext ) {
55
58
additionalParsingContext = additionalParsingContext || { } ;
@@ -176,7 +179,27 @@ function generateAST(code, options, additionalParsingContext) {
176
179
const ast = program . getSourceFile ( FILENAME ) ;
177
180
178
181
extra . code = code ;
179
- return convert ( ast , extra ) ;
182
+ return {
183
+ ast : convert ( ast , extra ) ,
184
+ program
185
+ } ;
186
+ }
187
+
188
+ /**
189
+ * Generate the `parserServices` object for a parse result
190
+ * @param {Result } result The return value of `generateAST`
191
+ * @returns {Object } The `parserServices` object
192
+ */
193
+ function getServices ( result ) {
194
+ const typeChecker = this . program . getTypeChecker ( ) ;
195
+ return {
196
+ program : result . program ,
197
+ getTSNode : nodeUtils . getTSNode ,
198
+ typeChecker
199
+ getType ( node ) {
200
+ return typeChecker . getTypeAtLocation ( nodeUtils . getTSNode ( node ) ) ;
201
+ }
202
+ } ;
180
203
}
181
204
182
205
//------------------------------------------------------------------------------
@@ -186,12 +209,15 @@ function generateAST(code, options, additionalParsingContext) {
186
209
exports . version = require ( "./package.json" ) . version ;
187
210
188
211
exports . parse = function parse ( code , options ) {
189
- return generateAST ( code , options , { isParseForESLint : false } ) ;
212
+ return generateAST ( code , options , { isParseForESLint : false } ) . ast ;
190
213
} ;
191
214
192
215
exports . parseForESLint = function parseForESLint ( code , options ) {
193
- const ast = generateAST ( code , options , { isParseForESLint : true } ) ;
194
- return { ast } ;
216
+ const result = generateAST ( code , options , { isParseForESLint : true } ) ;
217
+ return {
218
+ ast : result . ast ,
219
+ services : getServices ( result )
220
+ } ;
195
221
} ;
196
222
197
223
// Deep copy.
0 commit comments