Skip to content

Commit

Permalink
Merge f52431b into 7926822
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jul 30, 2020
2 parents 7926822 + f52431b commit f3d9430
Show file tree
Hide file tree
Showing 29 changed files with 141 additions and 39 deletions.
26 changes: 13 additions & 13 deletions src/utils.ts
Expand Up @@ -30,7 +30,7 @@ type YAMLMappingValue = {
}

export function getStaticYAMLValue(
node: YAMLMapping | YAMLFlowMapping,
node: YAMLMapping | YAMLFlowMapping | YAMLPair,
): YAMLMappingValue
export function getStaticYAMLValue(
node: YAMLSequence | YAMLFlowSequence,
Expand All @@ -45,14 +45,14 @@ export function getStaticYAMLValue(
): string | number | boolean | null
export function getStaticYAMLValue(node: YAMLAlias): YAMLContentValue
export function getStaticYAMLValue(
node: YAMLProgram | YAMLDocument | YAMLContent,
node: YAMLProgram | YAMLDocument | YAMLContent | YAMLPair,
): YAMLContentValue

/**
* Gets the static value for the given node.
*/
export function getStaticYAMLValue(
node: YAMLProgram | YAMLDocument | YAMLContent,
node: YAMLProgram | YAMLDocument | YAMLContent | YAMLPair,
): YAMLContentValue {
return resolver[node.type](node as any)
}
Expand All @@ -72,26 +72,26 @@ const resolver = {
YAMLMapping(node: YAMLMapping | YAMLFlowMapping) {
const result: YAMLMappingValue = {}
for (const pair of node.pairs) {
const key = pair.key ? getStaticYAMLValue(pair.key) : null
if (typeof key !== "string" && typeof key !== "number") {
// illegal key
continue
}
result[key] = pair.value ? getStaticYAMLValue(pair.value) : null
Object.assign(result, getStaticYAMLValue(pair))
}
return result
},
YAMLPair(node: YAMLPair) {
const result: YAMLMappingValue = {}
let key = node.key ? getStaticYAMLValue(node.key) : null
if (typeof key !== "string" && typeof key !== "number") {
key = String(key)
}
result[key] = node.value ? getStaticYAMLValue(node.value) : null
return result
},
YAMLFlowMapping(node: YAMLFlowMapping) {
// eslint-disable-next-line new-cap
return resolver.YAMLMapping(node)
},
YAMLSequence(node: YAMLSequence | YAMLFlowSequence) {
const result: YAMLContentValue[] = []
for (const entry of node.entries) {
if (entry.type === "YAMLPair") {
// illegal entry
continue
}
result.push(getStaticYAMLValue(entry))
}
return result
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/empty-pair01-value.json
@@ -1,3 +1,4 @@
{
"null": null,
"a": "b"
}
4 changes: 3 additions & 1 deletion tests/fixtures/parser/ast/empty-pair03-value.json
@@ -1 +1,3 @@
{}
{
"null": "a"
}
5 changes: 4 additions & 1 deletion tests/fixtures/parser/ast/flow01-value.json
Expand Up @@ -7,6 +7,9 @@
[
1,
2,
3
3,
{
"a": "b"
}
]
]
3 changes: 2 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/4ABK-value.json
@@ -1,5 +1,6 @@
{
"unquoted": "separate",
"http://foo.com": null,
"omitted value": null
"omitted value": null,
"null": "omitted key"
}
4 changes: 3 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/6BFJ-value.json
@@ -1 +1,3 @@
{}
{
"a,b,c": "value"
}
3 changes: 2 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/6M2F-value.json
@@ -1,3 +1,4 @@
{
"a": "b"
"a": "b",
"null": "a"
}
6 changes: 5 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/87E4-value.json
@@ -1,3 +1,7 @@
{
"implicit block key": []
"implicit block key": [
{
"implicit flow key": "value"
}
]
}
5 changes: 4 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/8UDB-value.json
Expand Up @@ -4,5 +4,8 @@
"plain text",
[
"nested"
]
],
{
"single": "pair"
}
]
18 changes: 15 additions & 3 deletions tests/fixtures/parser/yaml-test-suite/9MMW-value.json
@@ -1,5 +1,17 @@
[
[],
[],
[]
[
{
"YAML": "separate"
}
],
[
{
"null": "empty key entry"
}
],
[
{
"JSON like": "adjacent"
}
]
]
6 changes: 6 additions & 0 deletions tests/fixtures/parser/yaml-test-suite/CN3R-value.json
@@ -1,4 +1,10 @@
[
{
"a": "b"
},
{
"c": "d"
},
{
"e": "f"
},
Expand Down
6 changes: 5 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/CT4Q-value.json
@@ -1 +1,5 @@
[]
[
{
"foo bar": "baz"
}
]
3 changes: 2 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/DFF7-value.json
@@ -1,4 +1,5 @@
{
"explicit": "entry",
"implicit": "entry"
"implicit": "entry",
"null": null
}
1 change: 1 addition & 0 deletions tests/fixtures/parser/yaml-test-suite/FH7J-value.json
@@ -1,6 +1,7 @@
[
"",
{
"null": "a",
"b": ""
},
{
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/FRK4-value.json
@@ -1,3 +1,4 @@
{
"foo": null
"foo": null,
"null": "bar"
}
18 changes: 15 additions & 3 deletions tests/fixtures/parser/yaml-test-suite/KZN9-value.json
@@ -1,5 +1,17 @@
[
[],
[],
[]
[
{
"YAML": "separate"
}
],
[
{
"null": "empty key entry"
}
],
[
{
"[object Object]": "adjacent"
}
]
]
6 changes: 5 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/L9U5-value.json
@@ -1,3 +1,7 @@
{
"implicit block key": []
"implicit block key": [
{
"implicit flow key": "value"
}
]
}
6 changes: 5 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/LQZ7-value.json
@@ -1,3 +1,7 @@
{
"implicit block key": []
"implicit block key": [
{
"implicit flow key": "value"
}
]
}
4 changes: 3 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/LX3P-value.json
@@ -1 +1,3 @@
{}
{
"flow": "block"
}
11 changes: 10 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/M5DY-value.json
@@ -1 +1,10 @@
{}
{
"Detroit Tigers,Chicago cubs": [
"2001-07-23"
],
"New York Yankees,Atlanta Braves": [
"2001-07-02",
"2001-08-12",
"2001-08-14"
]
}
4 changes: 3 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/NHX8-value.json
@@ -1 +1,3 @@
{}
{
"null": null
}
7 changes: 6 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/Q9WF-value.json
@@ -1 +1,6 @@
{}
{
"[object Object]": {
"hr": 65,
"avg": 0.278
}
}
6 changes: 5 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/QF4Y-value.json
@@ -1 +1,5 @@
[]
[
{
"foo": "bar"
}
]
3 changes: 3 additions & 0 deletions tests/fixtures/parser/yaml-test-suite/RZP5-value.json
Expand Up @@ -2,6 +2,9 @@
"a": "double quotes",
"b": "plain value",
"c": "d",
"seq1": [
"seq2"
],
"e": [
{
"x": "y"
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/yaml-test-suite/S3PD-value.json
@@ -1,5 +1,6 @@
{
"plain key": "in-line value",
"null": null,
"quoted key": [
"entry"
]
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/SBG9-value.json
Expand Up @@ -2,5 +2,6 @@
"a": [
"b",
"c"
]
],
"d,e": "f"
}
6 changes: 5 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/V9D5-value.json
Expand Up @@ -2,5 +2,9 @@
{
"sun": "yellow"
},
{}
{
"[object Object]": {
"moon": "white"
}
}
]
8 changes: 7 additions & 1 deletion tests/fixtures/parser/yaml-test-suite/X38W-value.json
@@ -1 +1,7 @@
{}
{
"a,b": [
"c",
"b",
"d"
]
}
3 changes: 3 additions & 0 deletions tests/fixtures/parser/yaml-test-suite/XW4D-value.json
Expand Up @@ -2,6 +2,9 @@
"a": "double quotes",
"b": "plain value",
"c": "d",
"seq1": [
"seq2"
],
"e": [
{
"x": "y"
Expand Down

0 comments on commit f3d9430

Please sign in to comment.