Skip to content

Commit

Permalink
♻️ Fix linter errors/warnings after eslint upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukáš Horák committed Jan 13, 2022
1 parent e9f82fc commit 131a0ce
Show file tree
Hide file tree
Showing 28 changed files with 202 additions and 176 deletions.
25 changes: 20 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
{
"extends": [
"oclif",
"oclif-typescript",
"prettier"
]
"extends": ["oclif", "oclif-typescript", "prettier"],
"rules": {
"unicorn/prefer-node-protocol": "off",
"node/no-extraneous-import": "off",
"unicorn/prefer-module": "off",
"unicorn/import-style": [
"error",
{
"styles": {
"path": {
"namespace": true
}
}
}
],
"no-warning-comments": [
"warn",
{ "terms": ["fixme", "xxx"], "location": "start" }
]
}
}
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
"update-gitmoji-config": "node ./scripts/update-gitmoji-config.js"
},
"devDependencies": {
"@types/jest-when": "^2.7.2",
"chai": "^4.2.0",
"eslint": "^7.32.0",
"eslint-config-oclif": "^4.0.0",
"eslint-config-oclif-typescript": "^1.0.2",
"eslint-config-prettier": "^8.3.0",
"husky": "^4.3.0",
"jest-when": "^3.0.1",
"lint-staged": "^10.5.1",
"prettier": "^2.1.1",
"typescript": "^3.9"
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"devDependencies": {
"@oclif/dev-cli": "^1",
"@oclif/test": "^1",
"@types/node": "^10"
"@types/node": "^10",
"@types/jest-when": "^2.7.2",
"jest-when": "^3.0.1"
},
"engines": {
"node": ">=8.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const pkg = require("../package.json");
export default abstract class Base extends Command {
protected conf: undefined | null | ConfigType;

async init() {
async init(): Promise<void> {
const notifier = updateNotifier({
pkg,
updateCheckInterval: 1000,
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/__tests__/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe("update command", () => {
const mockSheetLines3 = [{ key: "sheet3.line_1" }, { key: "sheet3.line_2" }];

const test = oclifTest.register("setupMocks", () => ({
/* eslint-disable no-console */
run() {
ReaderMock.mockClear();
WorksheetReaderMock.mockClear();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Open extends Base {
id: cliFlags.id.flag(),
};

async run() {
async run(): Promise<void> {
const { flags } = this.parse(Open);

const sheetId = flags.id;
Expand Down
44 changes: 23 additions & 21 deletions packages/cli/src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,23 @@ class Update extends Base {
const allWorksheetLines = Object.values(linesByWorkshet).flat();
const OTHER_DOMAIN = "__other__";

const linesByDomain = allWorksheetLines.reduce(
(domainLines: { [domain: string]: Line[] }, line: Line) => {
const domain =
domains.find((d) => line.key.startsWith(`${d}.`)) || OTHER_DOMAIN;
const linesByDomain: { [domain: string]: Line[] } = {};

domainLines[domain] = domainLines[domain] ?? [];
domainLines[domain].push(line);
for (const line of allWorksheetLines) {
const domain =
domains.find((d) => line.key.startsWith(`${d}.`)) || OTHER_DOMAIN;

return domainLines;
},
{}
);
linesByDomain[domain] = linesByDomain[domain] ?? [];
linesByDomain[domain].push(line);
}

domains
.filter((domain) => linesByDomain[domain].length === 0)
.forEach((domain) => {
for (const domain of domains) {
if (linesByDomain[domain].length === 0) {
this.warn(
`😐 Received no lines for language ${langName} and domain ${domain}`
);
});
}
}

return (
Object.keys(linesByDomain)
Expand All @@ -128,7 +125,7 @@ class Update extends Base {
}

// eslint-disable-next-line complexity
async run() {
async run(): Promise<void> {
const { flags } = this.parse(Update);

const sheetId = flags.id ?? "";
Expand Down Expand Up @@ -161,14 +158,19 @@ class Update extends Base {
try {
worksheetReader = new WorksheetReader(sheets, { logger });
} catch (error) {
if (error instanceof InvalidFilterError) {
throw new IncorrectFlagValue(error.message);
} else {
throw error;
}
const normalizedError =
error instanceof InvalidFilterError
? new IncorrectFlagValue(error.message)
: error;

throw normalizedError;
}

const plugins = loadPlugins(this.conf?.plugins, { logger }, { languages });
const plugins = loadPlugins(
this.conf?.plugins ?? [],
{ logger },
{ languages }
);
const outputTransformer = transformersByFormat[format];
const reader = new Reader(sheetId, worksheetReader, plugins, {
logger,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/flags/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const flag = flags.build({
},
});

export const invariant = (id?: string) => {
export const invariant = (id?: string): void => {
if (!id) {
throw new MissingFlagValue("Sheet id");
}
Expand Down
12 changes: 7 additions & 5 deletions packages/cli/src/invariants.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { CLIError } from "@oclif/errors";

export const cliInvariant = (
expression: any,
expression: unknown,
message: string,
options: object = {}
) => {
options: Record<string, any> = {}
): void => {
if (!expression) {
throw new CLIError(message, options);
}
};

export const noExitCliInvariant = (expression: any, message: string) =>
cliInvariant(expression, message, { exit: false });
export const noExitCliInvariant = (
expression: unknown,
message: string
): void => cliInvariant(expression, message, { exit: false });
5 changes: 3 additions & 2 deletions packages/core/src/__tests__/writer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { EOL } from "os";
import * as mkdirp from "mkdirp";

const fs = {
accessAsync: jest.fn().mockResolvedValue(undefined),
accessAsync: jest.fn(),
readFileAsync: jest.fn().mockResolvedValue(""),
writeFileAsync: jest.fn().mockResolvedValue(undefined),
writeFileAsync: jest.fn(),
};

jest.mock("fs");
Expand All @@ -29,6 +29,7 @@ const plugin = {
pluginName: "test",
transformFullOutput: jest.fn(),
transformLine: jest.fn(),
readTranslation: jest.fn(),
};
const plugins = new PluginsRunner([plugin], { logger });

Expand Down
38 changes: 19 additions & 19 deletions packages/core/src/line.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable unicorn/filename-case */
const COMMENT_STARTERS = ["//", "#"];
const PLURAL_KEY_RE = /.+##\{(zero|one|two|few|many|other)\}/;
const PLURAL_POSTFIX_RE = /##\{(zero|one|two|few|many|other)\}/;
const PLURAL_KEY_RE = /.+##{(zero|one|two|few|many|other)}/;
const PLURAL_POSTFIX_RE = /##{(zero|one|two|few|many|other)}/;

enum Type {
COMMENT,
Expand Down Expand Up @@ -45,71 +44,72 @@ class Line {
this.value = value ?? "";
}

static checkIsComment(val: any): boolean {
static checkIsComment(val: string): boolean {
for (const commentStarter of COMMENT_STARTERS) {
if (val.indexOf(commentStarter) === 0) {
return true;
}
}

return false;
}

static checkIsPlural = function (val: any): boolean {
if (val.match(PLURAL_KEY_RE)) {
static checkIsPlural = function (val: string): boolean {
if (PLURAL_KEY_RE.test(val)) {
return true;
}

return false;
};

static parseKeysFromPlural = function (val: string) {
static parseKeysFromPlural = function (val: string): [string, string] {
const match = val.match(PLURAL_POSTFIX_RE);
if (match) {
return [val.replace(match[0], ""), match[1]];
}

return [val, ""];
};

static normalizeComment(val: string) {
static normalizeComment(val: string): string {
for (const commentStarter of COMMENT_STARTERS) {
const index = val.indexOf(commentStarter);

if (index === 0) {
const normalized = val.substr(
commentStarter.length,
val.length - commentStarter.length
);
const normalized = val.slice(commentStarter.length);

return normalized.trim();
}
}

return val;
}

isEmpty() {
isEmpty(): boolean {
return !this.isComment() && !(this.key && this.value);
}

isComment() {
isComment(): boolean {
return this.type === Type.COMMENT;
}

isPlural() {
isPlural(): boolean {
return this.type === Type.PLURAL;
}

getComment() {
getComment(): string {
return this.key;
}

getPluralKey() {
getPluralKey(): string {
return this.pluralKey;
}

setKey(nextKey: string | ((key: string) => string)) {
setKey(nextKey: string | ((key: string) => string)): void {
this.key = typeof nextKey === "function" ? nextKey(this.key) : nextKey;
}

setValue(nextValue: string | ((value: string) => string)) {
setValue(nextValue: string | ((value: string) => string)): void {
this.value =
typeof nextValue === "function" ? nextValue(this.value) : nextValue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const pluginDefaults: LoksePlugin = {
readTranslation: identity,
};

export function createPlugin(plugin: Partial<LoksePlugin>) {
export function createPlugin(plugin: Partial<LoksePlugin>): LoksePlugin {
return {
...pluginDefaults,
...plugin,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/plugins/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ function loadPlugin(
export type PluginName = string;
export interface PluginDefinition {
name: PluginName;
options: object;
options: Record<string, any>;
}

export function loadPlugins(
plugins: (PluginName | PluginDefinition)[] | unknown = [],
plugins: (PluginName | PluginDefinition)[] | unknown,
options: GeneralPluginOptions,
meta: GeneralPluginMeta
) {
): PluginsRunner {
let loadedPlugins: NamedLoksePlugin[];

if (Array.isArray(plugins)) {
Expand Down
27 changes: 14 additions & 13 deletions packages/core/src/reader/__tests__/spreadsheet-reader.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase */
import { GoogleSpreadsheet } from "google-spreadsheet";
import SpreadsheetReader from "../spreadsheet-reader";
import WorksheetReader from "../worksheet-reader";
Expand All @@ -14,6 +15,18 @@ const GoogleSpreadsheetMock = GoogleSpreadsheet as jest.Mock;

jest.mock("google-spreadsheet");

const makeFakeLine = (id: string) => {
return { id: `line_${id}` } as unknown as Line;
};

const makeFakeWorksheet = (title: string, lines: Line[]) => {
const fakeWorksheet = {
title,
extractLines: jest.fn().mockReturnValue(lines),
};
return fakeWorksheet as unknown as Worksheet;
};

describe("SpreadsheetReader", () => {
const testLogger = {
log: jest.fn(),
Expand Down Expand Up @@ -90,18 +103,6 @@ describe("SpreadsheetReader", () => {
});

describe("read", () => {
const makeFakeLine = (id: string) => {
return { id: `line_${id}` } as unknown as Line;
};

const makeFakeWorksheet = (title: string, lines: Line[]) => {
const fakeWorksheet = {
title,
extractLines: jest.fn().mockReturnValue(lines),
};
return fakeWorksheet as unknown as Worksheet;
};

const linesSet1 = [makeFakeLine("1_1"), makeFakeLine("1_2")];

const linesSet2 = [
Expand Down Expand Up @@ -204,7 +205,7 @@ describe("SpreadsheetReader", () => {
jest.spyOn(reader, "fetchSheets").mockResolvedValue(sheetsList);

await expect(reader.read("key", "en-gb")).resolves.toEqual({
fakeSheet1: linesSet1.concat(linesSet3),
fakeSheet1: [...linesSet1, ...linesSet3],
fakeSheet2: linesSet2,
});
expect(testLogger.warn).toHaveBeenCalledTimes(1);
Expand Down
Loading

0 comments on commit 131a0ce

Please sign in to comment.