yaml-unist-parser
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0-beta.4
"use strict"; | ||
exports.__esModule = true; | ||
var lines_and_columns_1 = require("lines-and-columns"); | ||
var yaml_1 = require("yaml"); | ||
var parse_1 = require("yaml/dist/ast/parse"); | ||
@@ -20,7 +21,12 @@ var attach_1 = require("./attach"); | ||
transformRange: function (range) { return range_1.transformRange(range, context); }, | ||
transformOffset: function (offset) { return offset_1.transformOffset(offset, context); }, | ||
assertSyntaxError: function (value, message, position) { | ||
return utils_1.assertSyntaxError(value, message, position, context); | ||
transformOffset: function (offset) { return offset_1.transformOffset(offset, context); } | ||
}; | ||
rawDocuments.forEach(function (rawDocument) { | ||
var rawErrors = new yaml_1["default"].Document() | ||
.parse(rawDocument) | ||
.errors.filter(utils_1.isYAMLError); | ||
if (rawErrors.length !== 0) { | ||
throw utils_1.createError(rawErrors[0], context); | ||
} | ||
}; | ||
}); | ||
var rootPosition = context.transformRange({ start: 0, end: text.length }); | ||
@@ -27,0 +33,0 @@ var root = { |
@@ -11,3 +11,3 @@ /// <reference types="yaml" /> | ||
transformNode: <T extends YamlNode>(node: T) => YamlToUnist<T>; | ||
transformRange: (range: number | { | ||
transformRange: (range: { | ||
start: number; | ||
@@ -17,4 +17,3 @@ end: number; | ||
transformOffset: (offset: number) => Point; | ||
assertSyntaxError: (value: boolean, message: string | (() => string), position: Position | (() => Position)) => void; | ||
} | ||
export declare function transformNode<T extends YamlNode>(node: T, context: Context): YamlToUnist<T>; |
@@ -27,3 +27,2 @@ "use strict"; | ||
} | ||
context.assertSyntaxError(node.error === null, function () { return node.error.message; }, function () { return context.transformRange(utils_1.getRange(node)); }); | ||
var transformedNode = _transformNode(node, context); | ||
@@ -30,0 +29,0 @@ if (transformedNode.type === "comment") { |
@@ -26,3 +26,3 @@ "use strict"; | ||
}); | ||
context.assertSyntaxError(contentsWithoutComments.length <= 1, "Multiple content nodes are not allowed in a document.", function () { return contentsWithoutComments[1].position; }); | ||
assert(contentsWithoutComments.length <= 1); | ||
var headPosition = (function (text) { | ||
@@ -29,0 +29,0 @@ var match = text.match(/(^|\n)---\s*$/); |
@@ -8,8 +8,8 @@ "use strict"; | ||
assert(flowCollection.valueRange !== null); | ||
context.assertSyntaxError(flowCollection.items.length >= 2, "Missing flow collection close marker", function () { return context.transformRange(flowCollection.valueRange.end - 1); }); | ||
context.assertSyntaxError(flowCollection.type === "FLOW_MAP" | ||
assert(flowCollection.items.length >= 2); | ||
assert(flowCollection.type === "FLOW_MAP" | ||
? flowCollection.items[0] === "{" && | ||
utils_1.getLast(flowCollection.items) === "}" | ||
: flowCollection.items[0] === "[" && | ||
utils_1.getLast(flowCollection.items) === "]", "Unpaired flow collection close marker", function () { return context.transformRange(flowCollection.valueRange.end - 1); }); | ||
utils_1.getLast(flowCollection.items) === "]"); | ||
var hasColon = false; | ||
@@ -23,3 +23,3 @@ var hasQuestion = false; | ||
var lastIndex = flowCollection.items.length - 2; | ||
var _loop_1 = function (i) { | ||
for (var i = 1; i <= lastIndex; i++) { | ||
var isComment = false; | ||
@@ -30,3 +30,3 @@ var item = flowCollection.items[i]; | ||
assert(true); | ||
return "continue"; | ||
continue; // convince control flow analysis | ||
} | ||
@@ -42,8 +42,8 @@ else if (typeof item === "object" && item.type === "COMMENT") { | ||
if (item === "?") { | ||
context.assertSyntaxError(!hasColon, "Key marker is not allowed to be behind value marker in the same group", function () { return context.transformRange(getItemRange(item)); }); | ||
context.assertSyntaxError(!hasQuestion, "Key marker is not allowed to be appeared more than once in the same group", function () { return context.transformRange(getItemRange(item)); }); | ||
assert(!hasColon); | ||
assert(!hasQuestion); | ||
hasQuestion = true; | ||
} | ||
else if (item === ":") { | ||
context.assertSyntaxError(!hasColon, "Value marker is not allowed to be appeared more than once in the same group", function () { return context.transformRange(getItemRange(item)); }); | ||
assert(!hasColon); | ||
hasColon = true; | ||
@@ -54,3 +54,3 @@ } | ||
if (i === lastIndex && itemBuffer.length === 0 && isComment) { | ||
return "break"; | ||
break; | ||
} | ||
@@ -64,7 +64,2 @@ if (item === "," || i === lastIndex) { | ||
} | ||
}; | ||
for (var i = 1; i <= lastIndex; i++) { | ||
var state_1 = _loop_1(i); | ||
if (state_1 === "break") | ||
break; | ||
} | ||
@@ -101,12 +96,4 @@ return tslib_1.__assign({ type: "flowCollection", children: children, position: context.transformRange(flowCollection.valueRange) }, utils_1.createCommentAttachableNode(), utils_1.createContentNode()); | ||
: itemBuffer.slice(0, -1)); | ||
assert(itemBufferWithoutComma.length <= 4); | ||
if (itemBufferWithoutComma.length === 0) { | ||
// [ , ] or { , } | ||
assert(itemBuffer.length === 1); | ||
var beforeCommaPosition = context.transformRange(rangeBuffer[0].start); | ||
return type === "FLOW_MAP" | ||
? createMappingItem(context.transformNode(null), context.transformNode(null), beforeCommaPosition) | ||
: createSequenceItem(context.transformNode(null), beforeCommaPosition); | ||
} | ||
else if (itemBufferWithoutComma.length === 1) { | ||
assert(itemBufferWithoutComma.length > 0 && itemBufferWithoutComma.length <= 4); | ||
if (itemBufferWithoutComma.length === 1) { | ||
var item = itemBufferWithoutComma[0]; | ||
@@ -113,0 +100,0 @@ if (typeof item === "string") { |
@@ -43,3 +43,3 @@ "use strict"; | ||
unshiftCount++; | ||
context.assertSyntaxError(itemsWithoutComments[index].type === "MAP_KEY", "Expected a MAP_KEY, but got " + itemsWithoutComments[index].type + ".", function () { return context.transformRange(utils_1.getRange(itemsWithoutComments[index])); }); | ||
assert(itemsWithoutComments[index].type === "MAP_KEY"); | ||
} | ||
@@ -46,0 +46,0 @@ return reduced.concat(buffer |
import { Context } from "../transform"; | ||
import { Position } from "../types"; | ||
export declare function transformRange(range: number | { | ||
export declare function transformRange(range: { | ||
start: number; | ||
end: number; | ||
}, context: Context): Position; |
"use strict"; | ||
exports.__esModule = true; | ||
function transformRange(range, context) { | ||
if (typeof range === "number") { | ||
var point = context.transformOffset(range); | ||
return { | ||
start: point, | ||
end: point | ||
}; | ||
} | ||
else { | ||
return { | ||
start: context.transformOffset(range.start), | ||
end: context.transformOffset(range.end) | ||
}; | ||
} | ||
return { | ||
start: context.transformOffset(range.start), | ||
end: context.transformOffset(range.end) | ||
}; | ||
} | ||
exports.transformRange = transformRange; |
/// <reference types="yaml" /> | ||
import { Context } from "./transform"; | ||
import { Comment, CommentAttachable, Content, Null, Point, Position, YamlUnistNode } from "./types"; | ||
import { Comment, CommentAttachable, Content, Null, Point, YAMLSyntaxError, YamlUnistNode } from "./types"; | ||
export declare function overwriteStart(node: YamlUnistNode, start: Point): void; | ||
@@ -8,3 +8,3 @@ export declare function overwriteEnd(node: YamlUnistNode, end: Point): void; | ||
export declare function defineCommentParent(comment: Comment, parent: YamlUnistNode): void; | ||
export declare function assertSyntaxError(value: boolean, message: string | (() => string), position: Position | (() => Position), context: Context): void; | ||
export declare function createError(rawError: Extract<yaml.YAMLError, SyntaxError>, context: Context): YAMLSyntaxError; | ||
export declare function createContentNode(): Content; | ||
@@ -15,2 +15,2 @@ export declare function createCommentAttachableNode(): CommentAttachable; | ||
export declare function updateEndPoints(node: YamlUnistNode, nodeStack?: YamlUnistNode[]): void; | ||
export declare function getRange(node: yaml.Node): yaml.Range; | ||
export declare function isYAMLError(e: any): e is yaml.YAMLError; |
@@ -22,16 +22,12 @@ "use strict"; | ||
exports.defineCommentParent = defineCommentParent; | ||
function createError(message, position, context) { | ||
var error = new SyntaxError(message); | ||
function createError(rawError, context) { | ||
var error = new SyntaxError(rawError.message); | ||
error.name = "YAMLSyntaxError"; | ||
error.source = context.text; | ||
error.position = position; | ||
error.position = context.transformRange( | ||
// istanbul ignore next | ||
(rawError.source.range || rawError.source.valueRange)); | ||
return error; | ||
} | ||
function assertSyntaxError(value, message, position, context) { | ||
if (!value) { | ||
// istanbul ignore next | ||
throw createError(typeof message === "function" ? message() : message, typeof position === "function" ? position() : position, context); | ||
} | ||
} | ||
exports.assertSyntaxError = assertSyntaxError; | ||
exports.createError = createError; | ||
function createContentNode() { | ||
@@ -97,5 +93,6 @@ return { | ||
exports.updateEndPoints = updateEndPoints; | ||
function getRange(node) { | ||
return /* istanbul ignore next */ (node.valueRange || node.range); | ||
function isYAMLError(e) { | ||
return (e instanceof Error && | ||
(e.name === "YAMLSyntaxError" || e.name === "YAMLSemanticError")); | ||
} | ||
exports.getRange = getRange; | ||
exports.isYAMLError = isYAMLError; |
{ | ||
"name": "yaml-unist-parser", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"description": "A YAML parser that produces output compatible with unist", | ||
@@ -36,16 +36,16 @@ "keywords": [ | ||
"jest": "21.2.1", | ||
"prettier": "1.12.1", | ||
"standard-version": "4.3.0", | ||
"prettier": "1.13.0", | ||
"standard-version": "4.4.0", | ||
"ts-jest": "21.2.4", | ||
"tslint": "5.10.0", | ||
"tslint-config-prettier": "1.12.0", | ||
"tslint-config-prettier": "1.13.0", | ||
"tslint-plugin-prettier": "1.3.0", | ||
"typescript": "2.8.3", | ||
"yaml": "1.0.0-beta.5" | ||
"typescript": "2.9.2", | ||
"yaml": "1.0.0-beta.7" | ||
}, | ||
"peerDependencies": { | ||
"yaml": "^1.0.0-beta.5" | ||
"yaml": "^1.0.0-beta.7" | ||
}, | ||
"engines": { | ||
"node": ">= 4" | ||
"node": ">= 6" | ||
}, | ||
@@ -52,0 +52,0 @@ "files": [ |
@@ -9,3 +9,5 @@ # yaml-unist-parser | ||
<!-- TODO: enable after stable | ||
[Changelog](https://github.com/ikatyang/yaml-unist-parser/blob/master/CHANGELOG.md) | ||
--> | ||
@@ -12,0 +14,0 @@ ## Install |
52
56920
1284