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(parsers): correctly stringify keys longer than 1024 chars #4275

Merged
merged 5 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions .yarn/versions/7041d864.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
releases:
"@yarnpkg/builder": patch
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/doctor": patch
"@yarnpkg/nm": patch
"@yarnpkg/parsers": patch
"@yarnpkg/plugin-compat": patch
"@yarnpkg/plugin-constraints": patch
"@yarnpkg/plugin-dlx": patch
"@yarnpkg/plugin-essentials": patch
"@yarnpkg/plugin-exec": patch
"@yarnpkg/plugin-file": patch
"@yarnpkg/plugin-git": patch
"@yarnpkg/plugin-github": patch
"@yarnpkg/plugin-http": patch
"@yarnpkg/plugin-init": patch
"@yarnpkg/plugin-interactive-tools": patch
"@yarnpkg/plugin-link": patch
"@yarnpkg/plugin-nm": patch
"@yarnpkg/plugin-npm": patch
"@yarnpkg/plugin-npm-cli": patch
"@yarnpkg/plugin-pack": patch
"@yarnpkg/plugin-patch": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/plugin-pnpm": patch
"@yarnpkg/plugin-stage": patch
"@yarnpkg/plugin-typescript": patch
"@yarnpkg/plugin-version": patch
"@yarnpkg/plugin-workspace-tools": patch
"@yarnpkg/pnpify": patch
"@yarnpkg/sdks": patch
"@yarnpkg/shell": patch
20 changes: 15 additions & 5 deletions packages/yarnpkg-parsers/sources/syml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,21 @@ function stringifyValue(value: any, indentLevel: number, newLineIfObject: boolea
? indent
: ``;

if (stringifiedValue.startsWith(`\n`)) {
return `${recordIndentation}${stringifiedKey}:${stringifiedValue}`;
} else {
return `${recordIndentation}${stringifiedKey}: ${stringifiedValue}`;
}

let keyPart: string;
let valuePart: string;

// Yaml 1.2 spec says that keys over 1024 characters need to be prefixed with ? and the : goes in a new line
if (stringifiedKey.length > 1024)
keyPart = `? ${stringifiedKey}\n:`;
else keyPart = `${stringifiedKey}:`;

if (stringifiedValue.startsWith(`\n`))
valuePart = stringifiedValue;
else valuePart = ` ${stringifiedValue}`;
elado marked this conversation as resolved.
Show resolved Hide resolved


return `${recordIndentation}${keyPart}${valuePart}`;
}).join(indentLevel === 0 ? `\n` : ``) || `\n`;

if (!newLineIfObject) {
Expand Down
18 changes: 15 additions & 3 deletions packages/yarnpkg-parsers/tests/syml.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {parseSyml} from '@yarnpkg/parsers';
import {parseSyml, stringifySyml} from '@yarnpkg/parsers';

describe(`Syml parser`, () => {
it(`shouldn't confuse old-style values with new-style keys`, () => {
Expand All @@ -21,13 +21,25 @@ describe(`Syml parser`, () => {

it(`should merge duplicates`, () => {
expect(
parseSyml(`
parseSyml(`
"lodash@npm:^4.17.20":
version: 4.17.20

"lodash@npm:^4.17.20":
version: 4.17.20
`),
).toEqual({'lodash@npm:^4.17.20': {version: `4.17.20`}});
});
});

describe(`Syml stringifyer`, () => {
it(`stringifies an object`, () => {
expect(stringifySyml({foo: {bar: `true`, baz: `quux`}})).toEqual(`foo:\n bar: true\n baz: quux\n`);
});

it(`stringifies an object with a long key with yaml 1.2 spec`, () => {
const longKey = `a`.repeat(1025); // long key is a string of length > 1024
expect(stringifySyml({[longKey]: {bar: `true`, baz: `quux`}})).toEqual(`? ${longKey}\n:\n bar: true\n baz: quux\n`);
expect(stringifySyml({[longKey]: {[longKey]: `quux`, baz: `quux`}})).toEqual(`? ${longKey}\n:\n ? ${longKey}\n: quux\n baz: quux\n`);
});
});