Skip to content

Commit

Permalink
Merge 805cc71 into 6e49ce6
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Aug 1, 2020
2 parents 6e49ce6 + 805cc71 commit a20069a
Show file tree
Hide file tree
Showing 73 changed files with 329 additions and 234 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "yaml-eslint-parser",
"version": "0.0.5",
"version": "0.0.6",
"description": "A YAML parser that produces output compatible with ESLint",
"main": "lib/index.js",
"files": [
Expand Down
22 changes: 11 additions & 11 deletions src/ast.ts
Expand Up @@ -47,7 +47,7 @@ export type YAMLNode =
| YAMLDirective
| YAMLContent
| YAMLPair
| YAMLWithMark
| YAMLWithMeta
| YAMLAnchor
| YAMLTag
export interface YAMLProgram extends BaseYAMLNode {
Expand All @@ -62,7 +62,7 @@ export interface YAMLProgram extends BaseYAMLNode {
export interface YAMLDocument extends BaseYAMLNode {
type: "YAMLDocument"
directives: YAMLDirective[]
content: YAMLContent | YAMLWithMark | null
content: YAMLContent | YAMLWithMeta | null
parent: YAMLProgram
anchors: { [key: string]: YAMLAnchor }
}
Expand All @@ -73,8 +73,8 @@ export interface YAMLDirective extends BaseYAMLNode {
parent: YAMLDocument
}

export interface YAMLWithMark extends BaseYAMLNode {
type: "YAMLWithMark"
export interface YAMLWithMeta extends BaseYAMLNode {
type: "YAMLWithMeta"
anchor: YAMLAnchor | null
tag: YAMLTag | null
value: YAMLContent | null
Expand All @@ -84,17 +84,17 @@ export interface YAMLWithMark extends BaseYAMLNode {
export interface YAMLAnchor extends BaseYAMLNode {
type: "YAMLAnchor"
name: string
parent: YAMLWithMark
parent: YAMLWithMeta
}

export interface YAMLTag extends BaseYAMLNode {
type: "YAMLTag"
tag: string
parent: YAMLWithMark
parent: YAMLWithMeta
}

interface BaseYAMLContentNode extends BaseYAMLNode {
parent: YAMLDocument | YAMLPair | YAMLSequence | YAMLWithMark
parent: YAMLDocument | YAMLPair | YAMLSequence | YAMLWithMeta
}

export type YAMLContent = YAMLMapping | YAMLSequence | YAMLScalar | YAMLAlias
Expand All @@ -113,21 +113,21 @@ export interface YAMLFlowMapping extends BaseYAMLContentNode {

export interface YAMLPair extends BaseYAMLNode {
type: "YAMLPair"
key: YAMLContent | YAMLWithMark | null
value: YAMLContent | YAMLWithMark | null
key: YAMLContent | YAMLWithMeta | null
value: YAMLContent | YAMLWithMeta | null
parent: YAMLMapping
}

export type YAMLSequence = YAMLBlockSequence | YAMLFlowSequence
export interface YAMLBlockSequence extends BaseYAMLContentNode {
type: "YAMLSequence"
style: "block"
entries: (YAMLContent | YAMLWithMark)[]
entries: (YAMLContent | YAMLWithMeta)[]
}
export interface YAMLFlowSequence extends BaseYAMLContentNode {
type: "YAMLSequence"
style: "flow"
entries: (YAMLContent | YAMLWithMark)[]
entries: (YAMLContent | YAMLWithMeta)[]
}
export type YAMLScalar =
| YAMLPlainScalar
Expand Down
59 changes: 38 additions & 21 deletions src/convert.ts
Expand Up @@ -48,7 +48,7 @@ import type {
YAMLAlias,
YAMLAnchor,
YAMLTag,
YAMLWithMark,
YAMLWithMeta,
YAMLSequence,
} from "./ast"
import { isFalse, isTrue, isNull } from "./utils"
Expand Down Expand Up @@ -269,7 +269,7 @@ function convertDocumentBody(
tokens: Token[],
code: string,
parent: YAMLDocument,
): YAMLContent | YAMLWithMark | null {
): YAMLContent | YAMLWithMeta | null {
const contentNode = node.children[0]
return contentNode
? convertContentNode(contentNode, tokens, code, parent, parent)
Expand All @@ -285,7 +285,7 @@ function convertContentNode(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLContent | YAMLWithMark {
): YAMLContent | YAMLWithMeta {
if (node.type === "mapping") {
return convertMapping(node, tokens, code, parent, doc)
}
Expand Down Expand Up @@ -328,7 +328,7 @@ function convertMapping(
code: string,
parent: YAMLDocument | YAMLPair | YAMLSequence,
doc: YAMLDocument,
): YAMLBlockMapping | YAMLWithMark {
): YAMLBlockMapping | YAMLWithMeta {
const loc = getConvertLocation(node)
const ast: YAMLBlockMapping = {
type: "YAMLMapping",
Expand All @@ -340,6 +340,12 @@ function convertMapping(
for (const n of node.children) {
ast.pairs.push(convertMappingItem(n, tokens, code, ast, doc))
}
const last = ast.pairs[ast.pairs.length - 1]
if (last && ast.range[1] !== last.range[1]) {
// adjust location
ast.range[1] = last.range[1]
ast.loc.end = clone(last.loc.end)
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

Expand All @@ -352,7 +358,7 @@ function convertFlowMapping(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLFlowMapping | YAMLWithMark {
): YAMLFlowMapping | YAMLWithMeta {
const loc = getConvertLocation(node)
const ast: YAMLFlowMapping = {
type: "YAMLMapping",
Expand Down Expand Up @@ -387,6 +393,11 @@ function convertMappingItem(
}
ast.key = convertMappingKey(node.children[0], tokens, code, ast, doc)
ast.value = convertMappingValue(node.children[1], tokens, code, ast, doc)
if (ast.value && ast.range[1] !== ast.value.range[1]) {
// adjust location
ast.range[1] = ast.value.range[1]
ast.loc.end = clone(ast.value.loc.end)
}
return ast
}

Expand All @@ -399,7 +410,7 @@ function convertMappingKey(
code: string,
parent: YAMLPair,
doc: YAMLDocument,
): YAMLContent | YAMLWithMark | null {
): YAMLContent | YAMLWithMeta | null {
if (node.children.length) {
return convertContentNode(node.children[0], tokens, code, parent, doc)
}
Expand All @@ -415,7 +426,7 @@ function convertMappingValue(
code: string,
parent: YAMLPair,
doc: YAMLDocument,
): YAMLContent | YAMLWithMark | null {
): YAMLContent | YAMLWithMeta | null {
if (node.children.length) {
return convertContentNode(node.children[0], tokens, code, parent, doc)
}
Expand All @@ -431,7 +442,7 @@ function convertSequence(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLBlockSequence | YAMLWithMark {
): YAMLBlockSequence | YAMLWithMeta {
const loc = getConvertLocation(node)
const ast: YAMLBlockSequence = {
type: "YAMLSequence",
Expand All @@ -443,6 +454,12 @@ function convertSequence(
for (const n of node.children) {
ast.entries.push(...convertSequenceItem(n, tokens, code, ast, doc))
}
const last = ast.entries[ast.entries.length - 1]
if (last && ast.range[1] !== last.range[1]) {
// adjust location
ast.range[1] = last.range[1]
ast.loc.end = clone(last.loc.end)
}
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast)
}

Expand All @@ -455,7 +472,7 @@ function convertFlowSequence(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLFlowSequence | YAMLWithMark {
): YAMLFlowSequence | YAMLWithMeta {
const loc = getConvertLocation(node)
const ast: YAMLFlowSequence = {
type: "YAMLSequence",
Expand Down Expand Up @@ -493,7 +510,7 @@ function* convertSequenceItem(
code: string,
parent: YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): IterableIterator<YAMLContent | YAMLWithMark> {
): IterableIterator<YAMLContent | YAMLWithMeta> {
if (node.children.length) {
yield convertContentNode(node.children[0], tokens, code, parent, doc)
}
Expand All @@ -508,7 +525,7 @@ function convertPlain(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLPlainScalar | YAMLWithMark {
): YAMLPlainScalar | YAMLWithMeta {
const loc = getConvertLocation(node)
if (loc.range[0] < loc.range[1]) {
const strValue = node.value
Expand Down Expand Up @@ -591,7 +608,7 @@ function convertQuoteDouble(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLDoubleQuotedScalar | YAMLWithMark {
): YAMLDoubleQuotedScalar | YAMLWithMeta {
const loc = getConvertLocation(node)
const strValue = node.value
const ast: YAMLDoubleQuotedScalar = {
Expand All @@ -616,7 +633,7 @@ function convertQuoteSingle(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLSingleQuotedScalar | YAMLWithMark {
): YAMLSingleQuotedScalar | YAMLWithMeta {
const loc = getConvertLocation(node)
const strValue = node.value
const ast: YAMLSingleQuotedScalar = {
Expand All @@ -641,7 +658,7 @@ function convertBlockLiteral(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLBlockLiteralScalar | YAMLWithMark {
): YAMLBlockLiteralScalar | YAMLWithMeta {
const loc = getConvertLocation(node)
const value = node.value
const ast: YAMLBlockLiteralScalar = {
Expand Down Expand Up @@ -722,7 +739,7 @@ function convertBlockFolded(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLBlockFoldedScalar | YAMLWithMark {
): YAMLBlockFoldedScalar | YAMLWithMeta {
const loc = getConvertLocation(node)
const value = node.value
const ast: YAMLBlockFoldedScalar = {
Expand Down Expand Up @@ -803,7 +820,7 @@ function convertAlias(
code: string,
parent: YAMLDocument | YAMLPair | YAMLBlockSequence | YAMLFlowSequence,
doc: YAMLDocument,
): YAMLAlias | YAMLWithMark {
): YAMLAlias | YAMLWithMeta {
const loc = getConvertLocation(node)
const value = node.value
const ast: YAMLAlias = {
Expand Down Expand Up @@ -851,10 +868,10 @@ function convertAnchorAndTag<V extends YAMLContent>(
value: V | null,
doc: YAMLDocument,
valueLoc: Locations,
): YAMLWithMark | V {
): YAMLWithMeta | V {
if (node.anchor || node.tag) {
const ast: YAMLWithMark = {
type: "YAMLWithMark",
const ast: YAMLWithMeta = {
type: "YAMLWithMeta",
anchor: null,
tag: null,
value,
Expand Down Expand Up @@ -893,7 +910,7 @@ function convertAnchor(
node: Anchor,
tokens: Token[],
code: string,
parent: YAMLWithMark,
parent: YAMLWithMeta,
doc: YAMLDocument,
): YAMLAnchor {
const loc = getConvertLocation(node)
Expand Down Expand Up @@ -942,7 +959,7 @@ function convertTag(
node: Tag,
tokens: Token[],
code: string,
parent: YAMLWithMark,
parent: YAMLWithMeta,
): YAMLTag {
const loc = getConvertLocation(node)
const value = node.value
Expand Down
10 changes: 5 additions & 5 deletions src/utils.ts
Expand Up @@ -9,7 +9,7 @@ import {
YAMLAlias,
YAMLAnchor,
YAMLPair,
YAMLWithMark,
YAMLWithMeta,
YAMLTag,
} from "./ast"

Expand All @@ -35,14 +35,14 @@ export function getStaticYAMLValue(
): string | number | boolean | null
export function getStaticYAMLValue(node: YAMLAlias): YAMLContentValue
export function getStaticYAMLValue(
node: YAMLProgram | YAMLDocument | YAMLContent | YAMLPair | YAMLWithMark,
node: YAMLProgram | YAMLDocument | YAMLContent | YAMLPair | YAMLWithMeta,
): YAMLContentValue

/**
* Gets the static value for the given node.
*/
export function getStaticYAMLValue(
node: YAMLProgram | YAMLDocument | YAMLContent | YAMLPair | YAMLWithMark,
node: YAMLProgram | YAMLDocument | YAMLContent | YAMLPair | YAMLWithMeta,
): YAMLContentValue {
return resolver[node.type](node as any)
}
Expand Down Expand Up @@ -89,7 +89,7 @@ const resolver = {
const anchor = findAnchor(node)
return anchor ? getStaticYAMLValue(anchor.parent) : null
},
YAMLWithMark(node: YAMLWithMark) {
YAMLWithMeta(node: YAMLWithMeta) {
if (node.tag) {
if (node.value == null) {
return getTaggedValue(node.tag, "", "")
Expand Down Expand Up @@ -130,7 +130,7 @@ function findAnchor(node: YAMLAlias): YAMLAnchor | null {
| YAMLSequence
| YAMLMapping
| YAMLPair
| YAMLWithMark
| YAMLWithMeta
| undefined = node.parent
let doc: YAMLDocument | null = null
while (p) {
Expand Down
2 changes: 1 addition & 1 deletion src/visitor-keys.ts
Expand Up @@ -10,7 +10,7 @@ const yamlKeys: { [key in YAMLNode["type"]]: string[] } = {
YAMLPair: ["key", "value"],
YAMLSequence: ["entries"],

YAMLWithMark: ["anchor", "tag", "value"],
YAMLWithMeta: ["anchor", "tag", "value"],

YAMLScalar: [],
YAMLAlias: [],
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/parser/ast/alias01-output.json
Expand Up @@ -9,7 +9,7 @@
"style": "block",
"entries": [
{
"type": "YAMLWithMark",
"type": "YAMLWithMeta",
"anchor": {
"type": "YAMLAnchor",
"name": "mark",
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/parser/ast/comments01-output.json
Expand Up @@ -52,7 +52,7 @@
},
"range": [
18,
30
26
],
"loc": {
"start": {
Expand All @@ -61,14 +61,14 @@
},
"end": {
"line": 5,
"column": 12
"column": 8
}
}
}
],
"range": [
18,
30
26
],
"loc": {
"start": {
Expand All @@ -77,7 +77,7 @@
},
"end": {
"line": 5,
"column": 12
"column": 8
}
}
},
Expand Down

0 comments on commit a20069a

Please sign in to comment.