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

Make Pair not extend NodeBase; drop its prop forwarding #250

Merged
merged 5 commits into from Apr 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/05_content_nodes.md
Expand Up @@ -46,7 +46,7 @@ On the other hand, `!!int` and `!!float` stringifiers will take `format` into ac
## Collections

```js
class Pair<K = unknown, V = unknown> extends NodeBase {
class Pair<K = unknown, V = unknown> {
key: K // When parsed, key and value are always
value: V // Node or null, but can be set to anything
}
Expand Down
11 changes: 9 additions & 2 deletions docs/07_parsing_yaml.md
Expand Up @@ -187,10 +187,17 @@ Some of the most common node properties include:
| `offset` | `number` | The start index within the source string or character stream. |
| `source` | `string` | A raw string representation of the node's value, including all newlines and indentation. |
| `indent` | `number` | The indent level of the current line; mostly just for internal use. |
| `items` | `{ ... }[]` | The contents of a collection; shape depends on the collection type, and may include `key: Token` and `value: Token`. |
| `items` | `Item[]` | The contents of a collection; exact shape depends on the collection type. |
| `start`, `sep`, `end` | `SourceToken[]` | Content before, within, and after "actual" values. Includes item and collection indicators, anchors, tags, comments, as well as other things. |

As an implementation detail, block and flow collections are parsed and presented rather differently due to their structural differences.
Collection items contain some subset of the following properties:

| Item property | Type | Description |
| ------------- | --------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `start` | `SourceToken[]` | Always defined. Content before the actual value. May include comments that are later assigned to the preceding item. |
| `key` | `Token ⎮ null` | Set for key/value pairs only, so never used in block sequences. |
| `sep` | `SourceToken[]` | Content between the key and the value. If defined, indicates that the `key` logically exists, even if its value is `null`. |
| `value` | `Token ⎮ null` | The value. Normally set, but may be left out for e.g. explicit keys with no matching value. |

### Counting Lines

Expand Down
2 changes: 1 addition & 1 deletion docs/08_errors.md
Expand Up @@ -28,6 +28,7 @@ To identify errors for special handling, you should primarily use `code` to diff
| `BAD_DIRECTIVE` | Only the `%YAML` and `%TAG` directives are supported, and they need to follow the specified strucutre. |
| `BAD_DQ_ESCAPE` | Double-quotes strings may include `\` escaped content, but that needs to be valid. |
| `BAD_INDENT` | Indentation is important in YAML, and collection items need to all start at the same level. Block scalars are also picky about their leading content. |
| `BAD_PROP_ORDER` | Anchors and tags must be placed after the `?`, `:` and `-` indicators. |
| `BAD_SCALAR_START` | Plain scalars cannot start with a block scalar indicator, or one of the two reserved characters: `@` and <code>`</code>. To fix, use a block or quoted scalar for the value. |
| `BLOCK_AS_IMPLICIT_KEY` | There's probably something wrong with the indentation, or you're trying to parse something like `a: b: c`, where it's not clear what's the key and what's the value. |
| `BLOCK_IN_FLOW` | YAML scalars and collections both have block and flow styles. Flow is allowed within block, but not the other way around. |
Expand All @@ -40,7 +41,6 @@ To identify errors for special handling, you should primarily use `code` to diff
| `MULTIPLE_ANCHORS` | A node is only allowed to have one anchor. |
| `MULTIPLE_DOCS` | A YAML stream may include multiple documents. If yours does, you'll need to use `parseAllDocuments()` to work with it. |
| `MULTIPLE_TAGS` | A node is only allowed to have one tag. |
| `PROP_BEFORE_SEP` | For an explicit key, anchors and tags must be after the `?` indicator |
| `TAB_AS_INDENT` | Only spaces are allowed as indentation. |
| `TAG_RESOLVE_FAILED` | Something went wrong when resolving a node's tag with the current schema. |
| `UNEXPECTED_TOKEN` | A token was encountered in a place where it wasn't expected. |
Expand Down
5 changes: 3 additions & 2 deletions src/compose/composer.ts
@@ -1,7 +1,7 @@
import { Directives } from '../doc/directives.js'
import { Document } from '../doc/Document.js'
import { ErrorCode, YAMLParseError, YAMLWarning } from '../errors.js'
import { isCollection } from '../nodes/Node.js'
import { isCollection, isPair } from '../nodes/Node.js'
import {
defaultOptions,
DocumentOptions,
Expand Down Expand Up @@ -99,7 +99,8 @@ export class Composer {
} else if (afterEmptyLine || doc.directives.marker || !dc) {
doc.commentBefore = comment
} else if (isCollection(dc) && !dc.flow && dc.items.length > 0) {
const it = dc.items[0]
let it = dc.items[0]
if (isPair(it)) it = it.key
const cb = it.commentBefore
it.commentBefore = cb ? `${comment}\n${cb}` : comment
} else {
Expand Down