This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
Better ban rule failure messages & allows for additional custom messages #1385
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7144ec9
Updating ban rule to have better messages, and an optional explanatio…
9b9d6a4
the no-console rule uses the ban rule, so updating the messages here …
bc96680
Minor text change suggestions
0f13e17
Merge remote-tracking branch 'upstream/master' into custom-ban-messages
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 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
This file contains 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 |
---|---|---|
|
@@ -25,23 +25,26 @@ export class Rule extends Lint.Rules.AbstractRule { | |
ruleName: "ban", | ||
description: "Bans the use of specific functions.", | ||
descriptionDetails: "At this time, there is no way to disable global methods with this rule.", | ||
optionsDescription: "A list of `['object', 'method']` pairs which ban `object.method()`.", | ||
optionsDescription: "A list of `['object', 'method', 'optional explanation here']` which ban `object.method()`.", | ||
options: { | ||
type: "list", | ||
listType: { | ||
type: "array", | ||
arrayMembers: [ | ||
{ type: "string" }, | ||
{ type: "string" }, | ||
{ type: "string" }, | ||
], | ||
}, | ||
}, | ||
optionExamples: [`[true, ["console", "log"], ["someObject", "someFunction"]]`], | ||
optionExamples: [`[true, ["someObject", "someFunction"], ["someObject", "someFunction", "Optional explanation"]]`], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want both banned functions to be |
||
type: "functionality", | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING_PART = "function invocation disallowed: "; | ||
public static FAILURE_STRING_FACTORY = (expression: string, messageAddition?: string) => { | ||
return `Calls to '${expression}' are not allowed.${messageAddition ? " " + messageAddition : ""}`; | ||
}; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
const options = this.getOptions(); | ||
|
@@ -86,7 +89,7 @@ export class BanFunctionWalker extends Lint.RuleWalker { | |
const failure = this.createFailure( | ||
expression.getStart(), | ||
expression.getWidth(), | ||
`${Rule.FAILURE_STRING_PART}${leftSideExpression}.${rightSideExpression}` | ||
Rule.FAILURE_STRING_FACTORY(`${leftSideExpression}.${rightSideExpression}`, bannedFunction[2]) | ||
); | ||
this.addFailure(failure); | ||
} | ||
|
This file contains 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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
console.time(); | ||
window.toString(); | ||
~~~~~~~~~~~~~~~ [function invocation disallowed: window.toString] | ||
~~~~~~~~~~~~~~~ [Calls to 'window.toString' are not allowed.] | ||
console.log(); | ||
document.window.toString(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~ [function invocation disallowed: window.toString] | ||
~~~~~~~~~~~~~~~~~~~~~~~~ [Calls to 'window.toString' are not allowed.] | ||
reference.randomContainer.window.toString(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [function invocation disallowed: window.toString] | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Calls to 'window.toString' are not allowed.] | ||
globals.getDocument().window.toString(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [function invocation disallowed: window.toString] | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Calls to 'window.toString' are not allowed.] | ||
_.keys(obj).forEach(fun); | ||
_.forEach(fun); | ||
~~~~~~~~~ [function invocation disallowed: _.forEach] | ||
~~~~~~~~~ [Calls to '_.forEach' are not allowed.] | ||
_.filter(array); | ||
~~~~~~~~ [Calls to '_.filter' are not allowed. Use the native JavaScript 'myArray.filter' instead.] |
This file contains 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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
{ | ||
"rules": { | ||
"ban": [true, ["window", "toString"], ["_", "forEach"]] | ||
"ban": [ | ||
true, | ||
["window", "toString"], | ||
["_", "forEach"], | ||
["_", "filter", "Use the native JavaScript 'myArray.filter' instead."] | ||
] | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
console.time(); | ||
console.log("log"); | ||
~~~~~~~~~~~ [function invocation disallowed: console.log] | ||
~~~~~~~~~~~ [Calls to 'console.log' are not allowed.] | ||
console.dir(object); | ||
~~~~~~~~~~~ [function invocation disallowed: console.dir] | ||
~~~~~~~~~~~ [Calls to 'console.dir' are not allowed.] | ||
console.info("info"); | ||
console.trace("trace"); | ||
console.warn("warn"); | ||
~~~~~~~~~~~~ [function invocation disallowed: console.warn] | ||
~~~~~~~~~~~~ [Calls to 'console.warn' are not allowed.] | ||
console.error("error"); | ||
~~~~~~~~~~~~~ [function invocation disallowed: console.error] | ||
~~~~~~~~~~~~~ [Calls to 'console.error' are not allowed.] | ||
console.something(); | ||
console.timeEnd(); |
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.
super small change, I prefer "provided" instead of "passed in"