Skip to content
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
8 changes: 6 additions & 2 deletions lib/js-yaml/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function mergeMappings(state, destination, source, overridableKeys) {
}
}

function storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode) {
function storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode, startLine, startPos) {
var index, quantity;

keyNode = String(keyNode);
Expand All @@ -302,6 +302,8 @@ function storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valu
if (!state.json &&
!_hasOwnProperty.call(overridableKeys, keyNode) &&
_hasOwnProperty.call(_result, keyNode)) {
state.line = startLine || state.line;
state.position = startPos || state.position;
throwError(state, 'duplicated mapping key');
}
_result[keyNode] = valueNode;
Expand Down Expand Up @@ -949,6 +951,7 @@ function readBlockMapping(state, nodeIndent, flowIndent) {
var following,
allowCompact,
_line,
_pos,
_tag = state.tag,
_anchor = state.anchor,
_result = {},
Expand All @@ -969,6 +972,7 @@ function readBlockMapping(state, nodeIndent, flowIndent) {
while (ch !== 0) {
following = state.input.charCodeAt(state.position + 1);
_line = state.line; // Save the current line.
_pos = state.position;

//
// Explicit notation case. There are two separate blocks:
Expand Down Expand Up @@ -1063,7 +1067,7 @@ function readBlockMapping(state, nodeIndent, flowIndent) {
}

if (!atExplicitKey) {
storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode);
storeMappingPair(state, _result, overridableKeys, keyTag, keyNode, valueNode, _line, _pos);
keyTag = keyNode = valueNode = null;
}

Expand Down
15 changes: 15 additions & 0 deletions test/issues/0243-basic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
a:
b: 1
c: 2
d:
e: 3
f: 4
duplicate: # 1
id: 1234
note: "this is the first one, so it won't raise an error"
duplicate: # 2
id: 5678
note: "this is the second one, so it will raise an error, hopefully at the start of the key"
g:
h: 5
i: 6
15 changes: 15 additions & 0 deletions test/issues/0243-nested.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
a:
b: 1
c: 2
d:
e: 3
f: 4
duplicate: # 1
id: 1234
note: "this is the first one, so it won't raise an error"
duplicate: # 2
id: 5678
note: "this is the second one, so it will raise an error, hopefully at the start of the key"
g:
h: 5
i: 6
29 changes: 29 additions & 0 deletions test/issues/0243.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';


var assert = require('assert');
var yaml = require('../../');
var readFileSync = require('fs').readFileSync;


test('Duplicated mapping key errors on top level throw at beginning of key', function () {
var src = readFileSync(require('path').join(__dirname, '/0243-basic.yml'), 'utf8');
var lines = src.split('\n');

try {
yaml.safeLoad(src);
} catch (e) {
assert.equal(lines[e.mark.line], 'duplicate: # 2');
}
});

test('Duplicated mapping key errors inside of mapping values throw at beginning of key', function () {
var src = readFileSync(require('path').join(__dirname, '/0243-nested.yml'), 'utf8');
var lines = src.split('\n');

try {
yaml.safeLoad(src);
} catch (e) {
assert.equal(lines[e.mark.line], ' duplicate: # 2');
}
});