-
Notifications
You must be signed in to change notification settings - Fork 100
[Feature] Add ban rule converter #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
KingDarBoja
wants to merge
1
commit into
typescript-eslint:master
from
KingDarBoja:feature/ban-converter
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { RuleConverter } from "../converter"; | ||
|
||
type BanRule = { | ||
object?: string; | ||
property?: string; | ||
message?: string; | ||
}; | ||
|
||
export const convertBan: RuleConverter = tslintRule => { | ||
let bannedObjMethod: BanRule[] = []; | ||
|
||
for (const rule of tslintRule.ruleArguments) { | ||
switch (rule.constructor.name) { | ||
case "String": | ||
bannedObjMethod.push({ | ||
property: rule, | ||
}); | ||
break; | ||
case "Array": | ||
if (rule.length == 1) { | ||
bannedObjMethod.push({ | ||
property: rule[0], | ||
}); | ||
} else { | ||
bannedObjMethod.push({ | ||
object: rule[0], | ||
property: rule[1], | ||
...{ message: rule[2] ? rule[2] : undefined }, | ||
}); | ||
} | ||
break; | ||
case "Object": | ||
if (Array.isArray(rule.name)) { | ||
if (rule.name.length == 2) { | ||
// TODO: Fix iteration if multiple properties. | ||
bannedObjMethod.push({ | ||
...{ object: rule.name[0] !== "*" ? rule.name[0] : undefined }, | ||
property: rule.name[1], | ||
...{ message: rule.message ? rule.message : undefined }, | ||
}); | ||
} else { | ||
// Don't know how to deal with nested functions. | ||
} | ||
} else { | ||
bannedObjMethod.push({ | ||
property: rule.name, | ||
...{ message: rule.message ? rule.message : undefined }, | ||
}); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
const ruleArguments = | ||
tslintRule.ruleArguments.length === 0 ? undefined : [2, ...bannedObjMethod]; | ||
|
||
return { | ||
rules: [ | ||
{ | ||
ruleName: "no-restricted-properties", | ||
...{ ruleArguments }, | ||
}, | ||
], | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { convertBan } from "../ban"; | ||
|
||
describe(convertBan, () => { | ||
test("conversion without arguments", () => { | ||
const result = convertBan({ | ||
ruleArguments: [], | ||
}); | ||
|
||
expect(result).toEqual({ | ||
rules: [ | ||
{ | ||
ruleName: "no-restricted-properties", | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test("conversion with single string argument", () => { | ||
const result = convertBan({ | ||
ruleArguments: [true, "eval"], | ||
}); | ||
|
||
expect(result).toEqual({ | ||
rules: [ | ||
{ | ||
ruleName: "no-restricted-properties", | ||
ruleArguments: [ | ||
2, | ||
{ | ||
property: "eval", | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test("conversion with single array argument", () => { | ||
const result = convertBan({ | ||
ruleArguments: [true, ["map"]], | ||
}); | ||
|
||
expect(result).toEqual({ | ||
rules: [ | ||
{ | ||
ruleName: "no-restricted-properties", | ||
ruleArguments: [ | ||
2, | ||
{ | ||
property: "map", | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test("conversion with array argument but no message", () => { | ||
const result = convertBan({ | ||
ruleArguments: [true, ["describe", "only"]], | ||
}); | ||
|
||
expect(result).toEqual({ | ||
rules: [ | ||
{ | ||
ruleName: "no-restricted-properties", | ||
ruleArguments: [ | ||
2, | ||
{ | ||
object: "describe", | ||
property: "only", | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test("conversion with array argument and message", () => { | ||
const result = convertBan({ | ||
ruleArguments: [true, ["describe", "only", "use another method"]], | ||
}); | ||
|
||
expect(result).toEqual({ | ||
rules: [ | ||
{ | ||
ruleName: "no-restricted-properties", | ||
ruleArguments: [ | ||
2, | ||
{ | ||
object: "describe", | ||
property: "only", | ||
message: "use another method", | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test("conversion with object argument and property name as string but no message", () => { | ||
const result = convertBan({ | ||
ruleArguments: [true, { name: "$" }], | ||
}); | ||
|
||
expect(result).toEqual({ | ||
rules: [ | ||
{ | ||
ruleName: "no-restricted-properties", | ||
ruleArguments: [ | ||
2, | ||
{ | ||
property: "$", | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test("conversion with object argument and property name as array and message", () => { | ||
const result = convertBan({ | ||
ruleArguments: [true, { name: ["it", "only"], message: "do not focus tests" }], | ||
}); | ||
|
||
expect(result).toEqual({ | ||
rules: [ | ||
{ | ||
ruleName: "no-restricted-properties", | ||
ruleArguments: [ | ||
2, | ||
{ | ||
object: "it", | ||
property: "only", | ||
message: "do not focus tests", | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this'll need some work. TSLint's
ban
rule bans both globals and properties. ESLint splits those betweenno-restricted-globals
andno-restricted-properties
.For example, this TSLint configuration:
...is, from my understanding, equivalent to this ESLint configuration:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am confused as the roadmap states that it should be only the
no-restricted-properties
rule. ESLint ruleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KingDarBoja perhaps the roadmap is incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be outdated but should be good if someone check that.