forked from microsoft/tslint-microsoft-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add allow-single-argument option and check argument types (fixes micr…
- Loading branch information
1 parent
082cb68
commit cd70f46
Showing
8 changed files
with
277 additions
and
31 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
73 changes: 73 additions & 0 deletions
73
tests/prefer-array-literal/allow-single-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,73 @@ | ||
let a: string[]; | ||
|
||
let b: Array<string> = []; | ||
~~~~~~~~~~~~~ [type % ("Array<string>")] | ||
|
||
interface C { | ||
myArray: Array<string>; | ||
~~~~~~~~~~~~~ [type % ("Array<string>")] | ||
} | ||
|
||
var d: Array<string>; | ||
~~~~~~~~~~~~~ [type % ("Array<string>")] | ||
|
||
function e(param: Array<number>) { } | ||
~~~~~~~~~~~~~ [type % ("Array<number>")] | ||
|
||
var f = new Array(); | ||
~~~~~~~~~~~ [constructor % ("new Array()")] | ||
|
||
var g = new Array(4, 5); | ||
~~~~~~~~~~~~~~~ [constructor % ("new Array(4, 5)")] | ||
|
||
var h = new Array(4); | ||
|
||
var i = Array(2); | ||
|
||
var j = new Array; | ||
~~~~~~~~~ [constructor % ("new Array")] | ||
|
||
// calls to Array function/constructor on global objects is forbidden | ||
var nc1 = window.Array(1); | ||
var nc2 = global.Array(1, 2); | ||
~~~~~~~~~~~~~~~~~~ [function % ("global.Array(1, 2)")] | ||
var nc3 = globalThis.Array('3'); | ||
~~~~~~~~~~~~~~~~~~~~~ [single-argument % ("function","globalThis.Array('3')")] | ||
|
||
var nn1 = new window.Array(1); | ||
var nn2 = new global.Array(1, 2); | ||
~~~~~~~~~~~~~~~~~~~~~~ [constructor % ("new global.Array(1, 2)")] | ||
var nn3 = new globalThis.Array('3'); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ [single-argument % ("constructor", "new globalThis.Array('3')")] | ||
|
||
// 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 % ("Array<number>")] | ||
declare var unionF: number | (() => number); | ||
|
||
const t1 = Array(num); | ||
const t2 = Array(str); | ||
~~~~~~~~~~ [single-argument % ("function", "Array(str)")] | ||
const t3 = Array(unionA); | ||
~~~~~~~~~~~~~ [single-argument % ("function", "Array(unionA)")] | ||
const t3 = Array(unionF); | ||
~~~~~~~~~~~~~ [single-argument % ("function", "Array(unionF)")] | ||
const t4 = Array(num + 1); | ||
const t5 = Array(str + 1); | ||
~~~~~~~~~~~~~~ [single-argument % ("function", "Array(str + 1)")] | ||
const t6 = Array(10 + 1); | ||
const t7 = Array(10 + '1'); | ||
~~~~~~~~~~~~~~~ [single-argument % ("function", "Array(10 + '1')")] | ||
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 | ||
|
||
[type]: Replace generic-typed Array with array literal: %s | ||
[constructor]: Replace Array constructor with an array literal: %s | ||
[function]: Replace Array function with an array literal: %s | ||
[single-argument]: To create an array of given length you should use non-negative integer. Otherwise replace Array %s with an array literal: %s |
5 changes: 5 additions & 0 deletions
5
tests/prefer-array-literal/allow-single-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-single-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-single-argument": true}] | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/prefer-array-literal/allow-single-argument/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,66 @@ | ||
let a: string[]; | ||
|
||
let b: Array<string> = []; | ||
~~~~~~~~~~~~~ [type % ("Array<string>")] | ||
|
||
interface C { | ||
myArray: Array<string>; | ||
~~~~~~~~~~~~~ [type % ("Array<string>")] | ||
} | ||
|
||
var d: Array<string>; | ||
~~~~~~~~~~~~~ [type % ("Array<string>")] | ||
|
||
function e(param: Array<number>) { } | ||
~~~~~~~~~~~~~ [type % ("Array<number>")] | ||
|
||
var f = new Array(); | ||
~~~~~~~~~~~ [constructor % ("new Array()")] | ||
|
||
var g = new Array(4, 5); | ||
~~~~~~~~~~~~~~~ [constructor % ("new Array(4, 5)")] | ||
|
||
var h = new Array(4); | ||
|
||
var i = Array(2); | ||
|
||
var j = new Array; | ||
~~~~~~~~~ [constructor % ("new Array")] | ||
|
||
// calls to Array function/constructor on global objects is forbidden | ||
var nc1 = window.Array(1); | ||
var nc2 = global.Array(1, 2); | ||
~~~~~~~~~~~~~~~~~~ [function % ("global.Array(1, 2)")] | ||
var nc3 = globalThis.Array('3'); // no error - limitation of untyped rule | ||
|
||
var nn1 = new window.Array(1); | ||
var nn2 = new global.Array(1, 2); | ||
~~~~~~~~~~~~~~~~~~~~~~ [constructor % ("new global.Array(1, 2)")] | ||
var nn3 = new globalThis.Array('3'); // no error - limitation of untyped rule | ||
|
||
// 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 % ("Array<number>")] | ||
declare var unionF: number | (() => number); | ||
|
||
// no errors below - limitation of untyped rule | ||
const t1 = Array(num); | ||
const t2 = Array(str); | ||
const t3 = Array(unionA); | ||
const t3 = Array(unionF); | ||
const t4 = Array(num + 1); | ||
const t5 = Array(str + 1); | ||
const t6 = Array(10 + 1); | ||
const t7 = Array(10 + '1'); | ||
const t8 = Array(1.5); | ||
const t9 = Array(-1); | ||
const t10 = Array(-num); | ||
|
||
[type]: Replace generic-typed Array with array literal: %s | ||
[constructor]: Replace Array constructor with an array literal: %s | ||
[function]: Replace Array function with an array literal: %s |
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-single-argument": true}] | ||
} | ||
} |
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