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

Commit

Permalink
feat: offer semantic services alongside AST (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueiniquity authored and JamesHenry committed Nov 22, 2018
1 parent 383616e commit 8787f16
Show file tree
Hide file tree
Showing 16 changed files with 1,849 additions and 121 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@types/jest": "^23.3.9",
"@types/lodash.isplainobject": "^4.0.4",
"@types/lodash.unescape": "^4.0.4",
"@types/node": "^10.12.2",
"@types/semver": "^5.5.0",
"@types/shelljs": "^0.8.0",
"babel-code-frame": "6.26.0",
Expand Down
24 changes: 18 additions & 6 deletions src/ast-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/
import { convert } from './convert';
import convert, { getASTMaps, resetASTMaps } from './convert';
import { convertComments } from './convert-comments';
import nodeUtils from './node-utils';
import ts from 'typescript';
import { Extra } from './temp-types-based-on-js-source';

/**
Expand All @@ -23,13 +24,17 @@ function convertError(error: any) {
);
}

export default (ast: any, extra: Extra) => {
export default (
ast: ts.SourceFile,
extra: Extra,
shouldProvideParserServices: boolean
) => {
/**
* The TypeScript compiler produced fundamental parse errors when parsing the
* source.
*/
if (ast.parseDiagnostics.length) {
throw convertError(ast.parseDiagnostics[0]);
if ((ast as any).parseDiagnostics.length) {
throw convertError((ast as any).parseDiagnostics[0]);
}

/**
Expand All @@ -41,7 +46,8 @@ export default (ast: any, extra: Extra) => {
ast,
additionalOptions: {
errorOnUnknownASTType: extra.errorOnUnknownASTType || false,
useJSXTextNode: extra.useJSXTextNode || false
useJSXTextNode: extra.useJSXTextNode || false,
shouldProvideParserServices
}
});

Expand All @@ -59,5 +65,11 @@ export default (ast: any, extra: Extra) => {
estree.comments = convertComments(ast, extra.code);
}

return estree;
let astMaps = undefined;
if (shouldProvideParserServices) {
astMaps = getASTMaps();
resetASTMaps();
}

return { estree, astMaps };
};
Loading

0 comments on commit 8787f16

Please sign in to comment.