fix: Use toml-eslint-parser instead of @iarna/toml (#25594)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
Sergei Zharinov 2023-11-06 12:34:52 -03:00 committed by GitHub
parent e7028951d0
commit d82ff3659f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 62 additions and 23 deletions

View file

@ -1,7 +1,7 @@
import { parse } from '@iarna/toml';
import { logger } from '../../../logger'; import { logger } from '../../../logger';
import type { SkipReason } from '../../../types'; import type { SkipReason } from '../../../types';
import { findLocalSiblingOrParent, readLocalFile } from '../../../util/fs'; import { findLocalSiblingOrParent, readLocalFile } from '../../../util/fs';
import { parse as parseToml } from '../../../util/toml';
import { CrateDatasource } from '../../datasource/crate'; import { CrateDatasource } from '../../datasource/crate';
import type { import type {
ExtractConfig, ExtractConfig,
@ -133,7 +133,7 @@ async function readCargoConfig(): Promise<CargoConfig | null> {
const payload = await readLocalFile(path, 'utf8'); const payload = await readLocalFile(path, 'utf8');
if (payload) { if (payload) {
try { try {
return parse(payload) as CargoConfig; return parseToml(payload) as CargoConfig;
} catch (err) { } catch (err) {
logger.debug({ err }, `Error parsing ${path}`); logger.debug({ err }, `Error parsing ${path}`);
} }
@ -214,7 +214,7 @@ export async function extractPackageFile(
let cargoManifest: CargoManifest; let cargoManifest: CargoManifest;
try { try {
cargoManifest = parse(content); cargoManifest = parseToml(content) as CargoManifest;
} catch (err) { } catch (err) {
logger.debug({ err, packageFile }, 'Error parsing Cargo.toml file'); logger.debug({ err, packageFile }, 'Error parsing Cargo.toml file');
return null; return null;

View file

@ -1,9 +1,9 @@
import { parse } from '@iarna/toml';
import is from '@sindresorhus/is'; import is from '@sindresorhus/is';
import deepmerge from 'deepmerge'; import deepmerge from 'deepmerge';
import type { SkipReason } from '../../../../types'; import type { SkipReason } from '../../../../types';
import { hasKey } from '../../../../util/object'; import { hasKey } from '../../../../util/object';
import { escapeRegExp, regEx } from '../../../../util/regex'; import { escapeRegExp, regEx } from '../../../../util/regex';
import { parse as parseToml } from '../../../../util/toml';
import type { PackageDependency } from '../../types'; import type { PackageDependency } from '../../types';
import type { import type {
GradleCatalog, GradleCatalog,
@ -246,7 +246,7 @@ export function parseCatalog(
packageFile: string, packageFile: string,
content: string content: string
): PackageDependency<GradleManagerData>[] { ): PackageDependency<GradleManagerData>[] {
const tomlContent = parse(content) as GradleCatalog; const tomlContent = parseToml(content) as GradleCatalog;
const versions = tomlContent.versions ?? {}; const versions = tomlContent.versions ?? {};
const libs = tomlContent.libraries ?? {}; const libs = tomlContent.libraries ?? {};
const libStartIndex = content.indexOf('libraries'); const libStartIndex = content.indexOf('libraries');

View file

@ -1,7 +1,7 @@
import toml from '@iarna/toml';
import is from '@sindresorhus/is'; import is from '@sindresorhus/is';
import { logger } from '../../../logger'; import { logger } from '../../../logger';
import { regEx } from '../../../util/regex'; import { regEx } from '../../../util/regex';
import { parse as parseToml } from '../../../util/toml';
import { PypiDatasource } from '../../datasource/pypi'; import { PypiDatasource } from '../../datasource/pypi';
import type { PackageDependency } from '../types'; import type { PackageDependency } from '../types';
import { PyProject, PyProjectSchema } from './schema'; import { PyProject, PyProjectSchema } from './schema';
@ -113,7 +113,7 @@ export function parsePyProject(
content: string content: string
): PyProject | null { ): PyProject | null {
try { try {
const jsonMap = toml.parse(content); const jsonMap = parseToml(content);
return PyProjectSchema.parse(jsonMap); return PyProjectSchema.parse(jsonMap);
} catch (err) { } catch (err) {
logger.debug( logger.debug(

View file

@ -1,10 +1,10 @@
import toml from '@iarna/toml';
import { RANGE_PATTERN } from '@renovatebot/pep440'; import { RANGE_PATTERN } from '@renovatebot/pep440';
import is from '@sindresorhus/is'; import is from '@sindresorhus/is';
import { logger } from '../../../logger'; import { logger } from '../../../logger';
import type { SkipReason } from '../../../types'; import type { SkipReason } from '../../../types';
import { localPathExists } from '../../../util/fs'; import { localPathExists } from '../../../util/fs';
import { regEx } from '../../../util/regex'; import { regEx } from '../../../util/regex';
import { parse as parseToml } from '../../../util/toml';
import { PypiDatasource } from '../../datasource/pypi'; import { PypiDatasource } from '../../datasource/pypi';
import type { PackageDependency, PackageFileContent } from '../types'; import type { PackageDependency, PackageFileContent } from '../types';
import type { PipFile } from './types'; import type { PipFile } from './types';
@ -110,7 +110,7 @@ export async function extractPackageFile(
let pipfile: PipFile; let pipfile: PipFile;
try { try {
// TODO: fix type (#9610) // TODO: fix type (#9610)
pipfile = toml.parse(content) as any; pipfile = parseToml(content) as any;
} catch (err) { } catch (err) {
logger.debug({ err, packageFile }, 'Error parsing Pipfile'); logger.debug({ err, packageFile }, 'Error parsing Pipfile');
return null; return null;

View file

@ -1,4 +1,3 @@
import { parse } from '@iarna/toml';
import is from '@sindresorhus/is'; import is from '@sindresorhus/is';
import { quote } from 'shlex'; import { quote } from 'shlex';
import { TEMPORARY_ERROR } from '../../../constants/error-messages'; import { TEMPORARY_ERROR } from '../../../constants/error-messages';
@ -17,6 +16,7 @@ import { getGitEnvironmentVariables } from '../../../util/git/auth';
import { find } from '../../../util/host-rules'; import { find } from '../../../util/host-rules';
import { regEx } from '../../../util/regex'; import { regEx } from '../../../util/regex';
import { Result } from '../../../util/result'; import { Result } from '../../../util/result';
import { parse as parseToml } from '../../../util/toml';
import { PypiDatasource } from '../../datasource/pypi'; import { PypiDatasource } from '../../datasource/pypi';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types'; import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
import { Lockfile, PoetrySchemaToml } from './schema'; import { Lockfile, PoetrySchemaToml } from './schema';
@ -88,7 +88,7 @@ export function getPoetryRequirement(
function getPoetrySources(content: string, fileName: string): PoetrySource[] { function getPoetrySources(content: string, fileName: string): PoetrySource[] {
let pyprojectFile: PoetryFile; let pyprojectFile: PoetryFile;
try { try {
pyprojectFile = parse(content); pyprojectFile = parseToml(content) as PoetryFile;
} catch (err) { } catch (err) {
logger.debug({ err }, 'Error parsing pyproject.toml file'); logger.debug({ err }, 'Error parsing pyproject.toml file');
return []; return [];

View file

@ -1,9 +1,9 @@
import { JsonMap, parse } from '@iarna/toml';
import { load, loadAll } from 'js-yaml'; import { load, loadAll } from 'js-yaml';
import JSON5 from 'json5'; import JSON5 from 'json5';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import type { JsonArray, JsonValue } from 'type-fest'; import type { JsonArray, JsonValue } from 'type-fest';
import { z } from 'zod'; import { z } from 'zod';
import { parse as parseToml } from './toml';
interface ErrorContext<T> { interface ErrorContext<T> {
error: z.ZodError; error: z.ZodError;
@ -244,9 +244,9 @@ export const MultidocYaml = z.string().transform((str, ctx): JsonArray => {
} }
}); });
export const Toml = z.string().transform((str, ctx): JsonMap => { export const Toml = z.string().transform((str, ctx) => {
try { try {
return parse(str); return parseToml(str);
} catch (e) { } catch (e) {
ctx.addIssue({ code: 'custom', message: 'Invalid TOML' }); ctx.addIssue({ code: 'custom', message: 'Invalid TOML' });
return z.NEVER; return z.NEVER;

31
lib/util/toml.spec.ts Normal file
View file

@ -0,0 +1,31 @@
import { codeBlock } from 'common-tags';
import { parse as parseToml } from './toml';
describe('util/toml', () => {
it('works', () => {
const input = codeBlock`
[tool.poetry]
## Hello world
include = [
"README.md",
{ path = "tests", format = "sdist" }
]
`;
expect(parseToml(input)).toStrictEqual({
tool: {
poetry: {
include: ['README.md', { path: 'tests', format: 'sdist' }],
},
},
});
});
it('handles invalid toml', () => {
const input = codeBlock`
!@#$%^&*()
`;
expect(() => parseToml(input)).toThrow(SyntaxError);
});
});

6
lib/util/toml.ts Normal file
View file

@ -0,0 +1,6 @@
import { getStaticTOMLValue, parseTOML } from 'toml-eslint-parser';
export function parse(input: string): unknown {
const ast = parseTOML(input);
return getStaticTOMLValue(ast);
}

View file

@ -151,7 +151,6 @@
"@aws-sdk/credential-providers": "3.363.0", "@aws-sdk/credential-providers": "3.363.0",
"@breejs/later": "4.1.0", "@breejs/later": "4.1.0",
"@cdktf/hcl2json": "0.19.0", "@cdktf/hcl2json": "0.19.0",
"@iarna/toml": "3.0.0",
"@opentelemetry/api": "1.6.0", "@opentelemetry/api": "1.6.0",
"@opentelemetry/context-async-hooks": "1.17.0", "@opentelemetry/context-async-hooks": "1.17.0",
"@opentelemetry/exporter-trace-otlp-http": "0.43.0", "@opentelemetry/exporter-trace-otlp-http": "0.43.0",
@ -241,6 +240,7 @@
"simple-git": "3.20.0", "simple-git": "3.20.0",
"slugify": "1.6.6", "slugify": "1.6.6",
"source-map-support": "0.5.21", "source-map-support": "0.5.21",
"toml-eslint-parser": "0.6.0",
"traverse": "0.6.7", "traverse": "0.6.7",
"tslib": "2.6.2", "tslib": "2.6.2",
"upath": "2.0.1", "upath": "2.0.1",

View file

@ -32,9 +32,6 @@ importers:
'@cdktf/hcl2json': '@cdktf/hcl2json':
specifier: 0.19.0 specifier: 0.19.0
version: 0.19.0 version: 0.19.0
'@iarna/toml':
specifier: 3.0.0
version: 3.0.0
'@opentelemetry/api': '@opentelemetry/api':
specifier: 1.6.0 specifier: 1.6.0
version: 1.6.0 version: 1.6.0
@ -302,6 +299,9 @@ importers:
source-map-support: source-map-support:
specifier: 0.5.21 specifier: 0.5.21
version: 0.5.21 version: 0.5.21
toml-eslint-parser:
specifier: 0.6.0
version: 0.6.0
traverse: traverse:
specifier: 0.6.7 specifier: 0.6.7
version: 0.6.7 version: 0.6.7
@ -2007,10 +2007,6 @@ packages:
engines: {node: ^14.18.0 || >=16.0.0} engines: {node: ^14.18.0 || >=16.0.0}
dev: true dev: true
/@iarna/toml@3.0.0:
resolution: {integrity: sha512-td6ZUkz2oS3VeleBcN+m//Q6HlCFCPrnI0FZhrt/h4XqLEdOyYp2u21nd8MdsR+WJy5r9PTDaHTDDfhf4H4l6Q==}
dev: false
/@isaacs/cliui@8.0.2: /@isaacs/cliui@8.0.2:
resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==} resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==}
engines: {node: '>=12'} engines: {node: '>=12'}
@ -6041,7 +6037,6 @@ packages:
/eslint-visitor-keys@3.4.3: /eslint-visitor-keys@3.4.3:
resolution: {integrity: sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==} resolution: {integrity: sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
dev: true
/eslint@8.52.0: /eslint@8.52.0:
resolution: {integrity: sha512-zh/JHnaixqHZsolRB/w9/02akBk9EPrOs9JwcTP2ek7yL5bVvXuRariiaAjjoJ5DvuwQ1WAE/HsMz+w17YgBCg==} resolution: {integrity: sha512-zh/JHnaixqHZsolRB/w9/02akBk9EPrOs9JwcTP2ek7yL5bVvXuRariiaAjjoJ5DvuwQ1WAE/HsMz+w17YgBCg==}
@ -10580,6 +10575,13 @@ packages:
dependencies: dependencies:
is-number: 7.0.0 is-number: 7.0.0
/toml-eslint-parser@0.6.0:
resolution: {integrity: sha512-aTmQa0RFb+2URe8IZOfo/oxt3b5rlXlpG9xE+6FmeI8immCGLnZYvKVxbnCYJx4bIKIaEwl0BnCDhwO70yeWSA==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
dependencies:
eslint-visitor-keys: 3.4.3
dev: false
/tr46@0.0.3: /tr46@0.0.3:
resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==} resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==}
dev: false dev: false