Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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
15 changes: 10 additions & 5 deletions packages/yarnpkg-parsers/sources/syml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,16 @@ function stringifyValue(value: any, indentLevel: number, newLineIfObject: boolea
? indent
: ``;

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

const valuePart = stringifiedValue.startsWith(`\n`)
? stringifiedValue
: ` ${stringifiedValue}`;

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`);
});
});