Skip to content

Commit

Permalink
fix eslint issues in adaptive expressions (#4833)
Browse files Browse the repository at this point in the history
  • Loading branch information
JhontSouth authored Jan 6, 2025
1 parent 5464ddd commit 7cafc93
Show file tree
Hide file tree
Showing 90 changed files with 299 additions and 376 deletions.
11 changes: 3 additions & 8 deletions libraries/adaptive-expressions/eslint.config.cjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
const onlyWarn = require("eslint-plugin-only-warn");
const sharedConfig = require("../../eslint.config.cjs")
const sharedConfig = require('../../eslint.config.cjs');

module.exports = [
...sharedConfig,
{
ignores: ["**/generated/*", "**/expressionProperty.test.js"],
ignores: ['**/generated/*', '**/expressionProperty.test.js'],
},
{
plugins: {
"only-warn": onlyWarn,
},
}];
];
1 change: 0 additions & 1 deletion libraries/adaptive-expressions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"btoa-lite": "^1.0.0",
"d3-format": "^2.0.0",
"dayjs": "^1.11.13",
"eslint-plugin-only-warn": "^1.1.0",
"jspath": "^0.4.0",
"lodash": "^4.17.21",
"lru-cache": "^5.1.1",
Expand Down
2 changes: 1 addition & 1 deletion libraries/adaptive-expressions/src/builtinFunctions/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class Add extends ExpressionEvaluator {
expression,
2,
Number.MAX_SAFE_INTEGER,
ReturnType.String | ReturnType.Number
ReturnType.String | ReturnType.Number,
);
}
}
13 changes: 5 additions & 8 deletions libraries/adaptive-expressions/src/builtinFunctions/addDays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export class AddDays extends TimeTransformEvaluator {
* Initializes a new instance of the [AddDays](xref:adaptive-expressions.AddDays) class.
*/
constructor() {
super(
ExpressionType.AddDays,
(ts: Date, num: number): Date => {
const newDate = new Date(ts);
newDate.setDate(ts.getDate() + num);
return newDate;
}
);
super(ExpressionType.AddDays, (ts: Date, num: number): Date => {
const newDate = new Date(ts);
newDate.setDate(ts.getDate() + num);
return newDate;
});
}
}
13 changes: 5 additions & 8 deletions libraries/adaptive-expressions/src/builtinFunctions/addHours.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export class AddHours extends TimeTransformEvaluator {
* Initializes a new instance of the [AddHours](xref:adaptive-expressions.AddHours) class.
*/
constructor() {
super(
ExpressionType.AddHours,
(ts: Date, num: number): Date => {
const newDate = new Date(ts);
newDate.setHours(ts.getHours() + num);
return newDate;
}
);
super(ExpressionType.AddHours, (ts: Date, num: number): Date => {
const newDate = new Date(ts);
newDate.setHours(ts.getHours() + num);
return newDate;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export class AddMinutes extends TimeTransformEvaluator {
* Initializes a new instance of the [AddMinutes](xref:adaptive-expressions.AddMinutes) class.
*/
constructor() {
super(
ExpressionType.AddMinutes,
(ts: Date, num: number): Date => {
const newDate = new Date(ts);
newDate.setMinutes(ts.getMinutes() + num);
return newDate;
}
);
super(ExpressionType.AddMinutes, (ts: Date, num: number): Date => {
const newDate = new Date(ts);
newDate.setMinutes(ts.getMinutes() + num);
return newDate;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class AddOrdinal extends ExpressionEvaluator {
private static evaluator(): EvaluateExpressionDelegate {
return FunctionUtils.apply(
(args: any[]): string => AddOrdinal.evalAddOrdinal(args[0]),
FunctionUtils.verifyInteger
FunctionUtils.verifyInteger,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export class AddSeconds extends TimeTransformEvaluator {
* Initializes a new instance of the [AddSeconds](xref:adaptive-expressions.AddSeconds) class.
*/
constructor() {
super(
ExpressionType.AddSeconds,
(ts: Date, num: number): Date => {
const newDate = new Date(ts);
newDate.setSeconds(ts.getSeconds() + num);
return newDate;
}
);
super(ExpressionType.AddSeconds, (ts: Date, num: number): Date => {
const newDate = new Date(ts);
newDate.setSeconds(ts.getSeconds() + num);
return newDate;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class AddToTime extends ExpressionEvaluator {
interval: number,
timeUnit: string,
format?: string,
locale?: string
locale?: string,
): ValueWithError {
let result: string;
const error = InternalFunctionUtils.verifyISOTimestamp(timeStamp);
Expand All @@ -81,7 +81,7 @@ export class AddToTime extends ExpressionEvaluator {
[ReturnType.String, ReturnType.String],
ReturnType.String,
ReturnType.Number,
ReturnType.String
ReturnType.String,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Average extends ExpressionEvaluator {
private static evaluator(): EvaluateExpressionDelegate {
return FunctionUtils.apply(
(args: any[]): number => args[0].reduce((x: number, y: number): number => x + y) / args[0].length,
FunctionUtils.verifyNumericList
FunctionUtils.verifyNumericList,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Base64ToBinary extends ExpressionEvaluator {
ExpressionType.Base64ToBinary,
Base64ToBinary.evaluator(),
ReturnType.Object,
FunctionUtils.validateUnary
FunctionUtils.validateUnary,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Base64ToString extends ExpressionEvaluator {
ExpressionType.Base64ToString,
Base64ToString.evaluator(),
ReturnType.String,
FunctionUtils.validateUnary
FunctionUtils.validateUnary,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Binary extends ExpressionEvaluator {
private static evaluator(): EvaluateExpressionDelegate {
return FunctionUtils.apply(
(args: any[]): Uint8Array => InternalFunctionUtils.getTextEncoder().encode(args[0]),
FunctionUtils.verifyString
FunctionUtils.verifyString,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ComparisonEvaluator extends ExpressionEvaluator {
type: string,
func: (arg0: any[]) => boolean,
validator: ValidateExpressionDelegate,
verify?: VerifyExpression
verify?: VerifyExpression,
) {
super(type, ComparisonEvaluator.evaluator(func, verify), ReturnType.Boolean, validator);
}
Expand All @@ -52,7 +52,7 @@ export class ComparisonEvaluator extends ExpressionEvaluator {
expression,
state,
newOptions,
verify
verify,
);
let error = childrenError;
if (!error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Concat extends ExpressionEvaluator {
ExpressionType.Concat,
Concat.evaluator(),
ReturnType.String | ReturnType.Array,
FunctionUtils.validateAtLeastOne
FunctionUtils.validateAtLeastOne,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class ConvertFromUTC extends ExpressionEvaluator {
timeStamp: string,
destinationTimeZone: string,
format?: string,
locale?: string
locale?: string,
): ValueWithError {
let result: string;
let error: string;
Expand Down Expand Up @@ -89,7 +89,7 @@ export class ConvertFromUTC extends ExpressionEvaluator {
expression,
[ReturnType.String, ReturnType.String],
ReturnType.String,
ReturnType.String
ReturnType.String,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class ConvertToUTC extends ExpressionEvaluator {
timeStamp: string,
sourceTimezone: string,
format?: string,
locale?: string
locale?: string,
): ValueWithError {
let result: string;
let error: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class CountWord extends ExpressionEvaluator {
private static evaluator(): EvaluateExpressionDelegate {
return FunctionUtils.apply(
(args: any[]): number => InternalFunctionUtils.parseStringOrUndefined(args[0]).trim().split(/\s+/).length,
FunctionUtils.verifyStringOrNull
FunctionUtils.verifyStringOrNull,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class DataUri extends ExpressionEvaluator {
private static evaluator(): EvaluateExpressionDelegate {
return FunctionUtils.apply(
(args: any[]): string => 'data:text/plain;charset=utf-8;base64,'.concat(btoa(args[0])),
FunctionUtils.verifyString
FunctionUtils.verifyString,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class DataUriToBinary extends ExpressionEvaluator {
ExpressionType.DataUriToBinary,
DataUriToBinary.evaluator(),
ReturnType.Object,
FunctionUtils.validateUnary
FunctionUtils.validateUnary,
);
}

Expand All @@ -34,7 +34,7 @@ export class DataUriToBinary extends ExpressionEvaluator {
private static evaluator(): EvaluateExpressionDelegate {
return FunctionUtils.apply(
(args: any[]): Uint8Array => InternalFunctionUtils.getTextEncoder().encode(args[0]),
FunctionUtils.verifyString
FunctionUtils.verifyString,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class DataUriToString extends ExpressionEvaluator {
ExpressionType.DataUriToString,
DataUriToString.evaluator(),
ReturnType.String,
FunctionUtils.validateUnary
FunctionUtils.validateUnary,
);
}

Expand All @@ -34,7 +34,7 @@ export class DataUriToString extends ExpressionEvaluator {
private static evaluator(): EvaluateExpressionDelegate {
return FunctionUtils.apply(
(args: any[]): string => atob(args[0].slice(args[0].indexOf(',') + 1)),
FunctionUtils.verifyString
FunctionUtils.verifyString,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export class EndsWith extends ExpressionEvaluator {
return FunctionUtils.apply(
(args: any[]): boolean =>
InternalFunctionUtils.parseStringOrUndefined(args[0]).endsWith(
InternalFunctionUtils.parseStringOrUndefined(args[1])
InternalFunctionUtils.parseStringOrUndefined(args[1]),
),
FunctionUtils.verifyStringOrNull
FunctionUtils.verifyStringOrNull,
);
}

Expand Down
3 changes: 1 addition & 2 deletions libraries/adaptive-expressions/src/builtinFunctions/eol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EvaluateExpressionDelegate, ExpressionEvaluator } from '../expressionEv
import { ExpressionType } from '../expressionType';
import { FunctionUtils } from '../functionUtils';
import { ReturnType } from '../returnType';
import os from 'os';

/**
* Return the newline string according to the environment.
Expand Down Expand Up @@ -39,8 +40,6 @@ export class EOL extends ExpressionEvaluator {
} else if (typeof self !== 'undefined') {
return self.navigator.platform.includes('Win') ? '\r\n' : '\n';
} else {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const os = require('os');
return os.EOL;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Equal extends ComparisonEvaluator {
super(
ExpressionType.Equal,
(args) => FunctionUtils.commonEquals(args[0], args[1]),
FunctionUtils.validateBinary
FunctionUtils.validateBinary,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Foreach extends ExpressionEvaluator {
ExpressionType.Foreach,
InternalFunctionUtils.foreach,
ReturnType.Array,
InternalFunctionUtils.ValidateLambdaExpression
InternalFunctionUtils.ValidateLambdaExpression,
);
}
}
58 changes: 31 additions & 27 deletions libraries/adaptive-expressions/src/builtinFunctions/formatNumber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable security/detect-object-injection */
/**
* @module adaptive-expressions
*/
Expand Down Expand Up @@ -31,35 +30,40 @@ export class FormatNumber extends ExpressionEvaluator {
* @private
*/
private static evaluator(): EvaluateExpressionDelegate {
return FunctionUtils.applyWithOptionsAndError((args: unknown[], options: Options): {
value: unknown;
error: string;
} => {
let value: unknown = null;
let error: string;
const number = args[0];
const precision = args[1];
let locale = options.locale ? options.locale : Intl.DateTimeFormat().resolvedOptions().locale;
locale = FunctionUtils.determineLocale(args, 3, locale);
if (!FunctionUtils.isNumber(number)) {
error = `formatNumber first argument ${number} must be a number`;
} else if (!FunctionUtils.isNumber(precision)) {
error = `formatNumber second argument ${precision} must be a number`;
} else if (locale && typeof locale !== 'string') {
error = `formatNubmer third argument ${locale} is not a valid locale`;
} else {
const fixedNotation = `,.${precision}f`;
const roundedNumber = this.roundToPrecision(number, precision);
const formatLocale = localeInfo[locale];
if (formatLocale !== undefined) {
value = d3formatLocale(formatLocale).format(fixedNotation)(roundedNumber);
return FunctionUtils.applyWithOptionsAndError(
(
args: unknown[],
options: Options,
): {
value: unknown;
error: string;
} => {
let value: unknown = null;
let error: string;
const number = args[0];
const precision = args[1];
let locale = options.locale ? options.locale : Intl.DateTimeFormat().resolvedOptions().locale;
locale = FunctionUtils.determineLocale(args, 3, locale);
if (!FunctionUtils.isNumber(number)) {
error = `formatNumber first argument ${number} must be a number`;
} else if (!FunctionUtils.isNumber(precision)) {
error = `formatNumber second argument ${precision} must be a number`;
} else if (locale && typeof locale !== 'string') {
error = `formatNubmer third argument ${locale} is not a valid locale`;
} else {
value = d3format(fixedNotation)(roundedNumber);
const fixedNotation = `,.${precision}f`;
const roundedNumber = this.roundToPrecision(number, precision);
const formatLocale = localeInfo[locale];
if (formatLocale !== undefined) {
value = d3formatLocale(formatLocale).format(fixedNotation)(roundedNumber);
} else {
value = d3format(fixedNotation)(roundedNumber);
}
}
}

return { value, error };
});
return { value, error };
},
);
}

private static roundToPrecision = (num: number, digits: number): number =>
Expand Down
Loading

0 comments on commit 7cafc93

Please sign in to comment.