Skip to content

Commit 5fa87cd

Browse files
MouzouridesJosh Goldberg
authored and
Josh Goldberg
committed
Implement converter for triple-equals rule #187 (#190)
1 parent 09cf55f commit 5fa87cd

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

src/rules/converters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import { convertUnifiedSignatures } from "./converters/unified-signatures";
100100
import { convertUnnecessaryBind } from "./converters/unnecessary-bind";
101101
import { convertUnnecessaryConstructor } from "./converters/unnecessary-constructor";
102102
import { convertUseIsnan } from "./converters/use-isnan";
103+
import { convertTripleEquals } from "./converters/triple-equals";
103104

104105
/**
105106
* Keys TSLint rule names to their ESLint rule converters.
@@ -207,7 +208,7 @@ export const converters = new Map([
207208
["no-octal-literal", convertNoOctalLiteral],
208209
["no-regex-spaces", convertNoRegexSpaces],
209210
["no-unnecessary-semicolons", convertNoUnnecessarySemicolons],
210-
211+
["triple-equals", convertTripleEquals],
211212
// These converters are all for rules that need more complex option conversions.
212213
// Some of them will likely need to have notices about changed lint behaviors...
213214
// If you're willing to take on that work, that'd be great! Please send PRs! 💖
@@ -225,7 +226,6 @@ export const converters = new Map([
225226
// ["no-void-expression", convertNoVoidExpression], // (no exact equivalent)
226227
// ["quotemark", convertQuotemark], // quotes
227228
// ["space-within-parens", convertSpaceWithinParens], // space-in-parens
228-
// ["triple-equals", convertTripleEquals], // eqeqeq
229229
// ["variable-name", convertVariableName], // a bunch of rules...
230230

231231
// tslint-microsoft-contrib rules:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { convertTripleEquals } from "../triple-equals";
2+
3+
describe(convertTripleEquals, () => {
4+
test("conversion without arguments", () => {
5+
const result = convertTripleEquals({
6+
ruleArguments: [],
7+
});
8+
9+
expect(result).toEqual({
10+
rules: [
11+
{
12+
ruleArguments: ["always"],
13+
ruleName: "eqeqeq",
14+
notices: [],
15+
},
16+
],
17+
});
18+
});
19+
20+
test("conversion with an allow-null-check argument", () => {
21+
const result = convertTripleEquals({
22+
ruleArguments: ["allow-null-check"],
23+
});
24+
25+
expect(result).toEqual({
26+
rules: [
27+
{
28+
ruleArguments: ["smart"],
29+
ruleName: "eqeqeq",
30+
notices: [
31+
'Option "smart" allows for comparing two literal values, evaluating the value of typeof and null comparisons.',
32+
],
33+
},
34+
],
35+
});
36+
});
37+
38+
test("conversion with an allow-undefined-check argument", () => {
39+
const result = convertTripleEquals({
40+
ruleArguments: ["allow-undefined-check"],
41+
});
42+
43+
expect(result).toEqual({
44+
rules: [
45+
{
46+
ruleArguments: ["smart"],
47+
ruleName: "eqeqeq",
48+
notices: [
49+
'Option "allow-undefined-check" is not supported by ESLint. Option "smart" is the closest.',
50+
'Option "smart" allows for comparing two literal values, evaluating the value of typeof and null comparisons.',
51+
],
52+
},
53+
],
54+
});
55+
});
56+
});

src/rules/converters/triple-equals.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { RuleConverter } from "../converter";
2+
3+
export const convertTripleEquals: RuleConverter = tslintRule => {
4+
const getRuleOptions = () => {
5+
const smartOptionNotice =
6+
'Option "smart" allows for comparing two literal values, evaluating the value of typeof and null comparisons.';
7+
8+
if (
9+
tslintRule.ruleArguments.length !== 0 &&
10+
tslintRule.ruleArguments[0] === "allow-null-check"
11+
) {
12+
return {
13+
arguments: ["smart"],
14+
notices: [smartOptionNotice],
15+
};
16+
}
17+
18+
if (
19+
tslintRule.ruleArguments.length !== 0 &&
20+
tslintRule.ruleArguments[0] === "allow-undefined-check"
21+
) {
22+
return {
23+
arguments: ["smart"],
24+
notices: [
25+
'Option "allow-undefined-check" is not supported by ESLint. Option "smart" is the closest.',
26+
smartOptionNotice,
27+
],
28+
};
29+
}
30+
31+
return {
32+
arguments: ["always"],
33+
notices: [],
34+
};
35+
};
36+
37+
const options = getRuleOptions();
38+
39+
return {
40+
rules: [
41+
{
42+
ruleName: "eqeqeq",
43+
ruleArguments: options.arguments,
44+
notices: options.notices,
45+
},
46+
],
47+
};
48+
};

0 commit comments

Comments
 (0)