-
-
Notifications
You must be signed in to change notification settings - Fork 96
Update dependency js-yaml to v5 #3289
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
Merged
+80
−60
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,70 +1,82 @@ | ||
| // From https://github.com/nodeca/js-yaml/issues/586#issuecomment-814310104 | ||
| // This file ensures that simple objects and arrays (ie without array or object | ||
| // children) will be serialized inline, and also ensures that "fullTargets" will be inlined as well | ||
|
|
||
| import { dump, Type, DEFAULT_SCHEMA } from "js-yaml"; | ||
| import type { DumpOptions } from "js-yaml"; | ||
| import type { Document, MappingNode, Node, SequenceNode } from "js-yaml"; | ||
| import { CORE_SCHEMA, dump, visit, YAML11_SCHEMA } from "js-yaml"; | ||
|
|
||
| class CustomDump { | ||
| constructor( | ||
| private readonly data: unknown, | ||
| private readonly opts: DumpOptions, | ||
| ) {} | ||
|
|
||
| represent(): string { | ||
| let result = dump(this.data, { replacer, schema, ...this.opts }); | ||
| result = result.trim(); | ||
| if (result.includes("\n")) { | ||
| result = `\n${result}`; | ||
| } | ||
| return result; | ||
| } | ||
| export function serialize(obj: unknown): string { | ||
| return dump(toSerializableObject(obj), { | ||
| noRefs: true, | ||
| quoteStyle: "double", | ||
| // CORE_SCHEMA preserves existing fixture output for numeric-looking strings | ||
| // like `1_01_001`, which js-yaml's default schema would quote. | ||
| schema: CORE_SCHEMA, | ||
| transform: inlineSimpleCollections, | ||
| }); | ||
| } | ||
|
|
||
| const customDumpType = new Type("!format", { | ||
| kind: "scalar", | ||
| resolve: () => false, | ||
| instanceOf: CustomDump, | ||
| represent: (d: unknown) => (d as CustomDump).represent(), | ||
| }); | ||
|
|
||
| const schema = DEFAULT_SCHEMA.extend({ implicit: [customDumpType] }); | ||
|
|
||
| const isObject = (value: unknown): value is object => | ||
| typeof value === "object" && value != null; | ||
|
|
||
| function hasSimpleChildren(value: unknown): boolean { | ||
| if (isObject(value)) { | ||
| return Object.values(value).every( | ||
| (value) => !isObject(value) && !Array.isArray(value), | ||
| ); | ||
| } | ||
| function toSerializableObject(value: unknown): unknown { | ||
| if (Array.isArray(value)) { | ||
| return value.every((value) => !isObject(value) && !Array.isArray(value)); | ||
| return value.map(toSerializableObject); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function replacer(key: string, value: unknown): unknown { | ||
| // top-level, don't change this | ||
| if (key === "") { | ||
| if (value == null || typeof value !== "object") { | ||
| return value; | ||
| } | ||
|
|
||
| if (hasSimpleChildren(value)) { | ||
| return new CustomDump(value, { flowLevel: 0 }); | ||
| } | ||
| return Object.fromEntries( | ||
| Object.entries(value).map(([key, value]) => [ | ||
| key, | ||
| toSerializableObject(value), | ||
| ]), | ||
| ); | ||
| } | ||
|
|
||
| function inlineSimpleCollections(documents: Document[]): void { | ||
| visit(documents, (node: Node, { depth, isKey, parent }) => { | ||
| // Keep nested simple objects and arrays in flow style, eg `{line: 0}` and | ||
| // `[default.a]`, while leaving top-level mappings in block style. | ||
| if (depth > 0 && isCollectionNode(node) && hasOnlyScalarChildren(node)) { | ||
| node.style.flow = true; | ||
| } | ||
|
|
||
| // default | ||
| return value; | ||
| // Existing fixtures use single quotes for flow scalar values that need | ||
| // quoting, eg `{character: ']'}`. Do not apply this to keys, non-string | ||
| // scalars, block scalars, or multiline strings. | ||
| if ( | ||
| !isKey && | ||
| parent != null && | ||
| parent.style.flow && | ||
| node.kind === "scalar" && | ||
| node.tag === "tag:yaml.org,2002:str" && | ||
| !node.value.includes("\n") && | ||
| scalarNeedsQuotesInFlow(node.value) | ||
| ) { | ||
| node.style.singleQuoted = true; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| export function serialize(obj: unknown): string { | ||
| const dump = new CustomDump(obj, { | ||
| noRefs: true, | ||
| quotingType: '"', | ||
| }) | ||
| .represent() | ||
| .trim(); | ||
| return `${dump}\n`; | ||
| function isCollectionNode(node: Node): node is MappingNode | SequenceNode { | ||
| return node.kind === "mapping" || node.kind === "sequence"; | ||
| } | ||
|
|
||
| function hasOnlyScalarChildren(node: MappingNode | SequenceNode): boolean { | ||
| return node.kind === "mapping" | ||
| ? node.items.every((item) => item.value.kind === "scalar") | ||
| : node.items.every((item) => item.kind === "scalar"); | ||
| } | ||
|
|
||
| function scalarNeedsQuotesInFlow(value: string): boolean { | ||
| // Dump the value inside a flow array so js-yaml applies the same scalar | ||
| // rules it would use for an inline collection. If the result starts with | ||
| // `["`, the value cannot be emitted plainly in flow style, so we switch the | ||
| // actual fixture value to single quotes to match the existing fixtures. | ||
| return dump([value], { | ||
| flowLevel: 0, | ||
| quoteStyle: "double", | ||
| // Use YAML 1.1 schema to preserve existing fixtures where legacy | ||
| // boolean-like strings such as `y` and `n` are quoted in flow collections. | ||
| schema: YAML11_SCHEMA, | ||
| }).startsWith('["'); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.