Skip to content

Commit 577e022

Browse files
eladotrivikrarcanis
authored
fix(parsers): correctly stringify keys longer than 1024 chars (#4275)
* Fix Yaml/Syml stringify with keys longer than 1024 chars * yarn version check * Update packages/yarnpkg-parsers/sources/syml.ts Co-authored-by: Trivikram Kamat <[email protected]> * formatting * Update syml.ts Co-authored-by: Trivikram Kamat <[email protected]> Co-authored-by: Maël Nison <[email protected]>
1 parent c45d9b9 commit 577e022

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

.yarn/versions/7041d864.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
releases:
2+
"@yarnpkg/builder": patch
3+
"@yarnpkg/cli": patch
4+
"@yarnpkg/core": patch
5+
"@yarnpkg/doctor": patch
6+
"@yarnpkg/nm": patch
7+
"@yarnpkg/parsers": patch
8+
"@yarnpkg/plugin-compat": patch
9+
"@yarnpkg/plugin-constraints": patch
10+
"@yarnpkg/plugin-dlx": patch
11+
"@yarnpkg/plugin-essentials": patch
12+
"@yarnpkg/plugin-exec": patch
13+
"@yarnpkg/plugin-file": patch
14+
"@yarnpkg/plugin-git": patch
15+
"@yarnpkg/plugin-github": patch
16+
"@yarnpkg/plugin-http": patch
17+
"@yarnpkg/plugin-init": patch
18+
"@yarnpkg/plugin-interactive-tools": patch
19+
"@yarnpkg/plugin-link": patch
20+
"@yarnpkg/plugin-nm": patch
21+
"@yarnpkg/plugin-npm": patch
22+
"@yarnpkg/plugin-npm-cli": patch
23+
"@yarnpkg/plugin-pack": patch
24+
"@yarnpkg/plugin-patch": patch
25+
"@yarnpkg/plugin-pnp": patch
26+
"@yarnpkg/plugin-pnpm": patch
27+
"@yarnpkg/plugin-stage": patch
28+
"@yarnpkg/plugin-typescript": patch
29+
"@yarnpkg/plugin-version": patch
30+
"@yarnpkg/plugin-workspace-tools": patch
31+
"@yarnpkg/pnpify": patch
32+
"@yarnpkg/sdks": patch
33+
"@yarnpkg/shell": patch

packages/yarnpkg-parsers/sources/syml.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,16 @@ function stringifyValue(value: any, indentLevel: number, newLineIfObject: boolea
9191
? indent
9292
: ``;
9393

94-
if (stringifiedValue.startsWith(`\n`)) {
95-
return `${recordIndentation}${stringifiedKey}:${stringifiedValue}`;
96-
} else {
97-
return `${recordIndentation}${stringifiedKey}: ${stringifiedValue}`;
98-
}
94+
// Yaml 1.2 spec says that keys over 1024 characters need to be prefixed with ? and the : goes in a new line
95+
const keyPart = stringifiedKey.length > 1024
96+
? `? ${stringifiedKey}\n:`
97+
: `${stringifiedKey}:`;
98+
99+
const valuePart = stringifiedValue.startsWith(`\n`)
100+
? stringifiedValue
101+
: ` ${stringifiedValue}`;
102+
103+
return `${recordIndentation}${keyPart}${valuePart}`;
99104
}).join(indentLevel === 0 ? `\n` : ``) || `\n`;
100105

101106
if (!newLineIfObject) {

packages/yarnpkg-parsers/tests/syml.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {parseSyml} from '@yarnpkg/parsers';
1+
import {parseSyml, stringifySyml} from '@yarnpkg/parsers';
22

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

2222
it(`should merge duplicates`, () => {
2323
expect(
24-
parseSyml(`
24+
parseSyml(`
2525
"lodash@npm:^4.17.20":
2626
version: 4.17.20
27-
27+
2828
"lodash@npm:^4.17.20":
2929
version: 4.17.20
3030
`),
3131
).toEqual({'lodash@npm:^4.17.20': {version: `4.17.20`}});
3232
});
3333
});
34+
35+
describe(`Syml stringifyer`, () => {
36+
it(`stringifies an object`, () => {
37+
expect(stringifySyml({foo: {bar: `true`, baz: `quux`}})).toEqual(`foo:\n bar: true\n baz: quux\n`);
38+
});
39+
40+
it(`stringifies an object with a long key with yaml 1.2 spec`, () => {
41+
const longKey = `a`.repeat(1025); // long key is a string of length > 1024
42+
expect(stringifySyml({[longKey]: {bar: `true`, baz: `quux`}})).toEqual(`? ${longKey}\n:\n bar: true\n baz: quux\n`);
43+
expect(stringifySyml({[longKey]: {[longKey]: `quux`, baz: `quux`}})).toEqual(`? ${longKey}\n:\n ? ${longKey}\n: quux\n baz: quux\n`);
44+
});
45+
});

0 commit comments

Comments
 (0)