forked from eslint/espree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
109 lines (81 loc) · 3.01 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @fileoverview Tests for tokenize().
* @author Nicholas C. Zakas
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("assert"),
espree = require("../../espree"),
tester = require("./tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("parse()", () => {
describe("ecmaVersion", () => {
it("should be 5 if not specified", () => {
// `ecmaVersion: 3` would throw on getters/setters
espree.parse("var foo = { get bar() {} }");
// needs `ecmaVersion: 6` or higher
assert.throws(() => {
espree.parse("let foo");
});
});
});
describe("modules", () => {
it("should have correct column number when strict mode error occurs", () => {
try {
espree.parse("function fn(a, a) {\n}", { ecmaVersion: 6, sourceType: "module" });
} catch (err) {
assert.strictEqual(err.column, 16);
}
});
});
describe("ES5", () => {
it("should throw an error when using the y regex flag", () => {
assert.throws(() => {
espree.parse("/./y");
});
});
it("should throw an error when using the u regex flag", () => {
assert.throws(() => {
espree.parse("/./u");
});
});
});
describe("general", () => {
it("should output tokens, comments, locs, and ranges when called with those options", () => {
const ast = espree.parse("let foo = bar;", {
ecmaVersion: 6,
comment: true,
tokens: true,
range: true,
loc: true
});
assert.deepStrictEqual(tester.getRaw(ast), require("../fixtures/parse/all-pieces.json"));
});
it("should reset lastToken on each parse", () => {
espree.parse("var foo = bar;");
const ast = espree.parse("//foo", {
comment: true,
tokens: true,
range: true,
loc: true
});
assert.deepStrictEqual(ast.range, [0, 5]);
assert.deepStrictEqual([ast.loc.start.line, ast.loc.start.column], [1, 0]);
assert.deepStrictEqual([ast.loc.end.line, ast.loc.end.column], [1, 5]);
});
it("should not mutate config", () => {
espree.parse("foo", Object.freeze({ ecmaFeatures: Object.freeze({}) }));
});
it("Cast valid non-string input", () => {
const str = "var foo = bar;";
assert.deepStrictEqual(
espree.parse(Buffer.from(str)),
espree.parse(str)
);
});
});
});