Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/modules/manager/cargo/extract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parse } from '@iarna/toml';
import { logger } from '../../../logger';
import type { SkipReason } from '../../../types';
import { findLocalSiblingOrParent, readLocalFile } from '../../../util/fs';
import { parse as parseToml } from '../../../util/toml';
import { CrateDatasource } from '../../datasource/crate';
import type {
ExtractConfig,
Expand Down Expand Up @@ -133,7 +133,7 @@ async function readCargoConfig(): Promise<CargoConfig | null> {
const payload = await readLocalFile(path, 'utf8');
if (payload) {
try {
return parse(payload) as CargoConfig;
return parseToml(payload) as CargoConfig;
} catch (err) {
logger.debug({ err }, `Error parsing ${path}`);
}
Expand Down Expand Up @@ -214,7 +214,7 @@ export async function extractPackageFile(

let cargoManifest: CargoManifest;
try {
cargoManifest = parse(content);
cargoManifest = parseToml(content) as CargoManifest;
} catch (err) {
logger.debug({ err, packageFile }, 'Error parsing Cargo.toml file');
return null;
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/manager/gradle/extract/catalog.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { parse } from '@iarna/toml';
import is from '@sindresorhus/is';
import deepmerge from 'deepmerge';
import type { SkipReason } from '../../../../types';
import { hasKey } from '../../../../util/object';
import { escapeRegExp, regEx } from '../../../../util/regex';
import { parse as parseToml } from '../../../../util/toml';
import type { PackageDependency } from '../../types';
import type {
GradleCatalog,
Expand Down Expand Up @@ -246,7 +246,7 @@ export function parseCatalog(
packageFile: string,
content: string
): PackageDependency<GradleManagerData>[] {
const tomlContent = parse(content) as GradleCatalog;
const tomlContent = parseToml(content) as GradleCatalog;
const versions = tomlContent.versions ?? {};
const libs = tomlContent.libraries ?? {};
const libStartIndex = content.indexOf('libraries');
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/manager/pep621/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import toml from '@iarna/toml';
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { regEx } from '../../../util/regex';
import { parse as parseToml } from '../../../util/toml';
import { PypiDatasource } from '../../datasource/pypi';
import type { PackageDependency } from '../types';
import { PyProject, PyProjectSchema } from './schema';
Expand Down Expand Up @@ -113,7 +113,7 @@ export function parsePyProject(
content: string
): PyProject | null {
try {
const jsonMap = toml.parse(content);
const jsonMap = parseToml(content);
return PyProjectSchema.parse(jsonMap);
} catch (err) {
logger.debug(
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/manager/pipenv/extract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import toml from '@iarna/toml';
import { RANGE_PATTERN } from '@renovatebot/pep440';
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import type { SkipReason } from '../../../types';
import { localPathExists } from '../../../util/fs';
import { regEx } from '../../../util/regex';
import { parse as parseToml } from '../../../util/toml';
import { PypiDatasource } from '../../datasource/pypi';
import type { PackageDependency, PackageFileContent } from '../types';
import type { PipFile } from './types';
Expand Down Expand Up @@ -110,7 +110,7 @@ export async function extractPackageFile(
let pipfile: PipFile;
try {
// TODO: fix type (#9610)
pipfile = toml.parse(content) as any;
pipfile = parseToml(content) as any;
} catch (err) {
logger.debug({ err, packageFile }, 'Error parsing Pipfile');
return null;
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/manager/poetry/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { parse } from '@iarna/toml';
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import { TEMPORARY_ERROR } from '../../../constants/error-messages';
Expand All @@ -17,6 +16,7 @@ import { getGitEnvironmentVariables } from '../../../util/git/auth';
import { find } from '../../../util/host-rules';
import { regEx } from '../../../util/regex';
import { Result } from '../../../util/result';
import { parse as parseToml } from '../../../util/toml';
import { PypiDatasource } from '../../datasource/pypi';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
import { Lockfile, PoetrySchemaToml } from './schema';
Expand Down Expand Up @@ -88,7 +88,7 @@ export function getPoetryRequirement(
function getPoetrySources(content: string, fileName: string): PoetrySource[] {
let pyprojectFile: PoetryFile;
try {
pyprojectFile = parse(content);
pyprojectFile = parseToml(content) as PoetryFile;
} catch (err) {
logger.debug({ err }, 'Error parsing pyproject.toml file');
return [];
Expand Down
6 changes: 3 additions & 3 deletions lib/util/schema-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { JsonMap, parse } from '@iarna/toml';
import { load, loadAll } from 'js-yaml';
import JSON5 from 'json5';
import { DateTime } from 'luxon';
import type { JsonArray, JsonValue } from 'type-fest';
import { z } from 'zod';
import { parse as parseToml } from './toml';

interface ErrorContext<T> {
error: z.ZodError;
Expand Down Expand Up @@ -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 {
return parse(str);
return parseToml(str);
} catch (e) {
ctx.addIssue({ code: 'custom', message: 'Invalid TOML' });
return z.NEVER;
Expand Down
31 changes: 31 additions & 0 deletions lib/util/toml.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
7 changes: 7 additions & 0 deletions lib/util/toml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getStaticTOMLValue, parseTOML } from 'toml-eslint-parser';
import type { JsonValue } from 'type-fest';

export function parse(input: string): JsonValue {
zharinov marked this conversation as resolved.
Show resolved Hide resolved
const ast = parseTOML(input);
return getStaticTOMLValue(ast) as JsonValue;
rarkins marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
"@aws-sdk/credential-providers": "3.363.0",
"@breejs/later": "4.1.0",
"@cdktf/hcl2json": "0.19.0",
"@iarna/toml": "3.0.0",
"@opentelemetry/api": "1.6.0",
"@opentelemetry/context-async-hooks": "1.17.0",
"@opentelemetry/exporter-trace-otlp-http": "0.43.0",
Expand Down Expand Up @@ -241,6 +240,7 @@
"simple-git": "3.20.0",
"slugify": "1.6.6",
"source-map-support": "0.5.21",
"toml-eslint-parser": "0.6.0",
"traverse": "0.6.7",
"tslib": "2.6.2",
"upath": "2.0.1",
Expand Down
18 changes: 10 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.