Skip to content

Commit

Permalink
Fixed multiple Static Analysis issues raised by Sonar Cloud (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvcabral authored May 2, 2024
1 parent e7a9abb commit 304307f
Show file tree
Hide file tree
Showing 39 changed files with 211 additions and 212 deletions.
20 changes: 9 additions & 11 deletions src/worker/brsTypes/BrsType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Boxable } from "./Boxing";
import { RoString } from "./components/RoString";
import { Int32 } from "./Int32";
import { Float } from "./Float";
import { roBoolean } from "./components/RoBoolean";
import { roInvalid } from "./components/RoInvalid";
import { RoBoolean } from "./components/RoBoolean";
import { RoInvalid } from "./components/RoInvalid";

/** Set of values supported in BrightScript. */
export enum ValueKind {
Expand Down Expand Up @@ -250,8 +250,8 @@ export class BrsBoolean implements BrsValue, Comparable, Boxable {
return this.value;
}

static False = new BrsBoolean(false);
static True = new BrsBoolean(true);
static readonly False = new BrsBoolean(false);
static readonly True = new BrsBoolean(true);
static from(value: boolean) {
return value ? BrsBoolean.True : BrsBoolean.False;
}
Expand Down Expand Up @@ -282,7 +282,7 @@ export class BrsBoolean implements BrsValue, Comparable, Boxable {
}

box() {
return new roBoolean(this);
return new RoBoolean(this);
}

/**
Expand Down Expand Up @@ -324,22 +324,20 @@ export class BrsBoolean implements BrsValue, Comparable, Boxable {
/** Internal representation of the BrightScript `invalid` value. */
export class BrsInvalid implements BrsValue, Comparable, Boxable {
readonly kind = ValueKind.Invalid;
static Instance = new BrsInvalid();
static readonly Instance = new BrsInvalid();

lessThan(other: BrsType): BrsBoolean {
// invalid isn't less than anything
// TODO: Validate on a Roku
return BrsBoolean.False;
}

greaterThan(other: BrsType): BrsBoolean {
// but isn't greater than anything either
// TODO: Validate on a Roku
return BrsBoolean.False;
}

equalTo(other: BrsType): BrsBoolean {
if (other.kind === ValueKind.Invalid || other instanceof roInvalid) {
if (other.kind === ValueKind.Invalid || other instanceof RoInvalid) {
return BrsBoolean.True;
}
return BrsBoolean.False;
Expand All @@ -354,14 +352,14 @@ export class BrsInvalid implements BrsValue, Comparable, Boxable {
}

box() {
return new roInvalid();
return new RoInvalid();
}
}

/** Internal representation of uninitialized BrightScript variables. */
export class Uninitialized implements BrsValue, Comparable {
readonly kind = ValueKind.Uninitialized;
static Instance = new Uninitialized();
static readonly Instance = new Uninitialized();

lessThan(other: BrsType): BrsBoolean {
// uninitialized values aren't less than anything
Expand Down
2 changes: 1 addition & 1 deletion src/worker/brsTypes/Callable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class StdlibArgument implements Argument {
}

/** A fake location exists only within the BRS runtime. */
static InternalLocation = {
static readonly InternalLocation = {
file: "(stdlib)",
start: { line: -1, column: -1 },
end: { line: -1, column: -1 },
Expand Down
7 changes: 2 additions & 5 deletions src/worker/brsTypes/Double.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BrsNumber, Numeric } from "./BrsNumber";
import { Int32 } from "./Int32";
import { Int64 } from "./Int64";
import { Float } from "./Float";
import { roDouble } from "./components/RoDouble";
import { RoDouble } from "./components/RoDouble";
import { Boxable } from "./Boxing";
import Long from "long";

Expand Down Expand Up @@ -55,7 +55,6 @@ export class Double implements Numeric, Comparable, Boxable {
subtract(rhs: BrsNumber): BrsNumber {
switch (rhs.kind) {
case ValueKind.Int64:
// TODO: Confirm that (double) - (int64) -> (double)
return new Double(this.getValue() - rhs.getValue().toNumber());
case ValueKind.Int32:
case ValueKind.Float:
Expand All @@ -67,7 +66,6 @@ export class Double implements Numeric, Comparable, Boxable {
multiply(rhs: BrsNumber): BrsNumber {
switch (rhs.kind) {
case ValueKind.Int64:
// TODO: Confirm that (double) - (int64) -> (double)
return new Double(this.getValue() * rhs.getValue().toNumber());
case ValueKind.Int32:
case ValueKind.Float:
Expand All @@ -79,7 +77,6 @@ export class Double implements Numeric, Comparable, Boxable {
divide(rhs: BrsNumber): Float | Double {
switch (rhs.kind) {
case ValueKind.Int64:
// TODO: Confirm that (double) - (int64) -> (double)
return new Double(this.getValue() / rhs.getValue().toNumber());
case ValueKind.Int32:
case ValueKind.Float:
Expand Down Expand Up @@ -209,6 +206,6 @@ export class Double implements Numeric, Comparable, Boxable {
}

box() {
return new roDouble(this);
return new RoDouble(this);
}
}
8 changes: 2 additions & 6 deletions src/worker/brsTypes/Float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BrsNumber, Numeric } from "./BrsNumber";
import { Int32 } from "./Int32";
import { Double } from "./Double";
import { Int64 } from "./Int64";
import { roFloat } from "./components/RoFloat";
import { RoFloat } from "./components/RoFloat";
import Long from "long";

/**
Expand Down Expand Up @@ -50,7 +50,6 @@ export class Float implements Numeric, Comparable, Boxable {
add(rhs: BrsNumber): BrsNumber {
switch (rhs.kind) {
case ValueKind.Int64:
// TODO: Confirm that (double) + (int64) -> (double)
return new Float(this.getValue() + rhs.getValue().toNumber());
case ValueKind.Int32:
case ValueKind.Float:
Expand All @@ -63,7 +62,6 @@ export class Float implements Numeric, Comparable, Boxable {
subtract(rhs: BrsNumber): BrsNumber {
switch (rhs.kind) {
case ValueKind.Int64:
// TODO: Confirm that (float) - (int64) -> (float)
return new Float(this.getValue() - rhs.getValue().toNumber());
case ValueKind.Int32:
case ValueKind.Float:
Expand All @@ -76,7 +74,6 @@ export class Float implements Numeric, Comparable, Boxable {
multiply(rhs: BrsNumber): BrsNumber {
switch (rhs.kind) {
case ValueKind.Int64:
// TODO: Confirm that (float) * (int64) -> (float)
return new Float(this.getValue() * rhs.getValue().toNumber());
case ValueKind.Int32:
case ValueKind.Float:
Expand All @@ -89,7 +86,6 @@ export class Float implements Numeric, Comparable, Boxable {
divide(rhs: BrsNumber): Float | Double {
switch (rhs.kind) {
case ValueKind.Int64:
// TODO: Confirm that (float) / (int64) -> (float)
return new Float(this.getValue() / rhs.getValue().toNumber());
case ValueKind.Int32:
case ValueKind.Float:
Expand Down Expand Up @@ -221,6 +217,6 @@ export class Float implements Numeric, Comparable, Boxable {
}

box() {
return new roFloat(this);
return new RoFloat(this);
}
}
4 changes: 2 additions & 2 deletions src/worker/brsTypes/Int32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Boxable } from "./Boxing";
import { Float } from "./Float";
import { Double } from "./Double";
import { Int64 } from "./Int64";
import { roInt } from "./components/RoInt";
import { RoInt } from "./components/RoInt";
import Long from "long";

export class Int32 implements Numeric, Comparable, Boxable {
Expand Down Expand Up @@ -230,6 +230,6 @@ export class Int32 implements Numeric, Comparable, Boxable {
}

box() {
return new roInt(this);
return new RoInt(this);
}
}
4 changes: 2 additions & 2 deletions src/worker/brsTypes/Int64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ValueKind, Comparable } from "./BrsType";
import { Float } from "./Float";
import { Double } from "./Double";
import { Boxable } from "./Boxing";
import { roLongInteger } from "./components/RoLongInteger";
import { RoLongInteger } from "./components/RoLongInteger";

export class Int64 implements Numeric, Comparable, Boxable {
readonly kind = ValueKind.Int64;
Expand Down Expand Up @@ -231,6 +231,6 @@ export class Int64 implements Numeric, Comparable, Boxable {
}

box() {
return new roLongInteger(this);
return new RoLongInteger(this);
}
}
2 changes: 1 addition & 1 deletion src/worker/brsTypes/components/BrsComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class BrsComponent {
new BrsInterface(interfaceName, methods)
);

methods.forEach((m) => this.methods.set((m.name || "").toLowerCase(), m));
methods.forEach((m) => this.methods.set((m.name ?? "").toLowerCase(), m));
});
}

Expand Down
24 changes: 12 additions & 12 deletions src/worker/brsTypes/components/BrsObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ import { RoDeviceInfo } from "./RoDeviceInfo";
import { RoFileSystem } from "./RoFileSystem";
import { Interpreter } from "../../interpreter";
import { RoString } from "./RoString";
import { roBoolean } from "./RoBoolean";
import { roDouble } from "./RoDouble";
import { roFloat } from "./RoFloat";
import { roInt } from "./RoInt";
import { roLongInteger } from "./RoLongInteger";
import { RoBoolean } from "./RoBoolean";
import { RoDouble } from "./RoDouble";
import { RoFloat } from "./RoFloat";
import { RoInt } from "./RoInt";
import { RoLongInteger } from "./RoLongInteger";
import { Double } from "../Double";
import { Float } from "../Float";
import { Int32 } from "../Int32";
import { Int64 } from "../Int64";
import { roInvalid } from "./RoInvalid";
import { RoInvalid } from "./RoInvalid";
import { RoFunction } from "./RoFunction";
import { Callable } from "../Callable";

Expand Down Expand Up @@ -74,11 +74,11 @@ export const BrsObjects = new Map<string, Function>([
new RoRegex(expression, flags),
],
["rostring", (interpreter: Interpreter) => new RoString()],
["roboolean", (interpreter: Interpreter, literal: BrsBoolean) => new roBoolean(literal)],
["rodouble", (interpreter: Interpreter, literal: Double) => new roDouble(literal)],
["rofloat", (interpreter: Interpreter, literal: Float) => new roFloat(literal)],
["roint", (interpreter: Interpreter, literal: Int32) => new roInt(literal)],
["rolonginteger", (interpreter: Interpreter, literal: Int64) => new roLongInteger(literal)],
["roboolean", (interpreter: Interpreter, literal: BrsBoolean) => new RoBoolean(literal)],
["rodouble", (interpreter: Interpreter, literal: Double) => new RoDouble(literal)],
["rofloat", (interpreter: Interpreter, literal: Float) => new RoFloat(literal)],
["roint", (interpreter: Interpreter, literal: Int32) => new RoInt(literal)],
["rolonginteger", (interpreter: Interpreter, literal: Int64) => new RoLongInteger(literal)],
["rofunction", (interpreter: Interpreter, sub: Callable) => new RoFunction(sub)],
["ropath", (interpreter: Interpreter, path: BrsString) => new RoPath(path)],
[
Expand Down Expand Up @@ -126,5 +126,5 @@ export const BrsObjects = new Map<string, Function>([
],
["roxmlelement", (interpreter: Interpreter) => new RoXMLElement()],
["rourltransfer", (interpreter: Interpreter) => new RoURLTransfer(interpreter)],
["roinvalid", (interpreter: Interpreter) => new roInvalid()],
["roinvalid", (interpreter: Interpreter) => new RoInvalid()],
]);
6 changes: 4 additions & 2 deletions src/worker/brsTypes/components/RoAssociativeArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,11 @@ export class RoAssociativeArray extends BrsComponent implements BrsValue, BrsIte
args: [new StdlibArgument("obj", ValueKind.Object)],
returns: ValueKind.Void,
},
impl: (_: Interpreter, obj: BrsType) => {
impl: (interpreter: Interpreter, obj: BrsComponent) => {
if (!(obj instanceof RoAssociativeArray)) {
// TODO: validate against RBI
interpreter.stderr.write(
`warning,BRIGHTSCRIPT: ERROR: roAssociativeArray.Append: invalid parameter type ${obj.getComponentName()}: ${interpreter.formatLocation()}`
);
return BrsInvalid.Instance;
}

Expand Down
12 changes: 4 additions & 8 deletions src/worker/brsTypes/components/RoBitmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Int32 } from "../Int32";
import { Float } from "../Float";
import { RoFont } from "./RoFont";
import { RoAssociativeArray } from "./RoAssociativeArray";
import { numberToHex } from "../../common";
import {
WorkerCanvas,
WorkerCanvasRenderingContext2D,
Expand Down Expand Up @@ -87,7 +88,6 @@ export class RoBitmap extends BrsComponent implements BrsValue {
this.valid = false;
}
this.canvas = createNewCanvas(width, height);
//TODO: Review alpha enable, it should only affect bitmap as destination.
this.context = this.canvas.getContext("2d", {
alpha: true,
}) as WorkerCanvasRenderingContext2D;
Expand All @@ -110,7 +110,7 @@ export class RoBitmap extends BrsComponent implements BrsValue {
const webpDecoder = new WebPDecoder();
const imgBuffer = Buffer.from(image);
const imgArray = WebPRiffParser(imgBuffer, 0);
if (imgArray?.frames && imgArray.frames.length) {
if (imgArray?.frames?.length) {
let aHeight = [0];
let aWidth = [0];
// Get only the first frame (animations not supported)
Expand Down Expand Up @@ -358,7 +358,7 @@ export class RoBitmap extends BrsComponent implements BrsValue {
new StdlibArgument("scaleX", ValueKind.Float),
new StdlibArgument("scaleY", ValueKind.Float),
new StdlibArgument("object", ValueKind.Object),
new StdlibArgument("rgba", ValueKind.Int32, BrsInvalid.Instance), // TODO: add support to rgba
new StdlibArgument("rgba", ValueKind.Int32, BrsInvalid.Instance),
],
returns: ValueKind.Boolean,
},
Expand Down Expand Up @@ -639,11 +639,7 @@ export function rgbaIntToHex(rgba: number, alpha: boolean = true): string {
if (!alpha) {
rgba = rgbaToOpaque(rgba);
}
let hex = (rgba >>> 0).toString(16);
while (hex.length < 8) {
hex = "0" + hex;
}
return "#" + hex;
return "#" + numberToHex(rgba, "0");
}

export function rgbaToTransparent(rgba: number): number {
Expand Down
4 changes: 2 additions & 2 deletions src/worker/brsTypes/components/RoBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Interpreter } from "../../interpreter";
import { BrsType, isBrsNumber } from "..";
import { Unboxable } from "../Boxing";

export class roBoolean extends BrsComponent implements BrsValue, Unboxable {
export class RoBoolean extends BrsComponent implements BrsValue, Unboxable {
readonly kind = ValueKind.Object;
private intrinsic: BrsBoolean;

Expand All @@ -28,7 +28,7 @@ export class roBoolean extends BrsComponent implements BrsValue, Unboxable {
}

equalTo(other: BrsType): BrsBoolean {
if (other instanceof roBoolean) {
if (other instanceof RoBoolean) {
return BrsBoolean.from(other.getValue() === this.getValue());
} else if (isBrsNumber(other) || other instanceof BrsBoolean) {
return BrsBoolean.from(other.toBoolean() === this.intrinsic.toBoolean());
Expand Down
4 changes: 2 additions & 2 deletions src/worker/brsTypes/components/RoDouble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Unboxable } from "../Boxing";
import { Double } from "../Double";
import { vsprintf } from "sprintf-js";

export class roDouble extends BrsComponent implements BrsValue, Unboxable {
export class RoDouble extends BrsComponent implements BrsValue, Unboxable {
readonly kind = ValueKind.Object;
private intrinsic: Double;

Expand All @@ -29,7 +29,7 @@ export class roDouble extends BrsComponent implements BrsValue, Unboxable {
}

equalTo(other: BrsType): BrsBoolean {
if (other instanceof roDouble) {
if (other instanceof RoDouble) {
return BrsBoolean.from(other.intrinsic.getValue() === this.intrinsic.getValue());
}

Expand Down
4 changes: 2 additions & 2 deletions src/worker/brsTypes/components/RoFloat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Unboxable } from "../Boxing";
import { Float } from "../Float";
import { vsprintf } from "sprintf-js";

export class roFloat extends BrsComponent implements BrsValue, Unboxable {
export class RoFloat extends BrsComponent implements BrsValue, Unboxable {
readonly kind = ValueKind.Object;
private intrinsic: Float;

Expand All @@ -29,7 +29,7 @@ export class roFloat extends BrsComponent implements BrsValue, Unboxable {
}

equalTo(other: BrsType): BrsBoolean {
if (other instanceof roFloat) {
if (other instanceof RoFloat) {
return BrsBoolean.from(other.intrinsic.getValue() === this.intrinsic.getValue());
}

Expand Down
4 changes: 2 additions & 2 deletions src/worker/brsTypes/components/RoInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BrsType, isBrsNumber } from "..";
import { Unboxable } from "../Boxing";
import { Int32 } from "../Int32";
import { vsprintf } from "sprintf-js";
export class roInt extends BrsComponent implements BrsValue, Unboxable {
export class RoInt extends BrsComponent implements BrsValue, Unboxable {
readonly kind = ValueKind.Object;
private intrinsic: Int32;

Expand All @@ -31,7 +31,7 @@ export class roInt extends BrsComponent implements BrsValue, Unboxable {
}

equalTo(other: BrsType): BrsBoolean {
if (other instanceof roInt) {
if (other instanceof RoInt) {
return BrsBoolean.from(other.intrinsic.getValue() === this.intrinsic.getValue());
}

Expand Down
Loading

0 comments on commit 304307f

Please sign in to comment.