-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
165 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Disallows the use of the `window` object. | ||
|
||
Using the `window` global is deprecated and scheduled for removal in Deno 2.0. | ||
Deno does not have a window and `typeof window === "undefined"` is often used to | ||
tell if the code is running in the browser. | ||
|
||
### Invalid: | ||
|
||
```typescript | ||
const a = await window.fetch("https://deno.land"); | ||
|
||
const b = window.Deno.metrics(); | ||
console.log(window); | ||
|
||
window.addEventListener("load", () => { | ||
console.log("Loaded."); | ||
}); | ||
``` | ||
|
||
### Valid: | ||
|
||
```typescript | ||
const a1 = await fetch("https://deno.land"); | ||
const a2 = await globalThis.fetch("https://deno.land"); | ||
const a3 = await self.fetch("https://deno.land"); | ||
|
||
const b1 = Deno.metrics(); | ||
const b2 = globalThis.Deno.metrics(); | ||
const b3 = self.Deno.metrics(); | ||
console.log(globalThis); | ||
|
||
addEventListener("load", () => { | ||
console.log("Loaded."); | ||
}); | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2020-2021 the Deno authors. All rights reserved. MIT license. | ||
use super::Context; | ||
use super::LintRule; | ||
use crate::handler::Handler; | ||
use crate::handler::Traverse; | ||
use crate::Program; | ||
|
||
use deno_ast::view as ast_view; | ||
use deno_ast::SourceRanged; | ||
use if_chain::if_chain; | ||
|
||
#[derive(Debug)] | ||
pub struct NoWindow; | ||
|
||
const CODE: &str = "no-window"; | ||
const MESSAGE: &str = | ||
"window is deprecated and scheduled for removal in Deno 2.0"; | ||
const HINT: &str = "Instead, use `globalThis`"; | ||
|
||
impl LintRule for NoWindow { | ||
fn tags(&self) -> &'static [&'static str] { | ||
&["recommended"] | ||
} | ||
|
||
fn code(&self) -> &'static str { | ||
CODE | ||
} | ||
|
||
fn lint_program_with_ast_view( | ||
&self, | ||
context: &mut Context, | ||
program: Program<'_>, | ||
) { | ||
NoWindowGlobalHandler.traverse(program, context); | ||
} | ||
|
||
#[cfg(feature = "docs")] | ||
fn docs(&self) -> &'static str { | ||
include_str!("../../docs/rules/no_window_global.md") | ||
} | ||
} | ||
|
||
struct NoWindowGlobalHandler; | ||
|
||
impl Handler for NoWindowGlobalHandler { | ||
fn ident(&mut self, ident: &ast_view::Ident, ctx: &mut Context) { | ||
if_chain! { | ||
if ident.sym() == "window"; | ||
if ctx.scope().is_global(&ident.to_id()); | ||
then { | ||
ctx.add_diagnostic_with_hint( | ||
ident.range(), | ||
CODE, | ||
MESSAGE, | ||
HINT, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn no_window_valid() { | ||
assert_lint_ok! { | ||
NoWindow, | ||
"fetch();", | ||
"self.fetch();", | ||
"globalThis.fetch();", | ||
|
||
// `window` is shadowed | ||
"const window = 42; window.fetch();", | ||
r#"const window = 42; window["fetch"]();"#, | ||
r#"const window = 42; window[`fetch`]();"#, | ||
"const window = 42; window.alert();", | ||
r#"const window = 42; window["alert"]();"#, | ||
r#"const window = 42; window[`alert`]();"#, | ||
}; | ||
} | ||
|
||
#[test] | ||
fn no_window_invalid() { | ||
assert_lint_err! { | ||
NoWindow, | ||
MESSAGE, | ||
HINT, | ||
r#"window.fetch()"#: [ | ||
{ | ||
col: 0, | ||
} | ||
], | ||
r#"window["fetch"]()"#: [ | ||
{ | ||
col: 0, | ||
} | ||
], | ||
r#"window[`fetch`]()"#: [ | ||
{ | ||
col: 0, | ||
} | ||
], | ||
r#" | ||
function foo() { | ||
const window = 42; | ||
return window; | ||
} | ||
window; | ||
"#: [ | ||
{ | ||
col: 0, | ||
line: 6, | ||
} | ||
], | ||
}; | ||
} | ||
} |
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