Skip to content

Commit cdf4c52

Browse files
authored
Fix bux linting warnings (#2683)
* Fix or suppress linting warnings Note this required reworking translation-functions a little to add stricter type checking * Workspace lint/clean runs on bux
1 parent c15c62a commit cdf4c52

File tree

7 files changed

+43
-27
lines changed

7 files changed

+43
-27
lines changed

util/bux/__tests__/cli.spec.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import { createEnglishTranslations } from "../translation-functions";
44
import * as fs from "fs";
55

66
describe("CLI", () => {
7+
beforeEach(() => {
8+
// Suppress expected console warnings
9+
jest.spyOn(console, "warn").mockImplementation(() => {
10+
// no-op
11+
});
12+
});
13+
14+
afterEach(() => jest.resetAllMocks());
15+
716
/* At present, Jest won't allow us to mock methods for reading and writing
817
* JSON files because support of ES modules is lacking.
918
*/
@@ -75,18 +84,16 @@ describe("CLI", () => {
7584
// Localization method should throw error due to duplicate keys
7685
// in files in the source directory
7786
test("Localization method should throw error", async () => {
78-
const throwThis = createEnglishTranslations(
79-
"./__tests__/loc-source-2",
80-
"./build/test-results/loc-results-3",
81-
"./build/test-results/loc-results-3"
82-
);
83-
84-
await expect(throwThis).rejects.toThrow(Error);
87+
await expect(
88+
createEnglishTranslations(
89+
"./__tests__/loc-source-2",
90+
"./build/test-results/loc-results-3",
91+
"./build/test-results/loc-results-3"
92+
)
93+
).rejects.toThrow(Error);
8594

8695
// expect destination directory to be empty due to error
87-
fs.readdir("./build/test-results/loc-results-3", (err, files) => {
88-
expect(err).toBeFalsy();
89-
expect(files.length).toBe(0);
90-
});
96+
const files = fs.readdirSync("./build/test-results/loc-results-3");
97+
expect(files.length).toBe(0);
9198
});
9299
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
account-summary-card:
2+
url: This is a conflict
23
filterApplied: New filter applied, results
34
filterCleared: Filter cleared, results

util/bux/check-cli-path.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
12
const shelljs = require("shelljs");
23
const color = require("cli-color");
34

util/bux/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
"prepack": "npm run build:clean",
2727
"test": "jest",
2828
"test:watch": "npm test -- --watch",
29+
"workspace:clean": "npm run clean",
30+
"workspace:lint": "npm run lint",
31+
"workspace:lint:fix": "npm run lint:fix",
2932
"workspace:test:all": "npm run build:clean && npm test"
3033
},
3134
"dependencies": {

util/bux/translation-functions.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// generate.ts
2-
// eslint-disable no-console
3-
//import "../../src/client/init";
1+
/* eslint-disable no-console */
42

53
import * as fs from "fs";
64
import { EOL } from "os";
@@ -11,6 +9,7 @@ import * as util from "util";
119
import * as path from "path";
1210

1311
type StringMap<V> = { [key: string]: V };
12+
type NestedStringMap<V> = StringMap<V> | StringMap<StringMap<V>>;
1413

1514
const writeFile = promisify(fs.writeFile);
1615

@@ -115,31 +114,34 @@ export class DevTranslationsLoader {
115114
) {
116115
const content = await readFile(path);
117116
this._mergeTranslations(
118-
this._flatten(jsyaml.load(content.toString())),
117+
this._flatten(
118+
jsyaml.load(content.toString()) as NestedStringMap<string>
119+
),
119120
path,
120121
duplicateCallback
121122
);
122123
}
123124

124-
private _flatten(translations: unknown): StringMap<string> {
125-
const output: StringMap<any> = {};
125+
private _flatten(translations: NestedStringMap<string>): StringMap<string> {
126+
const output: StringMap<string> = {};
126127

127128
function step(
128-
object: any,
129+
object: NestedStringMap<string>,
129130
prev: string | null = null,
130131
currentDepth: number = 0
131132
) {
132133
currentDepth = currentDepth || 1;
133134
for (const key of Object.keys(object)) {
134-
const value = object[key];
135-
const isString = typeof value === "string";
136-
137135
const newKey = prev ? prev + "." + key : key;
138-
139-
if (!isString && Object.keys(value).length) {
140-
output[newKey] = step(value, newKey, currentDepth + 1);
141-
} else {
136+
const value = object[key];
137+
if (typeof value === "string") {
142138
output[newKey] = value;
139+
} else if (value instanceof Object) {
140+
if (Object.keys(value).length > 0) {
141+
step(value, newKey, currentDepth + 1);
142+
}
143+
} else {
144+
throw new Error(`Invalid translation value for ${newKey}`);
143145
}
144146
}
145147
}
@@ -150,7 +152,7 @@ export class DevTranslationsLoader {
150152
}
151153

152154
private _mergeTranslations(
153-
translations: StringMap<any>,
155+
translations: StringMap<string>,
154156
source: string,
155157
duplicateCallback: DuplicateCallback
156158
) {

util/bux/util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const error = (...args: string[]) => console.error(color.red(...args));
3333

3434
export function readJsonOrDefault(filename: string, defaultJson = {}) {
3535
if (fs.existsSync(filename)) {
36+
// eslint-disable-next-line security/detect-non-literal-require
3637
return require(filename);
3738
}
3839
return defaultJson;
@@ -253,7 +254,7 @@ function printInfo(name: string, repoPath?: string): void {
253254
}
254255

255256
async function printLinkStatus(): Promise<void> {
256-
let linkChecks: boolean[] = [];
257+
const linkChecks: boolean[] = [];
257258
await runLinkageTask((opts: LinkOptions) => {
258259
let isLinked = false;
259260

util/common-config/.eslintrc-common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
],
8787
"security/detect-object-injection": "off",
8888
"security/detect-unsafe-regex": "off",
89+
"security/detect-non-literal-fs-filename": "off",
8990
"semi": ["error", "always"]
9091
}
9192
}

0 commit comments

Comments
 (0)