Skip to content
This repository has been archived by the owner on Jul 2, 2019. It is now read-only.

Commit

Permalink
feat: Expose line and column in annotation source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Sep 7, 2018
1 parent eaf094b commit 58c2932
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Master

### Enhancements

- Line and column numbers are now exposed in the parse result in source maps
for annotation elements.

### Bug Fixes

- Default and example values for headers are now validated to match the header
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"minim": "^0.20.5",
"minim-parse-result": "^0.10.1",
"peasant": "1.1.0",
"swagger-zoo": "2.17.2"
"swagger-zoo": "2.18.0"
},
"engines": {
"node": ">=6"
Expand Down
12 changes: 6 additions & 6 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export default class Ast {

if (!pieces.length) {
// This is the last item!
start = subNode[0].start_mark.pointer;
end = subNode[1].end_mark.pointer;
start = subNode[0].start_mark;
end = subNode[1].end_mark;
}
break;
}
Expand All @@ -52,11 +52,11 @@ export default class Ast {
if (!newNode && piece > 0 && node.value[piece - 1]) {
// Element in sequence does not exist. It could have been empty
// Let's provide the end of previous element
start = node.value[piece - 1].end_mark.pointer;
end = start + 1;
start = node.value[piece - 1].end_mark;
end = start;
} else {
start = newNode.start_mark.pointer;
end = newNode.end_mark.pointer;
start = newNode.start_mark;
end = newNode.end_mark;
}
}
} else {
Expand Down
22 changes: 16 additions & 6 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1418,16 +1418,26 @@ export default class Parser {
}

// Make a new source map for the given element
createSourceMap(element, path) {
createSourceMap(element, path, produceLineColumnAttributes) {
if (this.ast) {
const NumberElement = this.minim.elements.Number;
const SourceMap = this.minim.getElementClass('sourceMap');
const position = this.ast.getPosition(path);

// eslint-disable-next-line no-restricted-globals
if (position && !isNaN(position.start) && !isNaN(position.end)) {
element.attributes.set('sourceMap', [
new SourceMap([[position.start, position.end - position.start]]),
]);
if (position && position.start && position.end &&
!isNaN(position.start.pointer) && !isNaN(position.end.pointer)) {
const start = new NumberElement(position.start.pointer);
const end = new NumberElement(position.end.pointer - position.start.pointer);

if (produceLineColumnAttributes) {
start.attributes.set('line', position.start.line);
start.attributes.set('column', position.start.column);
end.attributes.set('line', position.end.line);
end.attributes.set('column', position.end.column);
}

element.attributes.set('sourceMap', [new SourceMap([[start, end]])]);
}
}
}
Expand All @@ -1447,7 +1457,7 @@ export default class Parser {
}

if (path && this.ast) {
this.createSourceMap(annotation, path);
this.createSourceMap(annotation, path, true);
}

return annotation;
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/ast-path-with-reference-sibling.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,30 @@
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 14
},
"column": {
"element": "number",
"content": 9
}
},
"content": 247
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 14
},
"column": {
"element": "number",
"content": 9
}
},
"content": 0
}
]
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/consumes-invalid-type.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,30 @@
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 5
},
"column": {
"element": "number",
"content": 4
}
},
"content": 101
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 5
},
"column": {
"element": "number",
"content": 33
}
},
"content": 29
}
]
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/headers-type-warning.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,30 @@
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 13
},
"column": {
"element": "number",
"content": 14
}
},
"content": 255
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 13
},
"column": {
"element": "number",
"content": 27
}
},
"content": 13
}
]
Expand Down Expand Up @@ -204,10 +224,30 @@
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 16
},
"column": {
"element": "number",
"content": 14
}
},
"content": 346
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 16
},
"column": {
"element": "number",
"content": 28
}
},
"content": 14
}
]
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/invalid-media-type.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,30 @@
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 6
},
"column": {
"element": "number",
"content": 4
}
},
"content": 107
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 6
},
"column": {
"element": "number",
"content": 13
}
},
"content": 9
}
]
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/invalid-reference.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,30 @@
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 12
},
"column": {
"element": "number",
"content": 10
}
},
"content": 205
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 14
},
"column": {
"element": "number",
"content": 0
}
},
"content": 48
}
]
Expand Down
42 changes: 41 additions & 1 deletion test/fixtures/non-existing-elm-in-sequence.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,31 @@
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 11
},
"column": {
"element": "number",
"content": 9
}
},
"content": 226
},
{
"element": "number",
"content": 1
"attributes": {
"line": {
"element": "number",
"content": 11
},
"column": {
"element": "number",
"content": 9
}
},
"content": 0
}
]
}
Expand Down Expand Up @@ -110,10 +130,30 @@
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 11
},
"column": {
"element": "number",
"content": 9
}
},
"content": 226
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 11
},
"column": {
"element": "number",
"content": 9
}
},
"content": 0
}
]
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/operation-consumes-invalid-type.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,30 @@
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 8
},
"column": {
"element": "number",
"content": 10
}
},
"content": 140
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 8
},
"column": {
"element": "number",
"content": 39
}
},
"content": 29
}
]
Expand Down
Loading

0 comments on commit 58c2932

Please sign in to comment.