Merge pull request #185 from github/joshmgross/lint-expressions

Lint the expressions package
This commit is contained in:
Josh Gross
2023-03-16 12:45:49 -04:00
committed by GitHub
10 changed files with 34 additions and 32 deletions
@@ -9,7 +9,7 @@ export function isDescriptionDictionary(x: ExpressionData): x is DescriptionDict
export class DescriptionDictionary extends Dictionary { export class DescriptionDictionary extends Dictionary {
private readonly descriptions = new Map<string, string>(); private readonly descriptions = new Map<string, string>();
public complete: boolean = true; public complete = true;
constructor(...pairs: DescriptionPair[]) { constructor(...pairs: DescriptionPair[]) {
super(); super();
+1 -3
View File
@@ -1,8 +1,6 @@
import {ExpressionData, ExpressionDataInterface, Kind} from "./expressiondata"; import {ExpressionDataInterface, Kind} from "./expressiondata";
export class Null implements ExpressionDataInterface { export class Null implements ExpressionDataInterface {
constructor() {}
public readonly kind = Kind.Null; public readonly kind = Kind.Null;
public primitive = true; public primitive = true;
+13
View File
@@ -0,0 +1,13 @@
import {NumberData} from "./number";
describe("number", () => {
it("coerces to string", () => {
expect(new NumberData(-0).coerceString()).toEqual("0");
expect(new NumberData(0).coerceString()).toEqual("0");
expect(new NumberData(1).coerceString()).toEqual("1");
expect(new NumberData(1.2).coerceString()).toEqual("1.2");
// Round to 15 digits precision
expect(new NumberData(1.2345678901234567).coerceString()).toEqual("1.234567890123457");
});
});
+1 -1
View File
@@ -8,7 +8,7 @@ export class NumberData implements ExpressionDataInterface {
public primitive = true; public primitive = true;
coerceString(): string { coerceString(): string {
if (this.value === -0) { if (this.value === 0) {
return "0"; return "0";
} }
+2 -2
View File
@@ -10,7 +10,7 @@ import {StringData} from "./string";
* *
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#replacer * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#replacer
*/ */
export function replacer(key: string, value: any): any { export function replacer(_key: string, value: unknown): unknown {
if (value instanceof Null) { if (value instanceof Null) {
return null; return null;
} }
@@ -34,7 +34,7 @@ export function replacer(key: string, value: any): any {
if (value instanceof Dictionary) { if (value instanceof Dictionary) {
const pairs = value.pairs(); const pairs = value.pairs();
const r: any = {}; const r: Record<string, unknown> = {};
for (const p of pairs) { for (const p of pairs) {
r[p.key] = p.value; r[p.key] = p.value;
} }
+9 -12
View File
@@ -1,7 +1,7 @@
import {Array as dArray} from "./array"; import {Array as dArray} from "./array";
import {BooleanData} from "./boolean"; import {BooleanData} from "./boolean";
import {Dictionary} from "./dictionary"; import {Dictionary} from "./dictionary";
import {ExpressionData, Pair} from "./expressiondata"; import {ExpressionData} from "./expressiondata";
import {Null} from "./null"; import {Null} from "./null";
import {NumberData} from "./number"; import {NumberData} from "./number";
import {StringData} from "./string"; import {StringData} from "./string";
@@ -11,7 +11,7 @@ import {StringData} from "./string";
* *
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#reviver * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#reviver
*/ */
export function reviver(key: string, val: any): ExpressionData { export function reviver(_key: string, val: unknown): ExpressionData {
if (val === null) { if (val === null) {
return new Null(); return new Null();
} }
@@ -25,25 +25,22 @@ export function reviver(key: string, val: any): ExpressionData {
} }
if (typeof val === "boolean") { if (typeof val === "boolean") {
return new BooleanData(val as boolean); return new BooleanData(val);
} }
if (Array.isArray(val)) { if (Array.isArray(val)) {
return new dArray(...val); return new dArray(...(val as ExpressionData[]));
} }
if (typeof val === "object") { if (typeof val === "object") {
return new Dictionary( return new Dictionary(
...Object.keys(val).map( ...Object.keys(val).map(k => ({
k => key: k,
({ value: val[k as keyof typeof val]
key: k, }))
value: val[k]
} as Pair)
)
); );
} }
// Pass through value // Pass through value
return val; return val as ExpressionData;
} }
+2 -2
View File
@@ -1,4 +1,4 @@
import {Array, BooleanData, ExpressionData, Kind} from "../data"; import {BooleanData, ExpressionData, Kind} from "../data";
import {equals} from "../result"; import {equals} from "../result";
import {FunctionDefinition} from "./info"; import {FunctionDefinition} from "./info";
@@ -19,7 +19,7 @@ export const contains: FunctionDefinition = {
return new BooleanData(ls.toLowerCase().includes(rs.toLowerCase())); return new BooleanData(ls.toLowerCase().includes(rs.toLowerCase()));
} }
} else if (left.kind === Kind.Array) { } else if (left.kind === Kind.Array) {
const la = left as Array; const la = left;
if (la.values().length === 0) { if (la.values().length === 0) {
return new BooleanData(false); return new BooleanData(false);
} }
+2 -8
View File
@@ -15,7 +15,7 @@ export const format: FunctionDefinition = {
while (index < fs.length) { while (index < fs.length) {
const lbrace = fs.indexOf("{", index); const lbrace = fs.indexOf("{", index);
let rbrace = fs.indexOf("}", index); const rbrace = fs.indexOf("}", index);
// Left brace // Left brace
if (lbrace >= 0 && (rbrace < 0 || rbrace > lbrace)) { if (lbrace >= 0 && (rbrace < 0 || rbrace > lbrace)) {
@@ -81,7 +81,7 @@ function safeCharAt(string: string, index: number): string {
function readArgIndex(string: string, startIndex: number): ArgIndex { function readArgIndex(string: string, startIndex: number): ArgIndex {
// Count the number of digits // Count the number of digits
let length = 0; let length = 0;
while (true) { for (;;) {
const nextChar = safeCharAt(string, startIndex + length); const nextChar = safeCharAt(string, startIndex + length);
if (nextChar >= "0" && nextChar <= "9") { if (nextChar >= "0" && nextChar <= "9") {
length++; length++;
@@ -112,9 +112,3 @@ interface ArgIndex {
result: number; result: number;
endIndex: number; endIndex: number;
} }
interface FormatSpecifiers {
success: boolean;
result: string;
rbrace: number;
}
+1 -1
View File
@@ -18,7 +18,7 @@ export const fromjson: FunctionDefinition = {
} }
try { try {
return JSON.parse(is, reviver); return JSON.parse(is, reviver) as ExpressionData;
} catch (e) { } catch (e) {
throw new ExpressionEvaluationError("Error parsing JSON when evaluating fromJson", {cause: e}); throw new ExpressionEvaluationError("Error parsing JSON when evaluating fromJson", {cause: e});
} }
+2 -2
View File
@@ -1,4 +1,4 @@
import {Array, ExpressionData, Kind, StringData} from "../data"; import {ExpressionData, Kind, StringData} from "../data";
import {FunctionDefinition} from "./info"; import {FunctionDefinition} from "./info";
export const join: FunctionDefinition = { export const join: FunctionDefinition = {
@@ -23,7 +23,7 @@ export const join: FunctionDefinition = {
// Convert items to strings // Convert items to strings
return new StringData( return new StringData(
(args[0] as Array) args[0]
.values() .values()
.map(item => item.coerceString()) .map(item => item.coerceString())
.join(separator) .join(separator)