Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Fix [ban]: wrong logic matching deeply nested methods #4383

Merged
merged 1 commit into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/rules/banRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class Rule extends Lint.Rules.AbstractRule {
{ name: ["it", "only"], message: "don't focus tests" },
{ name: ["chai", "assert", "equal"], message: "Use 'strictEqual' instead." },
{ name: ["*", "forEach"], message: "Use a regular for loop instead." },
{ name: ["*", "_id", "toString"], message: "Use 'toHexString' instead." },
],
],
type: "functionality",
Expand Down Expand Up @@ -167,14 +168,14 @@ class BanFunctionWalker extends Lint.AbstractWalker<Options> {
}

private checkForObjectMethodBan(expression: ts.PropertyAccessExpression) {
for (const ban of this.options.methods) {
outer: for (const ban of this.options.methods) {
if (expression.name.text !== ban.name) {
continue;
}
let current = expression.expression;
for (let i = ban.object.length - 1; i > 0; --i) {
if (!isPropertyAccessExpression(current) || current.name.text !== ban.object[i]) {
continue;
continue outer;
}
current = current.expression;
}
Expand Down
4 changes: 4 additions & 0 deletions test/rules/ban/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fit("some text", () => {});
someObject.fit()
chai.assert.equal(1, "1");
~~~~~~~~~~~~~~~~~ [err % ('chai.assert.equal', "Use 'strictEqual' instead.")]
chai.equal(1, "1");
assert.equal(1, "1");
foo.assert.equal(1, "1");
someObject.chai.assert.equal(1, "1");
Expand All @@ -33,5 +34,8 @@ arr.forEach(() => {});
~~~~~~~~~~~ [err % ('*.forEach', 'Use a regular for loop instead.')]
someObject.someProperty.forEach(() => {});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [err % ('*.forEach', 'Use a regular for loop instead.')]
someObject._id.toString();
~~~~~~~~~~~~~~~~~~~~~~~ [err % ('*._id.toString', "Use 'toHexString' instead.")]
someObject.toString();

[err]: Calls to '%s' are not allowed.
3 changes: 2 additions & 1 deletion test/rules/ban/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
{"name": ["_", "map"]},
["_", "filter", "Use the native JavaScript 'myArray.filter' instead."],
{"name": ["*", "forEach"], "message": "Use a regular for loop instead."},
{"name": ["chai", "assert", "equal"], "message": "Use 'strictEqual' instead."}
{"name": ["chai", "assert", "equal"], "message": "Use 'strictEqual' instead."},
{"name": ["*", "_id", "toString"], "message": "Use 'toHexString' instead."}
]
}
}