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

[WIP] Study possibility of moving from js-yaml to yaml #4135

Closed
Closed
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
64 changes: 26 additions & 38 deletions .pnp.cjs

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

Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions packages/yarnpkg-parsers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"license": "BSD-2-Clause",
"main": "./sources/index.ts",
"dependencies": {
"js-yaml": "^3.10.0",
"tslib": "^1.13.0"
"tslib": "^1.13.0",
"yaml": "^2.0.0-10"
},
"devDependencies": {
"@types/js-yaml": "^3.11.1",
"js-yaml": "^3.10.0",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: clear this, just for comparing the parsers on the yarn.lock

"pegjs": "^0.10.0"
},
"scripts": {
Expand Down
23 changes: 17 additions & 6 deletions packages/yarnpkg-parsers/sources/syml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {safeLoad, FAILSAFE_SCHEMA} from 'js-yaml';

import {parse as parseYaml} from 'yaml';
import {parse} from './grammars/syml';

const simpleStringPattern = /^(?![-?:,\][{}#&*!|>'"%@` \t\r\n]).([ \t]*(?![,\][{}:# \t\r\n]).)*$/;
Expand Down Expand Up @@ -141,10 +140,22 @@ function parseViaJsYaml(source: string) {
if (LEGACY_REGEXP.test(source))
return parseViaPeg(source);

const value = safeLoad(source, {
schema: FAILSAFE_SCHEMA,
json: true,
});
// js-yaml@3 doesn't parse `key: true` as a boolean, but as a string.
// as far as I tested it makes the lock file invalid after install.
const reviver = (key: unknown, value: unknown) => {
if (value === true) {
return 'true';
} else if (value === false) {
return 'false';
}
return value;
}

const value = parseYaml(source, reviver, {
uniqueKeys: false,
schema: 'failsafe',
customTags: []
})

// Empty files are parsed as `undefined` instead of an empty object
// Empty files with 2 newlines or more are `null` instead
Expand Down
45 changes: 42 additions & 3 deletions packages/yarnpkg-parsers/tests/syml.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {parseSyml} from '@yarnpkg/parsers';
import {parseSyml} from '@yarnpkg/parsers';
import fs from 'fs';
import { performance } from 'perf_hooks';
import {safeLoad as parseWithJsYaml, FAILSAFE_SCHEMA} from 'js-yaml';
import {parse as parseWithYaml } from 'yaml';

describe(`Syml parser`, () => {
it(`shouldn't confuse old-style values with new-style keys`, () => {
Expand All @@ -21,13 +25,48 @@ 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('Yaml parse comparison (to be removed / reworked)', () => {
it(`should parse identically as js-yaml@3`, () => {

const lock = fs.readFileSync(`${__dirname}/../../../yarn.lock`, `utf8`);

// js-yaml@3 doesn't parse `key: true` as a boolean, but as a string.
// as far as I tested it makes the lock file invalid after install.
const reviver = (key, value) => {
if (value === true) {
return 'true';
} else if (value === false) {
return 'false';
}
return value;
}

const startJsYaml = performance.now();
const jsYamlParsed = parseWithJsYaml(lock, {schema: FAILSAFE_SCHEMA});
const endJsYaml = performance.now();
const jsYamlTime = endJsYaml - startJsYaml;

const startYaml = performance.now();
const yamlParsed = parseWithYaml(lock, reviver, {
uniqueKeys: false,
schema: 'failsafe',
customTags: []
})
const endYaml = performance.now();
const yamlTime = endYaml - startYaml;

expect(yamlParsed).toStrictEqual(jsYamlParsed);
expect(yamlTime).toBeLessThan(jsYamlTime);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current finding:

Expected: < 64.09353400021791 ms (js-yaml)
Received: 513.0383080020547 ms (yaml)

Yes that might be a real blocker (at least for parsing yarn.lock which is not required). Adding both libs might not be a good move :(

})
})
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4287,13 +4287,6 @@ __metadata:
languageName: node
linkType: hard

"@types/js-yaml@npm:^3.11.1":
version: 3.12.5
resolution: "@types/js-yaml@npm:3.12.5"
checksum: fdc09cda3cc83a0c67d69ce179af9ede6e865f2e54f15b138814c5255b3b279306de2232da7cdea138248d885c960359ebf7f0bd21010e0ecf027f64454ab217
languageName: node
linkType: hard

"@types/json-patch@npm:0.0.30":
version: 0.0.30
resolution: "@types/json-patch@npm:0.0.30"
Expand Down Expand Up @@ -5773,10 +5766,10 @@ __metadata:
version: 0.0.0-use.local
resolution: "@yarnpkg/parsers@workspace:packages/yarnpkg-parsers"
dependencies:
"@types/js-yaml": ^3.11.1
js-yaml: ^3.10.0
pegjs: ^0.10.0
tslib: ^1.13.0
yaml: ^2.0.0-10
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -27468,6 +27461,13 @@ typescript@^3.8.3:
languageName: node
linkType: hard

"yaml@npm:^2.0.0-10":
version: 2.0.0-10
resolution: "yaml@npm:2.0.0-10"
checksum: 7a0bac2f80225da6008fb8ab887f43166fb2485cc3e1350185c43499c76841ea0ca9d79b40c0970e08ab3943f25ffb184e709fef7fb8b26d73987ed0452a91cc
languageName: node
linkType: hard

"yargs-parser@npm:^18.1.2":
version: 18.1.3
resolution: "yargs-parser@npm:18.1.3"
Expand Down