yaml-ast-parser
Advanced tools
Comparing version
@@ -9,2 +9,5 @@ import loader = require('./loader'); | ||
mark: Mark; | ||
private static CLASS_IDENTIFIER; | ||
static isInstance(instance: any): instance is YAMLException; | ||
getClassIdentifier(): string[]; | ||
constructor(reason: string, mark?: Mark); | ||
@@ -11,0 +14,0 @@ toString(compact?: boolean): any; |
@@ -12,2 +12,17 @@ "use strict"; | ||
} | ||
YAMLException.isInstance = function (instance) { | ||
if (instance != null && instance.getClassIdentifier | ||
&& typeof (instance.getClassIdentifier) == "function") { | ||
for (var _i = 0, _a = instance.getClassIdentifier(); _i < _a.length; _i++) { | ||
var currentIdentifier = _a[_i]; | ||
if (currentIdentifier == YAMLException.CLASS_IDENTIFIER) | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
YAMLException.prototype.getClassIdentifier = function () { | ||
var superIdentifiers = []; | ||
return superIdentifiers.concat(YAMLException.CLASS_IDENTIFIER); | ||
}; | ||
YAMLException.prototype.toString = function (compact) { | ||
@@ -22,2 +37,3 @@ if (compact === void 0) { compact = false; } | ||
}; | ||
YAMLException.CLASS_IDENTIFIER = "yaml-ast-parser.YAMLException"; | ||
return YAMLException; | ||
@@ -24,0 +40,0 @@ }()); |
@@ -112,2 +112,3 @@ "use strict"; | ||
this.errors = []; | ||
this.lines = []; | ||
this.input = input; | ||
@@ -119,2 +120,3 @@ this.filename = options['filename'] || null; | ||
this.allowAnyEscape = options['allowAnyEscape'] || false; | ||
this.ignoreDuplicateKeys = options['ignoreDuplicateKeys'] || false; | ||
this.implicitTypes = this.schema.compiledImplicit; | ||
@@ -134,2 +136,15 @@ this.typeMap = this.schema.compiledTypeMap; | ||
} | ||
function throwErrorFromPosition(state, position, message) { | ||
var line = positionToLine(state, position); | ||
if (!line) { | ||
return; | ||
} | ||
var hash = message + position; | ||
if (state.errorMap[hash]) { | ||
return; | ||
} | ||
var mark = new Mark(state.filename, state.input, position, line.line - 1, (position - line.start)); | ||
var error = new YAMLException(message, mark); | ||
state.errors.push(error); | ||
} | ||
function throwError(state, message) { | ||
@@ -269,2 +284,8 @@ var error = generateError(state, message); | ||
} | ||
!state.ignoreDuplicateKeys && _result.mappings.forEach(function (sibling) { | ||
if (sibling.key && sibling.key.value === (mapping.key && mapping.key.value)) { | ||
throwErrorFromPosition(state, mapping.key.startPosition, 'duplicate key'); | ||
throwErrorFromPosition(state, sibling.key.startPosition, 'duplicate key'); | ||
} | ||
}); | ||
_result.mappings.push(mapping); | ||
@@ -291,3 +312,28 @@ _result.endPosition = valueNode ? valueNode.endPosition : keyNode.endPosition + 1; | ||
state.lineStart = state.position; | ||
state.lines.push({ | ||
start: state.lineStart, | ||
line: state.line | ||
}); | ||
} | ||
var Line = (function () { | ||
function Line() { | ||
} | ||
return Line; | ||
}()); | ||
function positionToLine(state, position) { | ||
var line; | ||
for (var i = 0; i < state.lines.length; i++) { | ||
if (state.lines[i].start > position) { | ||
break; | ||
} | ||
line = state.lines[i]; | ||
} | ||
if (!line) { | ||
return { | ||
start: 0, | ||
line: 0 | ||
}; | ||
} | ||
return line; | ||
} | ||
function skipSeparationSpace(state, allowComments, checkIndent) { | ||
@@ -294,0 +340,0 @@ var lineBreaks = 0, ch = state.input.charCodeAt(state.position); |
{ | ||
"name": "yaml-ast-parser", | ||
"version": "0.0.30", | ||
"version": "0.0.31", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -16,2 +16,22 @@ | ||
private static CLASS_IDENTIFIER = "yaml-ast-parser.YAMLException"; | ||
public static isInstance(instance : any) : instance is YAMLException { | ||
if(instance != null && instance.getClassIdentifier | ||
&& typeof(instance.getClassIdentifier) == "function"){ | ||
for (let currentIdentifier of instance.getClassIdentifier()){ | ||
if(currentIdentifier == YAMLException.CLASS_IDENTIFIER) return true; | ||
} | ||
} | ||
return false; | ||
} | ||
public getClassIdentifier() : string[] { | ||
var superIdentifiers = []; | ||
return superIdentifiers.concat(YAMLException.CLASS_IDENTIFIER); | ||
} | ||
constructor(reason:string, mark:Mark=null) { | ||
@@ -18,0 +38,0 @@ this.name = 'YAMLException'; |
@@ -162,3 +162,6 @@ import ast=require("./yamlAST") | ||
allowAnyEscape:boolean | ||
ignoreDuplicateKeys: boolean; | ||
lines: Line[] = []; | ||
constructor(input:string,options:any){ | ||
@@ -172,2 +175,3 @@ this.input = input; | ||
this.allowAnyEscape = options['allowAnyEscape'] || false; | ||
this.ignoreDuplicateKeys = options['ignoreDuplicateKeys'] || false; | ||
@@ -196,2 +200,22 @@ this.implicitTypes = this.schema.compiledImplicit; | ||
function throwErrorFromPosition(state, position: number, message) { | ||
var line = positionToLine(state, position); | ||
if(!line) { | ||
return; | ||
} | ||
var hash = message + position; | ||
if(state.errorMap[hash]) { | ||
return; | ||
} | ||
var mark = new Mark(state.filename, state.input, position, line.line-1, (position - line.start)); | ||
var error = new YAMLException(message, mark); | ||
state.errors.push(error); | ||
} | ||
function throwError(state:State, message) { | ||
@@ -381,2 +405,10 @@ //FIXME | ||
} | ||
!state.ignoreDuplicateKeys && _result.mappings.forEach(sibling => { | ||
if(sibling.key && sibling.key.value === (mapping.key && mapping.key.value)) { | ||
throwErrorFromPosition(state, mapping.key.startPosition, 'duplicate key'); | ||
throwErrorFromPosition(state, sibling.key.startPosition, 'duplicate key'); | ||
} | ||
}); | ||
_result.mappings.push(mapping) | ||
@@ -407,4 +439,35 @@ _result.endPosition=valueNode? valueNode.endPosition : keyNode.endPosition+1; //FIXME.workaround should be position of ':' indeed | ||
state.lineStart = state.position; | ||
state.lines.push({ | ||
start: state.lineStart, | ||
line: state.line | ||
}); | ||
} | ||
class Line { | ||
start: number; | ||
line: number; | ||
} | ||
function positionToLine(state: State, position: number): Line { | ||
var line: Line; | ||
for(var i = 0; i < state.lines.length; i++) { | ||
if(state.lines[i].start > position) { | ||
break; | ||
} | ||
line = state.lines[i]; | ||
} | ||
if(!line) { | ||
return { | ||
start: 0, | ||
line: 0 | ||
} | ||
} | ||
return line; | ||
} | ||
function skipSeparationSpace(state:State, allowComments, checkIndent) { | ||
@@ -411,0 +474,0 @@ var lineBreaks = 0, |
@@ -6,2 +6,3 @@ { | ||
"gitUrl" : "https://github.com/raml-org/raml-js-parser-2.git", | ||
"gitBranch": "develop", | ||
"installTypings" : true | ||
@@ -12,2 +13,3 @@ }, | ||
"gitUrl" : "https://github.com/raml-org/raml-definition-system.git", | ||
"gitBranch": "develop", | ||
"installTypings" : true | ||
@@ -19,3 +21,4 @@ }, | ||
"gitUrl" : "https://github.com/raml-org/typesystem-ts.git", | ||
"installTypings" : true | ||
"installTypings" : true, | ||
"gitBranch": "develop" | ||
}, | ||
@@ -35,5 +38,6 @@ "ts-structure-parser" : { | ||
"gitUrl" : "https://github.com/mulesoft-labs/ts-model.git", | ||
"installTypings" : true | ||
"installTypings" : true, | ||
"gitBranch": "develop" | ||
} | ||
} | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
357033
1.92%7062
1.86%