-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rule to handle sessionStorage and localStorage
- Loading branch information
1 parent
4d44ea0
commit 49b8603
Showing
7 changed files
with
114 additions
and
0 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,13 @@ | ||
--- | ||
"@total-typescript/ts-reset": minor | ||
--- | ||
|
||
Added a rule, `/session`, to make sessionStorage and localStorage safer. | ||
|
||
```ts | ||
// Is now typed as `unknown`, not `any`! | ||
localStorage.a; | ||
|
||
// Is now typed as `unknown`, not `any`! | ||
sessionStorage.abc; | ||
``` |
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,5 @@ | ||
--- | ||
"@total-typescript/ts-reset": minor | ||
--- | ||
|
||
Added a `/dom` entrypoint to allow users to import DOM-only rules. |
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
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,2 @@ | ||
/// <reference path="recommended.d.ts" /> | ||
/// <reference path="storage.d.ts" /> |
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,3 @@ | ||
interface Storage { | ||
[name: string & {}]: unknown; | ||
} |
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,47 @@ | ||
import { doNotExecute, Equal, Expect } from "./utils"; | ||
|
||
// Ensure that arbitrary access to localStorage is not allowed | ||
doNotExecute(() => { | ||
type test = Expect<Equal<typeof localStorage.a, unknown>>; | ||
|
||
// @ts-expect-error | ||
localStorage.a.b.c; | ||
}); | ||
|
||
// Ensure that the type of localStorage remains correct | ||
doNotExecute(() => { | ||
type tests = [ | ||
Expect<Equal<typeof localStorage.getItem, (key: string) => string | null>>, | ||
Expect< | ||
Equal<typeof localStorage.setItem, (key: string, value: string) => void> | ||
>, | ||
Expect<Equal<typeof localStorage.removeItem, (key: string) => void>>, | ||
Expect<Equal<typeof localStorage.clear, () => void>>, | ||
Expect<Equal<typeof localStorage.key, (index: number) => string | null>>, | ||
Expect<Equal<typeof localStorage.length, number>>, | ||
]; | ||
}); | ||
|
||
// Ensure that arbitrary access to sessionStorage is not allowed | ||
doNotExecute(() => { | ||
type test = Expect<Equal<typeof sessionStorage.a, unknown>>; | ||
|
||
// @ts-expect-error | ||
sessionStorage.a.b.c; | ||
}); | ||
|
||
// Ensure that the type of sessionStorage remains correct | ||
doNotExecute(() => { | ||
type tests = [ | ||
Expect< | ||
Equal<typeof sessionStorage.getItem, (key: string) => string | null> | ||
>, | ||
Expect< | ||
Equal<typeof sessionStorage.setItem, (key: string, value: string) => void> | ||
>, | ||
Expect<Equal<typeof sessionStorage.removeItem, (key: string) => void>>, | ||
Expect<Equal<typeof sessionStorage.clear, () => void>>, | ||
Expect<Equal<typeof sessionStorage.key, (index: number) => string | null>>, | ||
Expect<Equal<typeof sessionStorage.length, number>>, | ||
]; | ||
}); |