-
Notifications
You must be signed in to change notification settings - Fork 889
Rule to prevent certain keywords being used as variable names #735
Conversation
@@ -32,8 +32,8 @@ | |||
"./rules/noConditionalAssignmentRule.ts", | |||
"./rules/noConsecutiveBlankLinesRule.ts", | |||
"./rules/noConsoleRule.ts", | |||
"./rules/noConstructRule.ts", |
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.
Not a negative diff, simply correcting ordering
looks like this would address microsoft/TypeScript#3081 |
import * as Lint from "../lint"; | ||
import * as ts from "typescript"; | ||
|
||
const BAD_NAMES = [ "any", "Number", "number", "String", "string", "Boolean", "boolean", "undefined" ]; |
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.
Is this a comprehensive list of keywords/types that tsc will let you redefine? It looks good to me, just want to make sure there isn't anything missing or extraneous
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.
@jkillian started with this to kick off discussion.
Arguably this list could include any ambient variable from lib.d.ts
, like Number
, String
and Boolean
The idea behind this rule is to increase the likelihood of writing code that is unambiguously understood by as many as possible. You could easily chuck RegExp
, Array
and other core interfaces that have real class equivalents in there too...
But then you could probably well argue it goes too far to include event
for example.
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.
Gotchya, I'm fine with it like this for this PR. Changing the list is easy and essentially backwards-compatible, so that can be discussed down the road.
Looks good other than my comments above! |
Also, perhaps there should be test cases for reassignments? I know
|
@jkillian agree that is totally bizarre but I intentionally didn't include such things in this rule because arguably you could/should make those definitions in |
@jkillian fixed the filename issue. Good catch, but per my comment can you tell me why the build didn't fail? Seems like that entry in |
@myitcv Perhaps the reason to not make them const is to reflect their JS behavior maybe? Since in JS you can theoretically redefine NaN, TS doesn't prevent you from doing that. Maybe there's some TS library out there that depends on redefining NaN haha. |
Could well be. Sounds like a candidate for another rule.... agree? |
As far as the build not failing, my guess is that your tests just weren't running at all (and also then not failing at all). My inclination is that it can be in this same rule - it seems reasonable that a rule that doesn't let you declare variables with the name |
Quite possibly, but the compiler/grunt should probably error if a listed file does not exist, that was more my point. Which then leads you to working out "oh my tests aren't running"...
Gave this some more thought last night. By preventing assignments like Consider the following: // marker 1
name = "test";
export function DoIt(): void {
// marker 2
let name = 5;
console.log(name);
} This is valid because of the declaration of declare var name: string; Couple of questions (at least) come out of this:
The In any case, I think preventing the assignment to The intention of this rule should I think be to prevent variables being named with known 'keywords'. The definition of 'keyword' is clearly up for debate with so many types ( |
Sounds good to me, I'm fine with leaving this rule as is then. As far as the strangeness with grunt and a lack of errors, I'll try to get that changed when we move to the test system proposed in #620 |
Can you just do two tweaks before this gets merged in?
|
@jkillian - done. The formatting changes in |
Rule to prevent certain keywords being used as variable names
Prevents nasties like:
Comments/thoughts welcomed.