Skip to content

Commit

Permalink
fix: validateArgs transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Oct 29, 2024
1 parent 2808040 commit 4a0b463
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/flash/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (!port || !filename) {

const verbose = !!argv.verbose;

let firmware: Buffer;
let firmware: Uint8Array;

const driver = new Driver(port, {
logConfig: verbose
Expand Down Expand Up @@ -99,7 +99,7 @@ async function flash() {
}

async function main() {
let rawFile: Buffer;
let rawFile: Uint8Array;
try {
const fullPath = path.isAbsolute(filename)
? filename
Expand Down
5 changes: 5 additions & 0 deletions packages/transformers/src/validateArgs/reason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export interface ExpectedBuffer {
type: "buffer";
}

export interface ExpectedUint8Array {
type: "uint8array";
}

export interface ExpectedClass {
type: "class";
name: string;
Expand Down Expand Up @@ -118,6 +122,7 @@ export type Reason =
| ExpectedObject
| ExpectedDate
| ExpectedBuffer
| ExpectedUint8Array
| ExpectedClass
| ExpectedNonPrimitive
| MissingObjectProperty
Expand Down
34 changes: 34 additions & 0 deletions packages/transformers/src/validateArgs/visitor-type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,38 @@ function visitBufferType(type: ts.ObjectType, visitorContext: VisitorContext) {
});
}

function visitUint8ArrayType(
type: ts.ObjectType,
visitorContext: VisitorContext,
) {
const name = VisitorTypeName.visitType(type, visitorContext, {
type: "type-check",
});
const f = visitorContext.factory;
return VisitorUtils.setFunctionIfNotExists(name, visitorContext, () => {
return VisitorUtils.createAssertionFunction(
f.createPrefixUnaryExpression(
ts.SyntaxKind.ExclamationToken,
f.createCallExpression(
f.createPropertyAccessExpression(
f.createCallExpression(
f.createIdentifier("require"),
undefined,
[f.createStringLiteral("@zwave-js/shared/safe")],
),
f.createIdentifier("isUint8Array"),
),
undefined,
[VisitorUtils.objectIdentifier],
),
),
{ type: "uint8array" },
name,
visitorContext,
);
});
}

function visitClassType(type: ts.ObjectType, visitorContext: VisitorContext) {
const f = visitorContext.factory;
const name = VisitorTypeName.visitType(type, visitorContext, {
Expand Down Expand Up @@ -687,6 +719,8 @@ function visitObjectType(type: ts.ObjectType, visitorContext: VisitorContext) {
return visitDateType(type, visitorContext);
} else if (VisitorUtils.checkIsNodeBuffer(type)) {
return visitBufferType(type, visitorContext);
} else if (VisitorUtils.checkIsUint8Array(type)) {
return visitUint8ArrayType(type, visitorContext);
} else if (VisitorUtils.checkIsIgnoredIntrinsic(type)) {
// This is an intrinsic type we can't check properly, so we just ignore it.
return VisitorUtils.getIgnoredTypeFunction(visitorContext);
Expand Down
2 changes: 2 additions & 0 deletions packages/transformers/src/validateArgs/visitor-type-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ function visitObjectType(
return visitTupleObjectType(type, visitorContext, mode);
} else if (checkIsNodeBuffer(type)) {
return "_buffer";
} else if (VisitorUtils.checkIsUint8Array(type)) {
return "_uint8array";
} else if (
visitorContext.checker.getIndexTypeOfType(type, ts.IndexKind.Number)
) {
Expand Down
12 changes: 12 additions & 0 deletions packages/transformers/src/validateArgs/visitor-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ export function checkIsNodeBuffer(type: ts.ObjectType): boolean {
);
}

export function checkIsUint8Array(type: ts.ObjectType): boolean {
return (
type.symbol !== undefined
&& type.symbol.valueDeclaration !== undefined
&& type.symbol.name === "Uint8Array"
&& !!(
ts.getCombinedModifierFlags(type.symbol.valueDeclaration)
& ts.ModifierFlags.Ambient
)
);
}

export function checkIsIgnoredIntrinsic(type: ts.ObjectType): boolean {
return (
type.symbol !== undefined
Expand Down
35 changes: 35 additions & 0 deletions packages/transformers/test/fixtures/testUint8Array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable no-restricted-globals */
import { validateArgs } from "@zwave-js/transformers";
import assert from "node:assert";

class Test {
@validateArgs()
foo(arg1: Uint8Array): 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(new Uint8Array());
test.foo(Uint8Array.from([0xaa, 0xbb, 0xcc]));
// These should also not throw
test.foo(Buffer.alloc(0));

// These should throw
assert.throws(
// @ts-expect-error
() => test.foo("string"),
/arg1 is not a Uint8Array/,
);
assert.throws(
// @ts-expect-error
() => test.foo(undefined),
/arg1 is not a Uint8Array/,
);
assert.throws(
// @ts-expect-error
() => test.foo({ type: "Buffer", data: [170, 187, 204] }),
/arg1 is not a Uint8Array/,
);

0 comments on commit 4a0b463

Please sign in to comment.