-
Notifications
You must be signed in to change notification settings - Fork 887
Conversation
Thanks for your interest in palantir/tslint, @Zzzen! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request. |
src/rules/banDomGlobalRule.ts
Outdated
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { |
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 you add a rationale explaining why this rule is useful? It would be confusing as a newcomer to TSLint reading the rule page on palantir.github.io/tslint why this would be useful.
It should probably also recommend specifically not using the dom lib in your tsconfig if you're not writing a browser app.
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.
Haven't you check out New rule to forbid access to commonly-confused ambient globals? I write browser app primarily and have been bit by name
in lib.dom.d.ts
. Maybe I should rename this to forbid-ambient-globals
and provides options like
[
{
"file": "lib.dom.d.ts",
"variables": [
"event",
"name"
]
},
{
"file": "@types/jquery/index.d.ts",
"variables": [
"$"
]
}
]
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.
Definition file names are redundant since we cannot use a global declared in lib A if it is also declared and used in lib B.
@JoshuaKGoldberg any advice? thanks |
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.
This seems like a rather difficult problem. Very cool and absolutely a problem that needs to be dealt with, but needs some more thought into best implementation details.
Can you file a separate issue to discuss continue this conversation in the linked issue I absolutely didn't miss on the first readthrough?
src/rules/noRestrictedGlobalsRule.ts
Outdated
Disallowing usage of specific global variables can be useful if you want to allow | ||
a set of global variables by enabling an environment, but still want to disallow | ||
some of those. For instance, early Internet Explorer versions exposed the current | ||
DOM event as a global variable event, but using this variable has been considered |
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.
Nit: escape the "event" in "global variable event" with code to make it a little more clear.
global variable \`event\`, but using
src/rules/noRestrictedGlobalsRule.ts
Outdated
a set of global variables by enabling an environment, but still want to disallow | ||
some of those. For instance, early Internet Explorer versions exposed the current | ||
DOM event as a global variable event, but using this variable has been considered | ||
as a bad practice for a long time. Restricting this will make sure this variable |
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.
Nit: just "considered a bad practice"
src/rules/noRestrictedGlobalsRule.ts
Outdated
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING(name: string) { | ||
return `Unexpected global variable '${name}'. Use local parameter instead.`; |
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.
Or a local variable, right?
Use a local parameter or variable instead.
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.
Updated. thanks
src/rules/noRestrictedGlobalsRule.ts
Outdated
return; | ||
} | ||
|
||
const declaredInLibDom = declarations.some((decl) => decl.getSourceFile().fileName.endsWith(".d.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.
What if you declare something in an externals.d.ts
file, or define your own lib.dom.d.ts
with either that name or my-lib.d.ts
?
Or what about a project that uses namespace
s and has a const name = "HA!";
somewhere?
Maybe the rule should get the list of files TS is compiling with (can we do that?) along with its compiler options, and add lib.d.ts
-like files as per lib
and --noLib
compiler settings?
Maybe the rule should keep a default list of regular expressions to match names against, like lib*.d.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.
IMO, custom definition files should be considered the same as official lib.dom.d.ts
. There is no need to get compiler options.
@Zzzen this is awesome! The ability to add code examples to the documentation just landed on master--would you mind adding some for this rule? See the |
|
Hey all, any word on when this PR can be merged? I've been bitten by this issue a handful of times and would love to use this rule. Thanks! |
Please take a look when you have time. @JoshuaKGoldberg |
Heya - I can take a look and give feedback, but I'm not actually an employee of Palantir or on the TSLint maintenance team. So you might want to ping one of them. Sorry! |
😂 |
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.
Has the "Some more test cases to consider..." comment been addressed?
Would love to see this in too, as I'm currently re-bundling updated lib declaration files just to ban a few of these global variables (which is very cumbersome)... Just to confirm, this is blocked only on the author providing more test cases, but the behavior/naming is acceptable? |
Edit 1/2/2019 - realized some more changes should be made, sorry! |
This would be very useful, is there anything that can be done to move it along? |
🤔 I guess all requested changes already have been addressed? |
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.
Had time to take a deeper look - a few things to change.
This should still be reviewed by a reviewer from palantirtech.
Code updated. You're really helpful. @JoshuaKGoldberg |
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.
nice work @Zzzen
return; | ||
} | ||
|
||
const isAmbientGlobal = declarations.some((decl) => decl.getSourceFile().isDeclarationFile); |
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.
This errors too often if the declaration comes from a declaration file that is not lib.dom.d.ts. Consider the case of adone's types in Definitely Typed:
// @Filename: adone-tests.ts
namespace eventsTests {
namespace EventEmitter {
namespace static {
const a: number = adone.event.Emitter.listenerCount(new adone.event.Emitter(), "event");
const b: number = adone.event.Emitter.defaultMaxListeners;
}
// @Filename: events.d.ts
declare namespace adone {
namespace event {
class Emitter {
static listenerCount(emitter: Emitter, event: string | symbol): number;
static defaultMaxListeners: number;
// ...
You will get bogus errors on event
.
PR checklist
Overview of change:
prevents accidental access to variables that are declared in
lib.dom.d.ts
.Is there anything you'd like reviewers to focus on?
CHANGELOG.md entry: