Skip to content

Commit

Permalink
Merge 2e1634f into 07cd973
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Dec 18, 2020
2 parents 07cd973 + 2e1634f commit aa62ef0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/AST.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ interface YAMLDocument extends Node {
directives: YAMLDirective[]
content: YAMLMapping | YAMLSequence | YAMLScalar | YAMLAlias | YAMLWithMeta | null
parent: YAMLProgram
anchors: { [key: string]: YAMLAnchor }
anchors: { [key: string]: YAMLAnchor[] }
}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface YAMLDocument extends BaseYAMLNode {
directives: YAMLDirective[]
content: YAMLContent | YAMLWithMeta | null
parent: YAMLProgram
anchors: { [key: string]: YAMLAnchor }
anchors: { [key: string]: YAMLAnchor[] }
}

export interface YAMLDirective extends BaseYAMLNode {
Expand Down
3 changes: 2 additions & 1 deletion src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,8 @@ function convertAnchor(
...loc,
}

doc.anchors[value] = ast
const anchors = doc.anchors[value] || (doc.anchors[value] = [])
anchors.push(ast)

const text = code.slice(...loc.range)
if (text.startsWith("&")) {
Expand Down
21 changes: 20 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,26 @@ function findAnchor(node: YAMLAlias): YAMLAnchor | null {
}
p = p.parent
}
return doc!.anchors[node.name] || null
const anchors = doc!.anchors[node.name]
if (!anchors) {
return null
}
let target: { anchor: null | YAMLAnchor; distance: number } = {
anchor: null,
distance: Infinity,
}
for (const anchor of anchors) {
if (anchor.range[0] < node.range[0]) {
const distance = node.range[0] - anchor.range[0]
if (target.distance >= distance) {
target = {
anchor,
distance,
}
}
}
}
return target.anchor
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/parser/yaml-test-suite/3GZX-value.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"First occurrence": "Foo",
"Second occurrence": "Bar",
"Second occurrence": "Foo",
"Override anchor": "Bar",
"Reuse anchor": "Bar"
}
44 changes: 44 additions & 0 deletions tests/src/parser/eslint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Linter } from "eslint"
import assert from "assert"
import * as parser from "../../../src/index"

function createLinter() {
const linter = new Linter()

linter.defineParser("yaml-eslint-parser", parser as any)
linter.defineRule("test", {
create(context) {
return {
YAMLDocument(node: any) {
context.report({
node,
message: "test",
})
},
}
},
})

return linter
}

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

describe("eslint custom parser", () => {
it("should work with eslint.", () => {
const code = `Hello!`

const linter = createLinter()
const messages = linter.verify(code, {
parser: "yaml-eslint-parser",
rules: {
test: "error",
},
})

assert.strictEqual(messages.length, 1)
assert.strictEqual(messages[0].message, "test")
})
})
8 changes: 4 additions & 4 deletions tests/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import assert from "assert"
import path from "path"
import fs from "fs"

import { parseForESLint } from "../../../src/parser"
import { KEYS } from "../../../src/visitor-keys"
import { traverseNodes, getKeys } from "../../../src/traverse"
import { getStaticYAMLValue } from "../../../src/utils"
import type { YAMLProgram } from "../../../src/ast"
import { parseYAML } from "../../../src"

const AST_FIXTURE_ROOT = path.resolve(__dirname, "../../fixtures/parser/ast")
const SUITE_FIXTURE_ROOT = path.resolve(
Expand All @@ -32,7 +32,7 @@ function replacer(key: string, value: any) {
}

function parse(code: string) {
return parseForESLint(code, {})
return parseYAML(code)
}

describe("Check for AST.", () => {
Expand All @@ -51,7 +51,7 @@ describe("Check for AST.", () => {
)

const input = fs.readFileSync(inputFileName, "utf8")
const ast = parse(input).ast
const ast = parse(input)
const astJson = JSON.stringify(ast, replacer, 2)
const output = fs.readFileSync(outputFileName, "utf8")
assert.strictEqual(astJson, output)
Expand Down Expand Up @@ -91,7 +91,7 @@ describe("yaml-test-suite.", () => {

let ast
try {
ast = parse(input).ast
ast = parse(input)
} catch (e) {
if (
typeof e.lineNumber === "number" &&
Expand Down

0 comments on commit aa62ef0

Please sign in to comment.