Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Aug 1, 2020
1 parent 294f3e3 commit da4a1fc
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 351 deletions.
2 changes: 1 addition & 1 deletion src/ast.ts
Expand Up @@ -77,7 +77,7 @@ export interface YAMLWithMark extends BaseYAMLNode {
type: "YAMLWithMark"
anchor: YAMLAnchor | null
tag: YAMLTag | null
value: YAMLContent
value: YAMLContent | null
parent: YAMLDocument | YAMLPair | YAMLSequence
}

Expand Down
107 changes: 61 additions & 46 deletions src/convert.ts
Expand Up @@ -340,7 +340,7 @@ function convertMapping(
for (const n of node.children) {
ast.pairs.push(convertMappingItem(n, tokens, code, ast, doc))
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

/**
Expand All @@ -364,7 +364,7 @@ function convertFlowMapping(
for (const n of node.children) {
ast.pairs.push(convertMappingItem(n, tokens, code, ast, doc))
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

/**
Expand Down Expand Up @@ -443,7 +443,7 @@ function convertSequence(
for (const n of node.children) {
ast.entries.push(...convertSequenceItem(n, tokens, code, ast, doc))
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

/**
Expand Down Expand Up @@ -481,7 +481,7 @@ function convertFlowSequence(
ast.entries.push(map)
}
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

/**
Expand Down Expand Up @@ -510,40 +510,52 @@ function convertPlain(
doc: YAMLDocument,
): YAMLPlainScalar | YAMLWithMark {
const loc = getConvertLocation(node)
const strValue = node.value
let value: string | number | boolean | null

if (isTrue(strValue)) {
value = true
} else if (isFalse(strValue)) {
value = false
} else if (isNull(strValue)) {
value = null
} else if (needParse(strValue)) {
value = yaml.parse(strValue) || strValue
} else {
value = strValue
}
const ast: YAMLPlainScalar = {
type: "YAMLScalar",
style: "plain",
strValue,
value,
parent,
...loc,
}
if (loc.range[0] < loc.range[1]) {
const strValue = node.value
let value: string | number | boolean | null

const type = typeof value
if (type === "boolean") {
addToken(tokens, "Boolean", clone(loc), code)
} else if (type === "number" && isFinite(Number(value))) {
addToken(tokens, "Numeric", clone(loc), code)
} else if (value === null) {
addToken(tokens, "Null", clone(loc), code)
} else {
addToken(tokens, "Identifier", clone(loc), code)
if (isTrue(strValue)) {
value = true
} else if (isFalse(strValue)) {
value = false
} else if (isNull(strValue)) {
value = null
} else if (needParse(strValue)) {
value = yaml.parse(strValue) || strValue
} else {
value = strValue
}
const ast: YAMLPlainScalar = {
type: "YAMLScalar",
style: "plain",
strValue,
value,
parent,
...loc,
}

const type = typeof value
if (type === "boolean") {
addToken(tokens, "Boolean", clone(loc), code)
} else if (type === "number" && isFinite(Number(value))) {
addToken(tokens, "Numeric", clone(loc), code)
} else if (value === null) {
addToken(tokens, "Null", clone(loc), code)
} else {
addToken(tokens, "Identifier", clone(loc), code)
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, loc)
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)

return convertAnchorAndTag<YAMLPlainScalar>(
node,
tokens,
code,
parent,
null,
doc,
loc,
)

/**
* Checks if the given string needs to be parsed
Expand Down Expand Up @@ -592,7 +604,7 @@ function convertQuoteDouble(
...loc,
}
addToken(tokens, "String", clone(loc), code)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

/**
Expand All @@ -617,7 +629,7 @@ function convertQuoteSingle(
...loc,
}
addToken(tokens, "String", clone(loc), code)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

/**
Expand Down Expand Up @@ -698,7 +710,7 @@ function convertBlockLiteral(
// ??
addToken(tokens, "BlockLiteral", clone(loc), code)
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

/**
Expand Down Expand Up @@ -779,7 +791,7 @@ function convertBlockFolded(
// ??
addToken(tokens, "BlockFolded", clone(loc), code)
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

/**
Expand Down Expand Up @@ -825,7 +837,7 @@ function convertAlias(
// ??
addToken(tokens, "Identifier", clone(loc), code)
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc)
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

/**
Expand All @@ -836,8 +848,9 @@ function convertAnchorAndTag<V extends YAMLContent>(
tokens: Token[],
code: string,
parent: YAMLDocument | YAMLPair | YAMLSequence,
value: V,
value: V | null,
doc: YAMLDocument,
valueLoc: Locations,
): YAMLWithMark | V {
if (node.anchor || node.tag) {
const ast: YAMLWithMark = {
Expand All @@ -846,10 +859,12 @@ function convertAnchorAndTag<V extends YAMLContent>(
tag: null,
value,
parent,
range: clone(value.range),
loc: clone(value.loc),
range: clone(valueLoc.range),
loc: clone(valueLoc.loc),
}
if (value) {
value.parent = ast
}
value.parent = ast

if (node.anchor) {
const anchor = convertAnchor(node.anchor, tokens, code, ast, doc)
Expand All @@ -868,7 +883,7 @@ function convertAnchorAndTag<V extends YAMLContent>(
return ast
}

return value
return value as any
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Expand Up @@ -91,6 +91,9 @@ const resolver = {
},
YAMLWithMark(node: YAMLWithMark) {
if (node.tag) {
if (node.value == null) {
return getTaggedValue(node.tag, "", "")
}
if (node.value.type === "YAMLScalar") {
if (node.value.style === "plain") {
return getTaggedValue(
Expand All @@ -111,6 +114,9 @@ const resolver = {
}
}
}
if (node.value == null) {
return null
}
return getStaticYAMLValue(node.value)
},
}
Expand Down

0 comments on commit da4a1fc

Please sign in to comment.