Skip to content
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

Add rule to disallow referencing DOM globals #181

Closed
ajafff opened this issue Apr 10, 2018 · 0 comments
Closed

Add rule to disallow referencing DOM globals #181

ajafff opened this issue Apr 10, 2018 · 0 comments
Assignees

Comments

@ajafff
Copy link
Member

ajafff commented Apr 10, 2018

It's a common mistake to reference DOM globals declared in lib.dom.d.ts:

function onEvent(e: Event) {
    event.target; // intended to use `e` instead
}
  • Identifier is used in the value domain (tsutils.getUsageDomain)
  • Identifier is declared in lib.dom.d.ts
  • exclude Identifier if type has a construct signature to allow for new Event(...), Event.AT_TARGET and e instanceof Event
    • maybe just exclude all Identifiers starting with an uppercase letter
  • exclude certain Identifiers:
    • document, window, navigator
    • alert, confirm, prompt (should be banned by another rule)
    • (set|clear)(Timeout|Interval|Immediate)
    • atob and btoa
    • console
    • (session|local)Storage
    • indexedDB
    • (request|cancel)AnimationFrame
    • URL, CSS
    • print
    • and probably many others once users start complaining
  • fixer could simply prepend window., but is this really desired? The code will work the same but the lint failure is gone afterwards ...
  • optimizations:
    • only execute this rule if lib.dom.d.ts is included in the program
    • looking up the the SourceFile of every declaration is expensive
      • only consider Symbols with ts.SymbolFlags.Variable or ts.SymbolFlags.Function
      • only consider ambient declarations (declare var and declare function)

Rule name: no-restricted-dom-globals?

Ref: palantir/tslint#3824 which imposes the burden on the user to configure every possible global variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant