This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve prefer-array-literal rule (#862)
* Use only tslint --test for prefer-array-literal rule * Restrict only global objects with Array (fixes #856) * Add allow-single-argument option and check argument types (fixes #822) * Update docs and metadata for prefer-array-literal rule * Apply suggestions related to wording in README.md Co-Authored-By: Josh Goldberg <[email protected]> * Change new option name to allow-size-argument * Use Set for restricted namespaces. Add `self` to restrictions * Fix error message for incorrect size argument * Handle array spread in argument position * Drop node text from all error messages * Handle potential exceptions from TS * Add comment about additional check for SpreadElement
- Loading branch information
1 parent
7ac0019
commit f85d5f6
Showing
12 changed files
with
407 additions
and
138 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
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 was deleted.
Oops, something went wrong.
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
82 changes: 82 additions & 0 deletions
82
tests/prefer-array-literal/allow-size-argument-typed/test.ts.lint
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,82 @@ | ||
let a: string[]; | ||
|
||
let b: Array<string> = []; | ||
~~~~~~~~~~~~~ [type] | ||
|
||
interface C { | ||
myArray: Array<string>; | ||
~~~~~~~~~~~~~ [type] | ||
} | ||
|
||
var d: Array<string>; | ||
~~~~~~~~~~~~~ [type] | ||
|
||
function e(param: Array<number>) { } | ||
~~~~~~~~~~~~~ [type] | ||
|
||
var f = new Array(); | ||
~~~~~~~~~~~ [constructor] | ||
|
||
var g = new Array(4, 5); | ||
~~~~~~~~~~~~~~~ [constructor] | ||
|
||
var h = new Array(4); | ||
|
||
var i = Array(2); | ||
|
||
var j = new Array; | ||
~~~~~~~~~ [constructor] | ||
|
||
// calls to Array function/constructor on global objects is forbidden | ||
var nc1 = window.Array(1); | ||
var nc2 = global.Array(1, 2); | ||
~~~~~~~~~~~~~~~~~~ [function] | ||
var nc3 = globalThis.Array('3'); | ||
~~~~~~~~~~~~~~~~~~~~~ [size-argument-function] | ||
|
||
var nn1 = new window.Array(1); | ||
var nn2 = new global.Array(1, 2); | ||
~~~~~~~~~~~~~~~~~~~~~~ [constructor] | ||
var nn3 = new globalThis.Array('3'); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ [size-argument-constructor] | ||
|
||
// calls to Array function/constructor from namespaces are valid | ||
import { Types } from 'mongoose'; | ||
export const foo: Types.Array<number> = new Types.Array(); | ||
|
||
declare var num: number; | ||
declare var str: string; | ||
declare var unionA: number | Array<number>; | ||
~~~~~~~~~~~~~ [type] | ||
declare var unionF: number | (() => number); | ||
|
||
const t1 = Array(num); | ||
const t2 = Array(str); | ||
~~~~~~~~~~ [size-argument-function] | ||
const t3 = Array(unionA); | ||
~~~~~~~~~~~~~ [size-argument-function] | ||
const t3 = Array(unionF); | ||
~~~~~~~~~~~~~ [size-argument-function] | ||
const t4 = Array(num + 1); | ||
const t5 = Array(str + 1); | ||
~~~~~~~~~~~~~~ [size-argument-function] | ||
const t6 = Array(10 + 1); | ||
const t7 = Array(10 + '1'); | ||
~~~~~~~~~~~~~~~ [size-argument-function] | ||
const t8 = Array(1.5); // no error - limitation of typed rule | ||
const t9 = Array(-1); // no error - limitation of typed rule | ||
const t10 = Array(-num); // no error - limitation of typed rule | ||
|
||
declare var arrS: number[]; | ||
declare var arrN: number[]; | ||
|
||
const t11 = Array(...arrS); | ||
~~~~~~~~~~~~~~ [size-argument-function] | ||
const t12 = Array(...arrN); | ||
~~~~~~~~~~~~~~ [size-argument-function] | ||
|
||
[type]: Replace generic-typed Array with array literal. | ||
[constructor]: Replace Array constructor with an array literal. | ||
[function]: Replace Array function with an array literal. | ||
[size-argument-constructor]: To create an array of a given length you should use non-negative integer. Otherwise replace Array constructor with an array literal. | ||
[size-argument-function]: To create an array of a given length you should use non-negative integer. Otherwise replace Array function with an array literal. |
5 changes: 5 additions & 0 deletions
5
tests/prefer-array-literal/allow-size-argument-typed/tsconfig.json
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/prefer-array-literal/allow-size-argument-typed/tslint.json
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 @@ | ||
{ | ||
"rules": { | ||
"prefer-array-literal": [true, {"allow-size-argument": true}] | ||
} | ||
} |
Oops, something went wrong.