diff --git a/.yarn/versions/7041d864.yml b/.yarn/versions/7041d864.yml new file mode 100644 index 000000000000..fe14fd26bb1f --- /dev/null +++ b/.yarn/versions/7041d864.yml @@ -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 diff --git a/packages/yarnpkg-parsers/sources/syml.ts b/packages/yarnpkg-parsers/sources/syml.ts index 498df852d869..db5afceff162 100644 --- a/packages/yarnpkg-parsers/sources/syml.ts +++ b/packages/yarnpkg-parsers/sources/syml.ts @@ -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) { diff --git a/packages/yarnpkg-parsers/tests/syml.test.js b/packages/yarnpkg-parsers/tests/syml.test.js index 259e8413b7b3..f1a307c369ae 100644 --- a/packages/yarnpkg-parsers/tests/syml.test.js +++ b/packages/yarnpkg-parsers/tests/syml.test.js @@ -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`, () => { @@ -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`); + }); +});