-
-
Notifications
You must be signed in to change notification settings - Fork 630
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
fix: argument validation of parameters with defaults, Map and Set #7435
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
import { validateArgs } from "@zwave-js/transformers"; | ||
import assert from "node:assert"; | ||
|
||
class Test { | ||
@validateArgs() | ||
foo(arg1: number = 5): void { | ||
arg1; | ||
return void 0; | ||
} | ||
|
||
@validateArgs() | ||
bar(arg1: number | string = 5): void { | ||
arg1; | ||
Check warning Code scanning / CodeQL Expression has no effect Warning test
This expression has no effect.
|
||
return void 0; | ||
} | ||
} | ||
|
||
const test = new Test(); | ||
// These should not throw | ||
test.foo(); | ||
test.foo(1); | ||
test.foo(undefined); | ||
|
||
test.bar(); | ||
test.bar(1); | ||
test.bar("str"); | ||
test.bar(undefined); | ||
|
||
// These should throw | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.foo(true), | ||
/optional parameter arg1 to be a number, got true/, | ||
); | ||
|
||
assert.throws( | ||
// @ts-expect-error | ||
() => test.bar(true), | ||
/optional parameter arg1 to be one of (number \| string|string \| number), got true/, | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
import { validateArgs } from "@zwave-js/transformers"; | ||
import assert from "node:assert"; | ||
|
||
class Test { | ||
@validateArgs() | ||
map(arg1: Map<any, any>): void { | ||
arg1; | ||
Check warning Code scanning / CodeQL Expression has no effect Warning test
This expression has no effect.
|
||
return void 0; | ||
} | ||
|
||
@validateArgs() | ||
readonlyMap(arg1: ReadonlyMap<any, any>): void { | ||
arg1; | ||
Check warning Code scanning / CodeQL Expression has no effect Warning test
This expression has no effect.
|
||
return void 0; | ||
} | ||
|
||
@validateArgs() | ||
set(arg1: Set<any>): void { | ||
arg1; | ||
Check warning Code scanning / CodeQL Expression has no effect Warning test
This expression has no effect.
|
||
return void 0; | ||
} | ||
|
||
@validateArgs() | ||
readonlySet(arg1: ReadonlySet<any>): void { | ||
arg1; | ||
Check warning Code scanning / CodeQL Expression has no effect Warning test
This expression has no effect.
|
||
return void 0; | ||
} | ||
} | ||
|
||
const test = new Test(); | ||
// These should not throw | ||
test.map(new Map()); | ||
test.readonlyMap(new Map()); // readonly does not exist at runtime | ||
test.set(new Set()); | ||
test.readonlySet(new Set()); // readonly does not exist at runtime | ||
|
||
// These should throw | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.map({}), | ||
/arg1 to be an instance of class Map, got object/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.readonlyMap({}), | ||
/arg1 to be an instance of class Map, got object/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.map("string"), | ||
/arg1 to be an instance of class Map, got string "string"/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.readonlyMap("string"), | ||
/arg1 to be an instance of class Map, got string "string"/, | ||
); | ||
|
||
assert.throws( | ||
// @ts-expect-error | ||
() => test.set({}), | ||
/arg1 to be an instance of class Set, got object/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.readonlySet({}), | ||
/arg1 to be an instance of class Set, got object/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.set("string"), | ||
/arg1 to be an instance of class Set, got string "string"/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.readonlySet("string"), | ||
/arg1 to be an instance of class Set, got string "string"/, | ||
); |
Check warning
Code scanning / CodeQL
Expression has no effect Warning test