Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit b4b3f38

Browse files
committed
Add some parserServices
1 parent f70a8e4 commit b4b3f38

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

lib/convert.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ module.exports = function convert(config) {
5555
let result = {
5656
type: "",
5757
range: [node.getStart(), node.end],
58-
loc: nodeUtils.getLoc(node, ast)
58+
loc: nodeUtils.getLoc(node, ast),
59+
[nodeUtils.originalNodeSymbol]: node
5960
};
6061

6162
/**

lib/node-utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ TOKEN_TO_TEXT[SyntaxKind.CaretEqualsToken] = "^=";
9898
TOKEN_TO_TEXT[SyntaxKind.AtToken] = "@";
9999
TOKEN_TO_TEXT[SyntaxKind.InKeyword] = "in";
100100

101+
const originalNodeSymbol = Symbol("Original TypeScript AST node");
102+
101103
/**
102104
* Find the first matching child based on the given sourceFile and predicate function.
103105
* @param {TSNode} node The current TSNode
@@ -155,6 +157,8 @@ module.exports = {
155157
* Expose the enum of possible TSNode `kind`s.
156158
*/
157159
SyntaxKind,
160+
originalNodeSymbol,
161+
getTSNode,
158162
isAssignmentOperator,
159163
isLogicalOperator,
160164
getTextForTokenKind,
@@ -189,6 +193,15 @@ module.exports = {
189193
};
190194
/* eslint-enable no-use-before-define */
191195

196+
/**
197+
* Get the TSNode associated with this ESTree node
198+
* @param {ASTNode} node the ESTree node
199+
* @returns {?TSNode} the TypeScript node
200+
*/
201+
function getTSNode(node) {
202+
return node[originalNodeSymbol];
203+
}
204+
192205
/**
193206
* Returns true if the given TSToken is the assignment operator
194207
* @param {TSToken} operator the operator token
@@ -550,7 +563,8 @@ function fixExports(node, result, ast) {
550563
type: declarationType,
551564
declaration: result,
552565
range: [exportKeyword.getStart(), result.range[1]],
553-
loc: getLocFor(exportKeyword.getStart(), result.range[1], ast)
566+
loc: getLocFor(exportKeyword.getStart(), result.range[1], ast),
567+
[originalNodeSymbol]: node
554568
};
555569

556570
if (!declarationIsDefault) {

parser.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
const astNodeTypes = require("./lib/ast-node-types"),
1212
ts = require("typescript"),
1313
convert = require("./lib/ast-converter"),
14-
semver = require("semver");
14+
semver = require("semver"),
15+
nodeUtils = require("./lib/node-utils");
1516

1617
const SUPPORTED_TYPESCRIPT_VERSIONS = require("./package.json").devDependencies.typescript;
1718
const ACTIVE_TYPESCRIPT_VERSION = ts.version;
@@ -44,12 +45,14 @@ function resetExtra() {
4445
// Parser
4546
//------------------------------------------------------------------------------
4647

48+
/** @typedef {{ ast: Program, program: ts.Program }} Result */
49+
4750
/**
4851
* Parses the given source code to produce a valid AST
4952
* @param {mixed} code TypeScript code
5053
* @param {Object} options configuration object for the parser
5154
* @param {Object} additionalParsingContext additional internal configuration
52-
* @returns {Object} the AST
55+
* @returns {Result} the result
5356
*/
5457
function generateAST(code, options, additionalParsingContext) {
5558
additionalParsingContext = additionalParsingContext || {};
@@ -176,7 +179,27 @@ function generateAST(code, options, additionalParsingContext) {
176179
const ast = program.getSourceFile(FILENAME);
177180

178181
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+
};
180203
}
181204

182205
//------------------------------------------------------------------------------
@@ -186,12 +209,15 @@ function generateAST(code, options, additionalParsingContext) {
186209
exports.version = require("./package.json").version;
187210

188211
exports.parse = function parse(code, options) {
189-
return generateAST(code, options, { isParseForESLint: false });
212+
return generateAST(code, options, { isParseForESLint: false }).ast;
190213
};
191214

192215
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+
};
195221
};
196222

197223
// Deep copy.

0 commit comments

Comments
 (0)