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

Commit

Permalink
Fix: Allow running without options (fixes #121)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Dec 5, 2016
1 parent 1dc4039 commit de1be29
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 72 deletions.
139 changes: 69 additions & 70 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Expand Down
15 changes: 13 additions & 2 deletions tests/lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,19 @@ function getRaw(ast) {

describe("parse()", function() {


describe("basic functionality", function() {

it("should parse an empty string", function() {
parser.parse("");
parser.parse("", {});
});

});

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) {
Expand All @@ -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: {
Expand All @@ -61,6 +71,7 @@ describe("parse()", function() {
assert.deepEqual(getRaw(ast), require("../fixtures/parse/all-pieces.json"));
});


});


});

0 comments on commit de1be29

Please sign in to comment.