Skip to content

refactor(ts-estree): add types to converter #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b3e8ac9
refactor(ts-estree): prototype of typed converter
armano2 Jan 28, 2019
98fa7e2
refactor: remove dead code
armano2 Jan 28, 2019
fbcfdcf
refactor: remove dead code
armano2 Jan 28, 2019
d10cb7f
refactor: add missing type checks
armano2 Jan 28, 2019
8b998a3
refactor: remove unnecessary type casings
armano2 Jan 28, 2019
123e66a
Merge branch 'master' into prototype-types-estree
armano2 Jan 29, 2019
87bc649
Merge branch 'master' into prototype-types-estree
armano2 Jan 29, 2019
a09514f
Merge branch 'master' into prototype-types-estree
armano2 Jan 30, 2019
ba413d8
Merge branch 'master' into prototype-types-estree
armano2 Jan 31, 2019
90c866e
Merge branch 'master' into prototype-types-estree
armano2 Feb 1, 2019
eed071c
chore(ts-estree): remove unneeded types from comments
armano2 Feb 1, 2019
97b1736
chore: add typedefs
armano2 Feb 1, 2019
567fbd7
chore: rename types and add check with AST_NODE_TYPES
armano2 Feb 1, 2019
ac1204b
chore: migrate types to typedefs
armano2 Feb 1, 2019
9514cce
chore: rename Position to LineAndColumnData
armano2 Feb 1, 2019
5016340
chore: enforce stricter type checking
armano2 Feb 1, 2019
11d0913
refactor(ts-estree): remove unnecessary assertions
armano2 Feb 1, 2019
c8d3601
chore: correct formatting after recent changes
armano2 Feb 2, 2019
8cced4b
refactor: restore ~original conversion of binary expression
armano2 Feb 2, 2019
9a411d3
refactor: add typing for convertTypeScriptJSXTagNameToESTreeName
armano2 Feb 2, 2019
92bc390
refactor: remove any cast in jsx tag name
armano2 Feb 2, 2019
6c5d3fa
chore: fix formatting
armano2 Feb 2, 2019
7da46c1
Merge branch 'master' into prototype-types-estree
armano2 Feb 2, 2019
147efe8
Merge branch 'master' into prototype-types-estree
JamesHenry Feb 3, 2019
d0b039a
chore(ts-estree): update accessibility and TODOs
armano2 Feb 3, 2019
ecf197c
Merge branch 'master' into prototype-types-estree
armano2 Feb 3, 2019
afe1dab
Merge branch 'master' into prototype-types-estree
armano2 Feb 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions packages/typescript-estree/src/ast-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/
import convert, { getASTMaps, resetASTMaps, convertError } from './convert';
import { convertError, Converter } from './convert';
import { convertComments } from './convert-comments';
import { convertTokens } from './node-utils';
import ts from 'typescript';
import { Extra } from './temp-types-based-on-js-source';
import { Extra } from './parser-options';

export default function astConverter(
ast: ts.SourceFile,
Expand All @@ -27,17 +27,14 @@ export default function astConverter(
/**
* Recursively convert the TypeScript AST into an ESTree-compatible AST
*/
const estree: any = convert({
node: ast,
parent: null,
ast,
additionalOptions: {
errorOnUnknownASTType: extra.errorOnUnknownASTType || false,
useJSXTextNode: extra.useJSXTextNode || false,
shouldProvideParserServices
}
const instance = new Converter(ast, {
errorOnUnknownASTType: extra.errorOnUnknownASTType || false,
useJSXTextNode: extra.useJSXTextNode || false,
shouldProvideParserServices
});

const estree = instance.convertProgram();

/**
* Optionally convert and include all tokens in the AST
*/
Expand All @@ -52,11 +49,9 @@ export default function astConverter(
estree.comments = convertComments(ast, extra.code);
}

let astMaps = undefined;
if (shouldProvideParserServices) {
astMaps = getASTMaps();
resetASTMaps();
}
const astMaps = shouldProvideParserServices
? instance.getASTMaps()
: undefined;

return { estree, astMaps };
}
55 changes: 25 additions & 30 deletions packages/typescript-estree/src/convert-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,28 @@

import ts from 'typescript';
import { getLocFor, getNodeContainer } from './node-utils';
import {
ESTreeComment,
LineAndColumnData
} from './temp-types-based-on-js-source';
import * as es from './typedefs';

/**
* Converts a TypeScript comment to an Esprima comment.
* @param {boolean} block True if it's a block comment, false if not.
* @param {string} text The text of the comment.
* @param {number} start The index at which the comment starts.
* @param {number} end The index at which the comment ends.
* @param {LineAndColumnData} startLoc The location at which the comment starts.
* @param {LineAndColumnData} endLoc The location at which the comment ends.
* @returns {Object} The comment object.
* @private
* @param block True if it's a block comment, false if not.
* @param text The text of the comment.
* @param start The index at which the comment starts.
* @param end The index at which the comment ends.
* @param startLoc The location at which the comment starts.
* @param endLoc The location at which the comment ends.
* @returns The comment object.
* @internal
*/
function convertTypeScriptCommentToEsprimaComment(
block: boolean,
text: string,
start: number,
end: number,
startLoc: LineAndColumnData,
endLoc: LineAndColumnData
): ESTreeComment {
const comment: ESTreeComment = {
startLoc: es.LineAndColumnData,
endLoc: es.LineAndColumnData
): es.Comment {
const comment: es.OptionalRangeAndLoc<es.Comment> = {
type: block ? 'Block' : 'Line',
value: text
};
Expand All @@ -47,22 +44,22 @@ function convertTypeScriptCommentToEsprimaComment(
};
}

return comment;
return comment as es.Comment;
}

/**
* Convert comment from TypeScript Triva Scanner.
* @param {ts.Scanner} triviaScanner TS Scanner
* @param {ts.SourceFile} ast the AST object
* @param {string} code TypeScript code
* @returns {ESTreeComment} the converted ESTreeComment
* @param triviaScanner TS Scanner
* @param ast the AST object
* @param code TypeScript code
* @returns the converted Comment
* @private
*/
function getCommentFromTriviaScanner(
triviaScanner: ts.Scanner,
ast: ts.SourceFile,
code: string
): ESTreeComment {
): es.Comment {
const kind = triviaScanner.getToken();
const isBlock = kind === ts.SyntaxKind.MultiLineCommentTrivia;
const range = {
Expand All @@ -77,30 +74,28 @@ function getCommentFromTriviaScanner(
: comment.replace(/^\/\//, '');
const loc = getLocFor(range.pos, range.end, ast);

const esprimaComment = convertTypeScriptCommentToEsprimaComment(
return convertTypeScriptCommentToEsprimaComment(
isBlock,
text,
range.pos,
range.end,
loc.start,
loc.end
);

return esprimaComment;
}

/**
* Convert all comments for the given AST.
* @param {ts.SourceFile} ast the AST object
* @param {string} code the TypeScript code
* @returns {ESTreeComment[]} the converted ESTreeComment
* @param ast the AST object
* @param code the TypeScript code
* @returns the converted ESTreeComment
* @private
*/
export function convertComments(
ast: ts.SourceFile,
code: string
): ESTreeComment[] {
const comments: ESTreeComment[] = [];
): es.Comment[] {
const comments: es.Comment[] = [];

/**
* Create a TypeScript Scanner, with skipTrivia set to false so that
Expand Down
Loading