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

compat(plugin:ast): adopt final Swagger UI implementation #1857

Merged
merged 4 commits into from
Aug 2, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions src/plugins/ast/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export function getLineNumberForPath(yaml, path) {
* YAML or JSON string
* @param {array} path
* an array of stings that constructs a
* JSON Path similiar to JSON Pointers(RFC 6901). The difference is, each
* component of path is an item of the array intead of beinf seperated with
* JSON Path similar to JSON Pointers(RFC 6901). The difference is, each
* component of path is an item of the array instead of being separated with
* slash(/) in a string
*/
export function positionRangeForPath(yaml, path) {
Expand All @@ -103,30 +103,30 @@ export function positionRangeForPath(yaml, path) {

let ast = cachedCompose(yaml)

// simply walks the tree using current path recursively to the point that
// simply walks the tree using astValue path recursively to the point that
// path is empty.
return find(ast)

function find(current) {
if (current.tag === MAP_TAG) {
for (i = 0; i < current.value.length; i++) {
var pair = current.value[i]
function find(astValue, astKeyValue) {
if (astValue.tag === MAP_TAG) {
for (i = 0; i < astValue.value.length; i++) {
var pair = astValue.value[i]
var key = pair[0]
var value = pair[1]

if (key.value === path[0]) {
path.shift()
return find(value)
return find(value, key)
}
}
}

if (current.tag === SEQ_TAG) {
var item = current.value[path[0]]
if (astValue.tag === SEQ_TAG) {
var item = astValue.value[path[0]]

if (item && item.tag) {
path.shift()
return find(item)
return find(item, astKeyValue)
}
}

Expand All @@ -135,17 +135,35 @@ export function positionRangeForPath(yaml, path) {
return invalidRange
}

return {
/* jshint camelcase: false */
const range = {
start: {
line: current.start_mark.line,
column: current.start_mark.column
line: astValue.start_mark.line,
column: astValue.start_mark.column,
pointer: astValue.start_mark.pointer,
},
end: {
line: current.end_mark.line,
column: current.end_mark.column
line: astValue.end_mark.line,
column: astValue.end_mark.column,
pointer: astValue.end_mark.pointer,
}
}

if(astKeyValue) {
// eslint-disable-next-line camelcase
range.key_start = {
line: astKeyValue.start_mark.line,
column: astKeyValue.start_mark.column,
pointer: astKeyValue.start_mark.pointer,
}
// eslint-disable-next-line camelcase
range.key_end = {
line: astKeyValue.end_mark.line,
column: astKeyValue.end_mark.column,
pointer: astKeyValue.end_mark.pointer,
}
}

return range
}
}

Expand Down Expand Up @@ -277,6 +295,6 @@ export let getLineNumberForPathAsync = promisifySyncFn(getLineNumberForPath)

function promisifySyncFn(fn) {
return function(...args) {
return new Promise(resolve => resolve(fn(...args)))
return new Promise((resolve) => resolve(fn(...args)))
}
}
124 changes: 81 additions & 43 deletions test/plugins/ast/ast-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@ import expect from "expect"
import { pathForPosition, positionRangeForPath } from "plugins/ast/ast"

describe("ASTManager", function() {
describe("#pathForPosition", function() {
describe("out of range", function() {
it("returns empty array for out of range row", function() {
let position = {line: 3, column: 0}
describe("#pathForPosition", function () {
describe("out of range", function () {
it("returns empty array for out of range row", function () {
let position = { line: 3, column: 0 }
let path = pathForPosition("swagger: 2.0", position)

expect(path).toEqual([])
})

it("returns empty array for out of range column", function() {
let position = {line: 0, column: 100}
it("returns empty array for out of range column", function () {
let position = { line: 0, column: 100 }
let path = pathForPosition("swagger: 2.0", position)

expect(path).toEqual([])
})
})

describe("when document is a simple hash `swagger: 2.0`", function() {
it("should return empty array when pointer is at middle of the hash key", function() {
let position = {line: 0, column: 3}
describe("when document is a simple hash `swagger: 2.0`", function () {
it("should return empty array when pointer is at middle of the hash key", function () {
let position = { line: 0, column: 3 }
let path = pathForPosition("swagger: 2.0", position)

expect(path).toEqual([])
})

it("should return ['swagger'] when pointer is at the value", function() {
let position = {line: 0, column: 10}
it("should return ['swagger'] when pointer is at the value", function () {
let position = { line: 0, column: 10 }
let path = pathForPosition("swagger: 2.0", position)

expect(path).toEqual(["swagger"])
})
})

describe("when document is an array: ['abc', 'cde']", function() {
describe("when document is an array: ['abc', 'cde']", function () {
let yaml = [
/*
0
Expand All @@ -45,29 +45,29 @@ describe("ASTManager", function() {
/* 1 */ "- def"
].join("\n")

it("should return empty array when pointer is at array dash", function() {
let position = {line: 0, column: 0}
it("should return empty array when pointer is at array dash", function () {
let position = { line: 0, column: 0 }
let path = pathForPosition(yaml, position)

expect(path).toEqual([])
})

it("should return ['0'] when pointer is at abc", function() {
let position = {line: 0, column: 3}
it("should return ['0'] when pointer is at abc", function () {
let position = { line: 0, column: 3 }
let path = pathForPosition(yaml, position)

expect(path).toEqual(["0"])
})

it("should return ['1'] when pointer is at abc", function() {
let position = {line: 1, column: 3}
it("should return ['1'] when pointer is at abc", function () {
let position = { line: 1, column: 3 }
let path = pathForPosition(yaml, position)

expect(path).toEqual(["1"])
})
})

describe("when document is an array of arrays", function() {
describe("when document is an array of arrays", function () {
let yaml = [
/*
0 10
Expand All @@ -80,15 +80,15 @@ describe("ASTManager", function() {
/* 5 */ " - DEF"
].join("\n")

it("should return ['0', '0'] when pointer is at 'abc'", function() {
let position = {line: 1, column: 5}
it("should return ['0', '0'] when pointer is at 'abc'", function () {
let position = { line: 1, column: 5 }
let path = pathForPosition(yaml, position)

expect(path).toEqual(["0", "0"])
})
})

describe("when document is an array of hashs", function() {
describe("when document is an array of hashs", function () {
let yaml = [
/*
0 10
Expand All @@ -99,29 +99,29 @@ describe("ASTManager", function() {
/* 3 */ " year: 2016"
].join("\n")

it("should return ['0'] when pointer is at 'key'", function() {
let position = {line: 0, column: 3}
it("should return ['0'] when pointer is at 'key'", function () {
let position = { line: 0, column: 3 }
let path = pathForPosition(yaml, position)

expect(path).toEqual(["0"])
})

it("should return ['0', 'key'] when pointer is at 'value'", function() {
let position = {line: 0, column: 9}
it("should return ['0', 'key'] when pointer is at 'value'", function () {
let position = { line: 0, column: 9 }
let path = pathForPosition(yaml, position)

expect(path).toEqual(["0", "key"])
})

it("should return ['1', 'year'] when pointer is at '2016'", function() {
let position = {line: 3, column: 10}
it("should return ['1', 'year'] when pointer is at '2016'", function () {
let position = { line: 3, column: 10 }
let path = pathForPosition(yaml, position)

expect(path).toEqual(["1", "year"])
})
})

describe("full document", function() {
describe("full document", function () {
let yaml = [
/*
0 10 20 30
Expand All @@ -137,8 +137,8 @@ describe("ASTManager", function() {
/* 8 */ " "
].join("\n")

it("should return ['info', 'contact', 'email'] when pointer is at me@", function() {
let position = {line: 7, column: 13}
it("should return ['info', 'contact', 'email'] when pointer is at me@", function () {
let position = { line: 7, column: 13 }
let path = pathForPosition(yaml, position)

expect(path).toEqual(["info", "contact", "email"])
Expand All @@ -156,8 +156,14 @@ describe("ASTManager", function() {

let position = positionRangeForPath(yaml, ["invalid"])

expect(position.start).toEqual({line: -1, column: -1})
expect(position.end).toEqual({line: -1, column: -1})
expect(position.start).toEqual({
line: -1,
column: -1
})
expect(position.end).toEqual({
line: -1,
column: -1
})
})

describe("when document is a simple hash `swagger: 2.0`", function() {
Expand All @@ -166,25 +172,41 @@ describe("ASTManager", function() {
it("return {0, 0} for start of empty array path (root)", function() {
let position = positionRangeForPath(yaml, [])

expect(position.start).toEqual({line: 0, column: 0})
expect(position.start).toEqual({
line: 0,
column: 0,
pointer: 0
})
})

it("return {0, 12} for end of empty array path (root)", function() {
let position = positionRangeForPath(yaml, [])

expect(position.end).toEqual({line: 0, column: 12})
expect(position.end).toEqual({
line: 0,
column: 12,
pointer: 12
})
})

it("return {0, 9} for start of ['swagger']", function() {
let position = positionRangeForPath(yaml, ["swagger"])

expect(position.start).toEqual({line: 0, column: 9})
expect(position.start).toEqual({
line: 0,
column: 9,
pointer: 9
})
})

it("return {0, 12} for end of ['swagger']", function() {
let position = positionRangeForPath(yaml, ["swagger"])

expect(position.end).toEqual({line: 0, column: 12})
expect(position.end).toEqual({
line: 0,
column: 12,
pointer: 12
})
})
})

Expand All @@ -198,15 +220,19 @@ describe("ASTManager", function() {
it("returns {1, 4} for ['key', '0']", function() {
let position = positionRangeForPath(yaml, ["key", "0"])

expect(position.start).toEqual({line: 1, column: 4})
expect(position.start).toEqual({
line: 1,
column: 4,
pointer: 9
})
})
})

describe("full document", function() {
let yaml = [
/*
0 10 20 30
012345678901234567890123456789012345678 */
0 10 20 30
012345678901234567890123456789012345678 */
/* 0 */ "swagger: '2.0'",
/* 1 */ "info:",
/* 2 */ " title: Test document",
Expand All @@ -221,19 +247,31 @@ describe("ASTManager", function() {
it("returns {2, 2} for start of ['info']", function() {
let position = positionRangeForPath(yaml, ["info"])

expect(position.start).toEqual({line: 2, column: 2})
expect(position.start).toEqual({
line: 2,
column: 2,
pointer: 23
})
})

it("returns {5, 10} for start of ['info', 'contact', 'name']", function() {
let position = positionRangeForPath(yaml, ["info", "contact", "name"])

expect(position.start).toEqual({line: 5, column: 10})
expect(position.start).toEqual({
line: 5,
column: 10,
pointer: 82
})
})

it("returns {5, 15} for end of ['info', 'contact', 'name']", function() {
let position = positionRangeForPath(yaml, ["info", "contact", "name"])

expect(position.end).toEqual({line: 5, column: 15})
expect(position.end).toEqual({
line: 5,
column: 15,
pointer: 87
})
})
})
})
Expand Down