Skip to content

Commit d5e69af

Browse files
author
Benjamin Lichtman
committed
refactor: add comment and use util function
1 parent 4a857b5 commit d5e69af

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

lib/node-utils.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ module.exports = {
166166
isTypeKeyword,
167167
isComment,
168168
isJSDocComment,
169-
createError
169+
createError,
170+
firstDefined
170171
};
171172

172173
/**
@@ -405,6 +406,7 @@ function hasStaticModifierFlag(node) {
405406

406407
/**
407408
* Finds the next token based on the previous one and its parent
409+
* Had to copy this from TS instead of using TS's version because theirs doesn't pass the ast to getChildren
408410
* @param {TSToken} previousToken The previous TSToken
409411
* @param {TSNode} parent The parent TSNode
410412
* @param {ts.SourceFileLike} ast The TS AST

parser.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const astNodeTypes = require('./lib/ast-node-types'),
1212
ts = require('typescript'),
1313
convert = require('./lib/ast-converter'),
1414
semver = require('semver'),
15-
calculateProjectParserOptions = require('./lib/tsconfig-parser');
15+
calculateProjectParserOptions = require('./lib/tsconfig-parser'),
16+
util = require('./lib/node-utils');
1617

1718
const SUPPORTED_TYPESCRIPT_VERSIONS = require('./package.json').devDependencies
1819
.typescript;
@@ -52,17 +53,13 @@ function resetExtra() {
5253
* @returns {{ast: ts.SourceFile, program: ts.Program} | undefined} If found, returns the source file corresponding to the code and the containing program
5354
*/
5455
function getASTFromProject(code, options) {
55-
for (const program of calculateProjectParserOptions(
56-
code,
57-
options,
58-
extra.project
59-
)) {
60-
const ast = program.getSourceFile(options.filePath);
61-
62-
if (ast) {
63-
return { ast, program };
56+
return util.firstDefined(
57+
calculateProjectParserOptions(code, options, extra.project),
58+
currentProgram => {
59+
const ast = currentProgram.getSourceFile(options.filePath);
60+
return ast && { ast, program: currentProgram };
6461
}
65-
}
62+
);
6663
}
6764

6865
/**

0 commit comments

Comments
 (0)