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

Commit ff283aa

Browse files
flying-sheepJamesHenry
authored andcommitted
Fix: Allow running without options (fixes #121) (#120)
1 parent dd03b2f commit ff283aa

File tree

3 files changed

+82
-72
lines changed

3 files changed

+82
-72
lines changed

parser.js

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -135,82 +135,81 @@ function parse(code, options) {
135135
// pass through jsx option
136136
extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx;
137137
}
138+
}
138139

139-
// Even if jsx option is set in typescript compiler, filename still has to
140-
// contain .tsx file extension
141-
var FILENAME = (extra.ecmaFeatures.jsx) ? "eslint.tsx" : "eslint.ts";
142-
143-
var compilerHost = {
144-
fileExists: function() {
145-
return true;
146-
},
147-
getCanonicalFileName: function() {
148-
return FILENAME;
149-
},
150-
getCurrentDirectory: function() {
151-
return "";
152-
},
153-
getDefaultLibFileName: function() {
154-
return "lib.d.ts";
155-
},
156-
157-
// TODO: Support Windows CRLF
158-
getNewLine: function() {
159-
return "\n";
160-
},
161-
getSourceFile: function(filename) {
162-
return ts.createSourceFile(filename, code, ts.ScriptTarget.Latest, true);
163-
},
164-
readFile: function() {
165-
return null;
166-
},
167-
useCaseSensitiveFileNames: function() {
168-
return true;
169-
},
170-
writeFile: function() {
171-
return null;
172-
}
173-
};
140+
// Even if jsx option is set in typescript compiler, filename still has to
141+
// contain .tsx file extension
142+
var FILENAME = (extra.ecmaFeatures.jsx) ? "eslint.tsx" : "eslint.ts";
143+
144+
var compilerHost = {
145+
fileExists: function() {
146+
return true;
147+
},
148+
getCanonicalFileName: function() {
149+
return FILENAME;
150+
},
151+
getCurrentDirectory: function() {
152+
return "";
153+
},
154+
getDefaultLibFileName: function() {
155+
return "lib.d.ts";
156+
},
157+
158+
// TODO: Support Windows CRLF
159+
getNewLine: function() {
160+
return "\n";
161+
},
162+
getSourceFile: function(filename) {
163+
return ts.createSourceFile(filename, code, ts.ScriptTarget.Latest, true);
164+
},
165+
readFile: function() {
166+
return null;
167+
},
168+
useCaseSensitiveFileNames: function() {
169+
return true;
170+
},
171+
writeFile: function() {
172+
return null;
173+
}
174+
};
174175

175-
program = ts.createProgram([FILENAME], {
176-
noResolve: true,
177-
target: ts.ScriptTarget.Latest,
178-
jsx: extra.ecmaFeatures.jsx ? "preserve" : undefined
179-
}, compilerHost);
180-
181-
var ast = program.getSourceFile(FILENAME);
182-
183-
if (extra.attachComment || extra.comment) {
184-
/**
185-
* Create a TypeScript Scanner, with skipTrivia set to false so that
186-
* we can parse the comments
187-
*/
188-
var triviaScanner = ts.createScanner(ast.languageVersion, false, 0, code);
189-
190-
var kind = triviaScanner.scan();
191-
while (kind !== ts.SyntaxKind.EndOfFileToken) {
192-
if (kind !== ts.SyntaxKind.SingleLineCommentTrivia && kind !== ts.SyntaxKind.MultiLineCommentTrivia) {
193-
kind = triviaScanner.scan();
194-
continue;
195-
}
196-
197-
var isBlock = (kind === ts.SyntaxKind.MultiLineCommentTrivia);
198-
var range = {
199-
pos: triviaScanner.getTokenPos(),
200-
end: triviaScanner.getTextPos(),
201-
kind: triviaScanner.getToken()
202-
};
203-
204-
var comment = code.substring(range.pos, range.end);
205-
var text = comment.replace("//", "").replace("/*", "").replace("*/", "");
206-
var loc = getLocFor(range.pos, range.end, ast);
207-
208-
var esprimaComment = convertTypeScriptCommentToEsprimaComment(isBlock, text, range.pos, range.end, loc.start, loc.end);
209-
extra.comments.push(esprimaComment);
176+
program = ts.createProgram([FILENAME], {
177+
noResolve: true,
178+
target: ts.ScriptTarget.Latest,
179+
jsx: extra.ecmaFeatures.jsx ? "preserve" : undefined
180+
}, compilerHost);
210181

182+
var ast = program.getSourceFile(FILENAME);
183+
184+
if (extra.attachComment || extra.comment) {
185+
/**
186+
* Create a TypeScript Scanner, with skipTrivia set to false so that
187+
* we can parse the comments
188+
*/
189+
var triviaScanner = ts.createScanner(ast.languageVersion, false, 0, code);
190+
191+
var kind = triviaScanner.scan();
192+
while (kind !== ts.SyntaxKind.EndOfFileToken) {
193+
if (kind !== ts.SyntaxKind.SingleLineCommentTrivia && kind !== ts.SyntaxKind.MultiLineCommentTrivia) {
211194
kind = triviaScanner.scan();
195+
continue;
212196
}
213197

198+
var isBlock = (kind === ts.SyntaxKind.MultiLineCommentTrivia);
199+
var range = {
200+
pos: triviaScanner.getTokenPos(),
201+
end: triviaScanner.getTextPos(),
202+
kind: triviaScanner.getToken()
203+
};
204+
205+
var comment = code.substring(range.pos, range.end);
206+
var text = comment.replace("//", "").replace("/*", "").replace("*/", "");
207+
var loc = getLocFor(range.pos, range.end, ast);
208+
209+
var esprimaComment = convertTypeScriptCommentToEsprimaComment(isBlock, text, range.pos, range.end, loc.start, loc.end);
210+
extra.comments.push(esprimaComment);
211+
212+
kind = triviaScanner.scan();
214213
}
215214

216215
}
File renamed without changes.

tests/lib/parse.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,19 @@ function getRaw(ast) {
3333

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

36+
37+
describe("basic functionality", function() {
38+
39+
it("should parse an empty string", function() {
40+
assert.deepEqual(parser.parse("").body, []);
41+
assert.deepEqual(parser.parse("", {}).body, []);
42+
});
43+
44+
});
45+
3646
describe("modules", function() {
3747

3848
it("should have correct column number when strict mode error occurs", function() {
39-
4049
try {
4150
parser.parse("function fn(a, a) {\n}", { sourceType: "module" });
4251
} catch (err) {
@@ -47,6 +56,7 @@ describe("parse()", function() {
4756
});
4857

4958
describe("general", function() {
59+
5060
it("should output tokens, comments, locs, and ranges when called with those options", function() {
5161
var ast = parser.parse("let foo = bar;", {
5262
ecmaFeatures: {
@@ -61,6 +71,7 @@ describe("parse()", function() {
6171
assert.deepEqual(getRaw(ast), require("../fixtures/parse/all-pieces.json"));
6272
});
6373

64-
6574
});
75+
76+
6677
});

0 commit comments

Comments
 (0)