Skip to content

Commit f0308e9

Browse files
jeremyyapJosh Goldberg
authored and
Josh Goldberg
committed
Add no-caller and no-eval mergers (#227)
1 parent 6a59ffa commit f0308e9

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

src/rules/mergers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { mergeBanTypes } from "./mergers/ban-types";
2+
import { mergeNoCaller } from "./mergers/no-caller";
3+
import { mergeNoEval } from "./mergers/no-eval";
24
import { mergeNoUnnecessaryTypeAssertion } from "./mergers/no-unnecessary-type-assertion";
35

46
export const mergers = new Map([
57
["@typescript-eslint/ban-types", mergeBanTypes],
68
["@typescript-eslint/no-unnecessary-type-assertion", mergeNoUnnecessaryTypeAssertion],
9+
["no-caller", mergeNoCaller],
10+
["no-eval", mergeNoEval],
711
]);

src/rules/mergers/no-caller.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { RuleMerger } from "../merger";
2+
3+
export const mergeNoCaller: RuleMerger = () => {
4+
// no-caller rule does not accept any options
5+
return [];
6+
};

src/rules/mergers/no-eval.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { RuleMerger } from "../merger";
2+
3+
export const mergeNoEval: RuleMerger = (existingOptions, newOptions) => {
4+
if (existingOptions === undefined && newOptions === undefined) {
5+
return [];
6+
}
7+
8+
let allowIndirect = true;
9+
10+
for (const options of [existingOptions, newOptions]) {
11+
if (
12+
options === undefined ||
13+
options.length === 0 ||
14+
options[0].allowIndirect === undefined
15+
) {
16+
allowIndirect = false;
17+
break;
18+
}
19+
20+
allowIndirect = allowIndirect && options[0].allowIndirect;
21+
}
22+
23+
return [
24+
{
25+
...(allowIndirect && { allowIndirect }),
26+
},
27+
];
28+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { mergeNoCaller } from "../no-caller";
2+
3+
describe(mergeNoCaller, () => {
4+
test("neither options existing", () => {
5+
const result = mergeNoCaller(undefined, undefined);
6+
7+
expect(result).toEqual([]);
8+
});
9+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { mergeNoEval } from "../no-eval";
2+
3+
describe(mergeNoEval, () => {
4+
test("neither options existing", () => {
5+
const result = mergeNoEval(undefined, undefined);
6+
7+
expect(result).toEqual([]);
8+
});
9+
10+
test("neither allowIndirect existing", () => {
11+
const result = mergeNoEval([{}], [{}]);
12+
13+
expect(result).toEqual([{}]);
14+
});
15+
16+
test("original allowIndirect existing", () => {
17+
const result = mergeNoEval([{ allowIndirect: true }], [{}]);
18+
19+
expect(result).toEqual([{}]);
20+
});
21+
22+
test("new allowIndirect existing", () => {
23+
const result = mergeNoEval([{}], [{ allowIndirect: true }]);
24+
25+
expect(result).toEqual([{}]);
26+
});
27+
28+
test("original allowIndirect is false but new allowIndirect is true", () => {
29+
const result = mergeNoEval([{ allowIndirect: false }], [{ allowIndirect: true }]);
30+
31+
expect(result).toEqual([{}]);
32+
});
33+
34+
test("original allowIndirect is true but new allowIndirect is false", () => {
35+
const result = mergeNoEval([{ allowIndirect: true }], [{ allowIndirect: false }]);
36+
37+
expect(result).toEqual([{}]);
38+
});
39+
40+
test("both allowIndirect are true", () => {
41+
const result = mergeNoEval([{ allowIndirect: true }], [{ allowIndirect: true }]);
42+
43+
expect(result).toEqual([{ allowIndirect: true }]);
44+
});
45+
});

0 commit comments

Comments
 (0)