From 585a1c42424070b1cc7245daded941ed20f332de Mon Sep 17 00:00:00 2001 From: Philipp A Date: Mon, 5 Dec 2016 20:51:39 +0100 Subject: [PATCH] Fix: Allow running without options (fixes #121) --- parser.js | 139 ++++++++++++------------- tests/lib/{.eslintrc => .eslintrc.yml} | 0 tests/lib/parse.js | 15 ++- 3 files changed, 82 insertions(+), 72 deletions(-) rename tests/lib/{.eslintrc => .eslintrc.yml} (100%) diff --git a/parser.js b/parser.js index 8fc1672..34e5ede 100644 --- a/parser.js +++ b/parser.js @@ -135,82 +135,81 @@ function parse(code, options) { // pass through jsx option extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx; } + } - // Even if jsx option is set in typescript compiler, filename still has to - // contain .tsx file extension - var FILENAME = (extra.ecmaFeatures.jsx) ? "eslint.tsx" : "eslint.ts"; - - var compilerHost = { - fileExists: function() { - return true; - }, - getCanonicalFileName: function() { - return FILENAME; - }, - getCurrentDirectory: function() { - return ""; - }, - getDefaultLibFileName: function() { - return "lib.d.ts"; - }, - - // TODO: Support Windows CRLF - getNewLine: function() { - return "\n"; - }, - getSourceFile: function(filename) { - return ts.createSourceFile(filename, code, ts.ScriptTarget.Latest, true); - }, - readFile: function() { - return null; - }, - useCaseSensitiveFileNames: function() { - return true; - }, - writeFile: function() { - return null; - } - }; + // Even if jsx option is set in typescript compiler, filename still has to + // contain .tsx file extension + var FILENAME = (extra.ecmaFeatures.jsx) ? "eslint.tsx" : "eslint.ts"; + + var compilerHost = { + fileExists: function() { + return true; + }, + getCanonicalFileName: function() { + return FILENAME; + }, + getCurrentDirectory: function() { + return ""; + }, + getDefaultLibFileName: function() { + return "lib.d.ts"; + }, + + // TODO: Support Windows CRLF + getNewLine: function() { + return "\n"; + }, + getSourceFile: function(filename) { + return ts.createSourceFile(filename, code, ts.ScriptTarget.Latest, true); + }, + readFile: function() { + return null; + }, + useCaseSensitiveFileNames: function() { + return true; + }, + writeFile: function() { + return null; + } + }; - program = ts.createProgram([FILENAME], { - noResolve: true, - target: ts.ScriptTarget.Latest, - jsx: extra.ecmaFeatures.jsx ? "preserve" : undefined - }, compilerHost); - - var ast = program.getSourceFile(FILENAME); - - if (extra.attachComment || extra.comment) { - /** - * Create a TypeScript Scanner, with skipTrivia set to false so that - * we can parse the comments - */ - var triviaScanner = ts.createScanner(ast.languageVersion, false, 0, code); - - var kind = triviaScanner.scan(); - while (kind !== ts.SyntaxKind.EndOfFileToken) { - if (kind !== ts.SyntaxKind.SingleLineCommentTrivia && kind !== ts.SyntaxKind.MultiLineCommentTrivia) { - kind = triviaScanner.scan(); - continue; - } - - var isBlock = (kind === ts.SyntaxKind.MultiLineCommentTrivia); - var range = { - pos: triviaScanner.getTokenPos(), - end: triviaScanner.getTextPos(), - kind: triviaScanner.getToken() - }; - - var comment = code.substring(range.pos, range.end); - var text = comment.replace("//", "").replace("/*", "").replace("*/", ""); - var loc = getLocFor(range.pos, range.end, ast); - - var esprimaComment = convertTypeScriptCommentToEsprimaComment(isBlock, text, range.pos, range.end, loc.start, loc.end); - extra.comments.push(esprimaComment); + program = ts.createProgram([FILENAME], { + noResolve: true, + target: ts.ScriptTarget.Latest, + jsx: extra.ecmaFeatures.jsx ? "preserve" : undefined + }, compilerHost); + var ast = program.getSourceFile(FILENAME); + + if (extra.attachComment || extra.comment) { + /** + * Create a TypeScript Scanner, with skipTrivia set to false so that + * we can parse the comments + */ + var triviaScanner = ts.createScanner(ast.languageVersion, false, 0, code); + + var kind = triviaScanner.scan(); + while (kind !== ts.SyntaxKind.EndOfFileToken) { + if (kind !== ts.SyntaxKind.SingleLineCommentTrivia && kind !== ts.SyntaxKind.MultiLineCommentTrivia) { kind = triviaScanner.scan(); + continue; } + var isBlock = (kind === ts.SyntaxKind.MultiLineCommentTrivia); + var range = { + pos: triviaScanner.getTokenPos(), + end: triviaScanner.getTextPos(), + kind: triviaScanner.getToken() + }; + + var comment = code.substring(range.pos, range.end); + var text = comment.replace("//", "").replace("/*", "").replace("*/", ""); + var loc = getLocFor(range.pos, range.end, ast); + + var esprimaComment = convertTypeScriptCommentToEsprimaComment(isBlock, text, range.pos, range.end, loc.start, loc.end); + extra.comments.push(esprimaComment); + + kind = triviaScanner.scan(); } } diff --git a/tests/lib/.eslintrc b/tests/lib/.eslintrc.yml similarity index 100% rename from tests/lib/.eslintrc rename to tests/lib/.eslintrc.yml diff --git a/tests/lib/parse.js b/tests/lib/parse.js index faf9c42..00ad316 100644 --- a/tests/lib/parse.js +++ b/tests/lib/parse.js @@ -33,10 +33,19 @@ function getRaw(ast) { describe("parse()", function() { + + describe("basic functionality", function() { + + it("should parse an empty string", function() { + assert.deepEqual(parser.parse("").body, []); + assert.deepEqual(parser.parse("", {}).body, []); + }); + + }); + describe("modules", function() { it("should have correct column number when strict mode error occurs", function() { - try { parser.parse("function fn(a, a) {\n}", { sourceType: "module" }); } catch (err) { @@ -47,6 +56,7 @@ describe("parse()", function() { }); describe("general", function() { + it("should output tokens, comments, locs, and ranges when called with those options", function() { var ast = parser.parse("let foo = bar;", { ecmaFeatures: { @@ -61,6 +71,7 @@ describe("parse()", function() { assert.deepEqual(getRaw(ast), require("../fixtures/parse/all-pieces.json")); }); - }); + + });