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

fix(*): brute force initial conversion to TS #25

Merged
merged 1 commit into from
Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules
npm-debug.log
_test.js
.DS_Store
.vscode
.vscode
dist
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ script:
- commitlint-travis
- yarn check-format
- yarn test
- yarn build
after_success:
- npm run travis-deploy-once "npm run semantic-release"
branches:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

## About

This parser is somewhat generic and robust, and could be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatiable AST.
This parser is somewhat generic and robust, and could be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatible AST.

In fact, it is already used within these hyper-popular open-source projects to power their TypeScript support:

Expand Down
29 changes: 24 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "typescript-estree",
"description": "A parser that converts TypeScript source code into an ESTree compatible form",
"homepage": "https://github.com/JamesHenry/typescript-estree",
"main": "parser.js",
"main": "dist/parser.js",
"version": "0.0.0-development",
"files": [
"lib",
"parser.js"
"dist",
"README.md",
"LICENSE"
],
"engines": {
"node": ">=6.14.0"
Expand All @@ -20,6 +21,12 @@
"@commitlint/cli": "^7.1.2",
"@commitlint/config-conventional": "^7.1.2",
"@commitlint/travis-cli": "^7.1.2",
"@types/babel-code-frame": "^6.20.1",
"@types/jest": "^23.3.9",
"@types/lodash.isplainobject": "^4.0.4",
"@types/lodash.unescape": "^4.0.4",
"@types/semver": "^5.5.0",
"@types/shelljs": "^0.8.0",
"babel-code-frame": "6.26.0",
"babylon": "7.0.0-beta.39",
"cz-conventional-changelog": "2.1.0",
Expand All @@ -32,6 +39,7 @@
"semantic-release": "^15.9.16",
"shelljs": "0.8.2",
"travis-deploy-once": "^5.0.8",
"ts-jest": "^23.10.4",
"typescript": "~3.1.1"
},
"keywords": [
Expand All @@ -44,6 +52,7 @@
"syntax"
],
"scripts": {
"build": "tsc",
"test": "npm run unit-tests && npm run ast-alignment-tests",
"unit-tests": "jest",
"ast-alignment-tests": "jest --config=./tests/ast-alignment/jest.config.js",
Expand Down Expand Up @@ -79,8 +88,18 @@
},
"jest": {
"testEnvironment": "node",
"testRegex": "tests/lib/.+\\.js$",
"testPathIgnorePatterns": [],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/tests/lib/.*)\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"collectCoverage": true,
"coverageReporters": [
"text-summary"
Expand Down
28 changes: 7 additions & 21 deletions lib/ast-converter.js → src/ast-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,25 @@
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/

'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const convert = require('./convert'),
convertComments = require('./convert-comments').convertComments,
nodeUtils = require('./node-utils');

//------------------------------------------------------------------------------
// Private
//------------------------------------------------------------------------------
import { convert } from './convert';
import { convertComments } from './convert-comments';
import nodeUtils from './node-utils';
import { Extra } from './temp-types-based-on-js-source';

/**
* Extends and formats a given error object
* @param {Object} error the error object
* @returns {Object} converted error object
*/
function convertError(error) {
function convertError(error: any) {
return nodeUtils.createError(
error.file,
error.start,
error.message || error.messageText
);
}

//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------

module.exports = (ast, extra) => {
export default (ast: any, extra: Extra) => {
/**
* The TypeScript compiler produced fundamental parse errors when parsing the
* source.
Expand All @@ -49,7 +35,7 @@ module.exports = (ast, extra) => {
/**
* Recursively convert the TypeScript AST into an ESTree-compatible AST
*/
const estree = convert({
const estree: any = convert({
node: ast,
parent: null,
ast,
Expand Down
15 changes: 1 addition & 14 deletions lib/ast-node-types.js → src/ast-node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,7 @@
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/

'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

// None!

//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------

module.exports = {
export const AST_NODE_TYPES: { [key: string]: string } = {
ArrayExpression: 'ArrayExpression',
ArrayPattern: 'ArrayPattern',
ArrowFunctionExpression: 'ArrowFunctionExpression',
Expand Down
78 changes: 36 additions & 42 deletions lib/convert-comments.js → src/convert-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,34 @@
* MIT License
*/

'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const ts = require('typescript'),
nodeUtils = require('./node-utils');

//------------------------------------------------------------------------------
// Private
//------------------------------------------------------------------------------
import ts from 'typescript';
import nodeUtils from './node-utils';
import {
ESTreeComment,
ESTreeToken,
LineAndColumnData
} from './temp-types-based-on-js-source';

/**
* 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 {int} start The index at which the comment starts.
* @param {int} end The index at which the comment ends.
* @param {Location} startLoc The location at which the comment starts.
* @param {Location} endLoc The location at which the comment ends.
* @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
*/
function convertTypeScriptCommentToEsprimaComment(
block,
text,
start,
end,
startLoc,
endLoc
) {
const comment = {
block: boolean,
text: string,
start: number,
end: number,
startLoc: LineAndColumnData,
endLoc: LineAndColumnData
): ESTreeComment {
const comment: Partial<ESTreeToken> = {
type: block ? 'Block' : 'Line',
value: text
};
Expand All @@ -53,18 +48,22 @@ function convertTypeScriptCommentToEsprimaComment(
};
}

return comment;
return comment as ESTreeComment;
}

/**
* Convert comment from TypeScript Triva Scanner.
* @param {Object} triviaScanner TS Scanner
* @param {Object} ast the AST object
* @param {ts.Scanner} triviaScanner TS Scanner
* @param {ts.SourceFile} ast the AST object
* @param {string} code TypeScript code
* @returns {ESTreeComment} the converted ESTreeComment
* @private
*/
function getCommentFromTriviaScanner(triviaScanner, ast, code) {
function getCommentFromTriviaScanner(
triviaScanner: ts.Scanner,
ast: ts.SourceFile,
code: string
): ESTreeComment {
const kind = triviaScanner.getToken();
const isBlock = kind === ts.SyntaxKind.MultiLineCommentTrivia;
const range = {
Expand All @@ -91,23 +90,18 @@ function getCommentFromTriviaScanner(triviaScanner, ast, code) {
return esprimaComment;
}

//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------

module.exports = {
convertComments
};

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

/**
* Create a TypeScript Scanner, with skipTrivia set to false so that
Expand All @@ -120,7 +114,7 @@ function convertComments(ast, code) {
const start = triviaScanner.getTokenPos();
const end = triviaScanner.getTextPos();

let container = null;
let container: ts.Token<any> | null = null;
switch (kind) {
case ts.SyntaxKind.SingleLineCommentTrivia:
case ts.SyntaxKind.MultiLineCommentTrivia: {
Expand All @@ -130,7 +124,7 @@ function convertComments(ast, code) {
break;
}
case ts.SyntaxKind.CloseBraceToken:
container = nodeUtils.getNodeContainer(ast, start, end);
container = nodeUtils.getNodeContainer(ast, start, end) as ts.Node;

if (
container.kind === ts.SyntaxKind.TemplateMiddle ||
Expand All @@ -142,7 +136,7 @@ function convertComments(ast, code) {
break;
case ts.SyntaxKind.SlashToken:
case ts.SyntaxKind.SlashEqualsToken:
container = nodeUtils.getNodeContainer(ast, start, end);
container = nodeUtils.getNodeContainer(ast, start, end) as ts.Node;

if (container.kind === ts.SyntaxKind.RegularExpressionLiteral) {
kind = triviaScanner.reScanSlashToken();
Expand Down
Loading