Fix many ESlint errors (#1265)

* Unvendor toml package

* Fix many ESlint errors

For mysterious reasons, these errors appear on my branch #1262 even
though they are not related to changes there. The eslint config seems
a bit unstable.

Anyways this fixes them.

* Put back Record

* Fix typescript compilation

* Fix lints

* Try @iarna/toml instead

* Fix import

* Use @ltd/j-toml

* Update test

* Use toml-j0.4

* Some changes

* Fix toml import

* Try adding eslint gha job

* Add forgotten checkout action

* Force CI to run

* Blah

* Fix

* Revert changes to github workflow

* Fix lints

* wget toml-j0.4 type definitions

* Add toml-j types workaround to eslint workflow

* Apply formatter

* Use @hoodmane/toml-j0.4

* Import from @hoodmane/toml-j0.4
This commit is contained in:
Hood Chatham
2023-03-13 15:51:28 +01:00
committed by GitHub
parent 653e2c9be4
commit 37c9db09c6
18 changed files with 196 additions and 122 deletions

View File

@@ -1,4 +1,4 @@
import toml from 'toml-j0.4';
import toml from '@hoodmane/toml-j0.4';
import { getLogger } from './logger';
import { version } from './version';
import { getAttribute, readTextFromPath, htmlDecode, createDeprecationWarning } from './utils';
@@ -6,6 +6,7 @@ import { UserError, ErrorCode } from './exceptions';
const logger = getLogger('py-config');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface AppConfig extends Record<string, any> {
name?: string;
description?: string;
@@ -42,11 +43,11 @@ export type PyScriptMetadata = {
time?: string;
};
const allKeys = {
const allKeys = Object.entries({
string: ['name', 'description', 'version', 'type', 'author_name', 'author_email', 'license'],
number: ['schema_version'],
array: ['runtimes', 'interpreters', 'packages', 'fetch', 'plugins'],
};
});
export const defaultConfig: AppConfig = {
schema_version: 1,
@@ -106,6 +107,7 @@ function fillUserData(inputConfig: AppConfig, resultConfig: AppConfig): AppConfi
for (const key in inputConfig) {
// fill in all extra keys ignored by the validator
if (!(key in defaultConfig)) {
// eslint-disable-next-line
resultConfig[key] = inputConfig[key];
}
}
@@ -122,13 +124,14 @@ function mergeConfig(inlineConfig: AppConfig, externalConfig: AppConfig): AppCon
} else {
let merged: AppConfig = {};
for (const keyType in allKeys) {
const keys: string[] = allKeys[keyType];
for (const [keyType, keys] of allKeys) {
keys.forEach(function (item: string) {
if (keyType === 'boolean') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
merged[item] =
typeof inlineConfig[item] !== 'undefined' ? inlineConfig[item] : externalConfig[item];
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
merged[item] = inlineConfig[item] || externalConfig[item];
}
});
@@ -143,7 +146,7 @@ function mergeConfig(inlineConfig: AppConfig, externalConfig: AppConfig): AppCon
}
}
function parseConfig(configText: string, configType = 'toml') {
function parseConfig(configText: string, configType = 'toml'): AppConfig {
if (configType === 'toml') {
// TOML parser is soft and can parse even JSON strings, this additional check prevents it.
if (configText.trim()[0] === '{') {
@@ -153,8 +156,9 @@ function parseConfig(configText: string, configType = 'toml') {
);
}
try {
return toml.parse(configText);
} catch (err) {
return toml.parse(configText) as AppConfig;
} catch (e) {
const err = e as Error;
const errMessage: string = err.toString();
throw new UserError(
@@ -164,8 +168,9 @@ function parseConfig(configText: string, configType = 'toml') {
}
} else if (configType === 'json') {
try {
return JSON.parse(configText);
} catch (err) {
return JSON.parse(configText) as AppConfig;
} catch (e) {
const err = e as Error;
const errMessage: string = err.toString();
throw new UserError(
ErrorCode.BAD_CONFIG,
@@ -185,17 +190,17 @@ function validateConfig(configText: string, configType = 'toml') {
const finalConfig: AppConfig = {};
for (const keyType in allKeys) {
const keys: string[] = allKeys[keyType];
for (const [keyType, keys] of allKeys) {
keys.forEach(function (item: string) {
if (validateParamInConfig(item, keyType, config)) {
if (item === 'interpreters') {
finalConfig[item] = [];
const interpreters = config[item] as InterpreterConfig[];
const interpreters = config[item];
interpreters.forEach(function (eachInterpreter: InterpreterConfig) {
const interpreterConfig: InterpreterConfig = {};
for (const eachInterpreterParam in eachInterpreter) {
if (validateParamInConfig(eachInterpreterParam, 'string', eachInterpreter)) {
// eslint-disable-next-line
interpreterConfig[eachInterpreterParam] = eachInterpreter[eachInterpreterParam];
}
}
@@ -214,11 +219,12 @@ function validateConfig(configText: string, configType = 'toml') {
'',
);
finalConfig['interpreters'] = [];
const interpreters = config[item] as InterpreterConfig[];
const interpreters = config[item];
interpreters.forEach(function (eachInterpreter: InterpreterConfig) {
const interpreterConfig: InterpreterConfig = {};
for (const eachInterpreterParam in eachInterpreter) {
if (validateParamInConfig(eachInterpreterParam, 'string', eachInterpreter)) {
// eslint-disable-next-line
interpreterConfig[eachInterpreterParam] = eachInterpreter[eachInterpreterParam];
}
}
@@ -226,18 +232,20 @@ function validateConfig(configText: string, configType = 'toml') {
});
} else if (item === 'fetch') {
finalConfig[item] = [];
const fetchList = config[item] as FetchConfig[];
const fetchList = config[item];
fetchList.forEach(function (eachFetch: FetchConfig) {
const eachFetchConfig: FetchConfig = {};
for (const eachFetchConfigParam in eachFetch) {
const targetType = eachFetchConfigParam === 'files' ? 'array' : 'string';
if (validateParamInConfig(eachFetchConfigParam, targetType, eachFetch)) {
// eslint-disable-next-line
eachFetchConfig[eachFetchConfigParam] = eachFetch[eachFetchConfigParam];
}
}
finalConfig[item].push(eachFetchConfig);
});
} else {
// eslint-disable-next-line
finalConfig[item] = config[item];
}
}