/
strict.js
212 lines (192 loc) 路 5.33 KB
/
strict.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import eslint from "eslint";
import fs from "fs";
import path from "path";
import * as parser from "../../../../../babel-eslint-parser";
eslint.linter.defineParser("@babel/eslint-parser", parser);
const paths = {
fixtures: new URL("../../../fixtures/rules", import.meta.url).pathname,
};
const encoding = "utf8";
const errorLevel = 2;
const baseEslintOpts = {
parser: "@babel/eslint-parser",
parserOptions: {
sourceType: "script",
requireConfigFile: false,
babelOptions: { configFile: false },
},
};
/**
* Load a fixture and run eslint.linter.verify() on it.
* Pass the return value to done().
* @param object opts
* @param function done
*/
function lint(opts, done) {
readFixture(opts.fixture, (err, src) => {
if (err) return done(err);
done(null, eslint.linter.verify(src, opts.eslint));
});
}
/**
* Read a fixture file, passing the content to done().
* @param string|array id
* @param function done
*/
function readFixture(id, done) {
if (Array.isArray(id)) id = path.join.apply(path, id);
if (!path.extname(id)) id += ".js";
fs.readFile(path.join(paths.fixtures, id), encoding, done);
}
// readFixture
describe("Rules:", () => {
describe("`strict`", strictSuite);
});
function strictSuite() {
const ruleId = "strict";
describe("when set to 'never'", () => {
const eslintOpts = Object.assign({}, baseEslintOpts, {
rules: {},
});
eslintOpts.rules[ruleId] = [errorLevel, "never"];
["global-with", "function-with"].forEach(fixture => {
it(`should error on ${fixture.match(/^[^-]+/)[0]} directive`, done => {
lint(
{
fixture: ["strict", fixture],
eslint: eslintOpts,
},
(err, report) => {
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
done();
},
);
});
});
});
describe("when set to 'global'", () => {
const eslintOpts = Object.assign({}, baseEslintOpts, {
rules: {},
});
eslintOpts.rules[ruleId] = [errorLevel, "global"];
it("shouldn't error on single global directive", done => {
lint(
{
fixture: ["strict", "global-with"],
eslint: eslintOpts,
},
(err, report) => {
if (err) return done(err);
expect(report.length).toBe(0);
done();
},
);
});
it("should error twice on global directive: no and function directive: yes", done => {
lint(
{
fixture: ["strict", "function-with"],
eslint: eslintOpts,
},
(err, report) => {
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
done();
},
);
});
it("should error on function directive", done => {
lint(
{
fixture: ["strict", "global-with-function-with"],
eslint: eslintOpts,
},
(err, report) => {
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
// This is to make sure the test fails prior to adapting Babel AST
// directive representation to ESLint format. Otherwise it reports an
// error for missing global directive that masquerades as the expected
// result of the previous assertion.
expect(report[0].nodeType).not.toBe("Program");
done();
},
);
});
it("should error on no directive", done => {
lint(
{
fixture: ["strict", "none"],
eslint: eslintOpts,
},
(err, report) => {
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
done();
},
);
});
});
describe("when set to 'function'", () => {
const eslintOpts = Object.assign({}, baseEslintOpts, {
rules: {},
});
eslintOpts.rules[ruleId] = [errorLevel, "function"];
it("shouldn't error on single function directive", done => {
lint(
{
fixture: ["strict", "function-with"],
eslint: eslintOpts,
},
(err, report) => {
if (err) return done(err);
expect(report.length).toBe(0);
done();
},
);
});
it("should error twice on function directive: no and global directive: yes", done => {
lint(
{
fixture: ["strict", "global-with-function-without"],
eslint: eslintOpts,
},
(err, report) => {
if (err) return done(err);
[0, 1].forEach(i => {
expect(report[i].ruleId).toBe(ruleId);
});
done();
},
);
});
it("should error on only global directive", done => {
lint(
{
fixture: ["strict", "global-with"],
eslint: eslintOpts,
},
(err, report) => {
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
done();
},
);
});
it("should error on extraneous global directive", done => {
lint(
{
fixture: ["strict", "global-with-function-with"],
eslint: eslintOpts,
},
(err, report) => {
if (err) return done(err);
expect(report[0].ruleId).toBe(ruleId);
expect(report[0].nodeType.indexOf("Function")).toBe(-1);
done();
},
);
});
});
}