Skip to content

Commit

Permalink
Fix: don't mutate user-provided configs (fixes #329) (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored Apr 4, 2017
1 parent d5d1808 commit 628cf3a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
26 changes: 23 additions & 3 deletions espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ var lookahead,
extra,
lastToken;

/**
* Object.assign polyfill for Node < 4
* @param {Object} target The target object
* @param {...Object} sources Sources for the object
* @returns {Object} `target` after being mutated
*/
var assign = Object.assign || function assign(target) {
for (var argIndex = 1; argIndex < arguments.length; argIndex++) {
if (arguments[argIndex] !== null && typeof arguments[argIndex] === "object") {
var keys = Object.keys(arguments[argIndex]);

for (var keyIndex = 0; keyIndex < keys.length; keyIndex++) {
target[keys[keyIndex]] = arguments[argIndex][keys[keyIndex]];
}
}
}

return target;
};

/**
* Resets the extra object to its default.
* @returns {void}
Expand Down Expand Up @@ -515,7 +535,7 @@ function tokenize(code, options) {
lookahead = null;

// Options matching.
options = options || {};
options = assign({}, options);

var acornOptions = {
ecmaVersion: DEFAULT_ECMA_VERSION,
Expand Down Expand Up @@ -551,7 +571,7 @@ function tokenize(code, options) {

// apply parsing flags
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
extra.ecmaFeatures = options.ecmaFeatures;
extra.ecmaFeatures = assign({}, options.ecmaFeatures);
impliedStrict = extra.ecmaFeatures.impliedStrict;
extra.ecmaFeatures.impliedStrict = typeof impliedStrict === "boolean" && impliedStrict;
}
Expand Down Expand Up @@ -687,7 +707,7 @@ function parse(code, options) {

// apply parsing flags after sourceType to allow overriding
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
extra.ecmaFeatures = options.ecmaFeatures;
extra.ecmaFeatures = assign({}, options.ecmaFeatures);
impliedStrict = extra.ecmaFeatures.impliedStrict;
extra.ecmaFeatures.impliedStrict = typeof impliedStrict === "boolean" && impliedStrict;
if (options.ecmaFeatures.globalReturn) {
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,9 @@ describe("parse()", function() {
assert.deepEqual([ast.loc.end.line, ast.loc.end.column], [1, 5]);
});

it("should not mutate config", function() {
espree.parse("foo", Object.freeze({ ecmaFeatures: Object.freeze({}) }));
});

});
});
4 changes: 4 additions & 0 deletions tests/lib/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,8 @@ describe("tokenize()", function() {
);
});

it("should not mutate config", function() {
espree.tokenize("foo", Object.freeze({ ecmaFeatures: Object.freeze({}) }));
});

});

0 comments on commit 628cf3a

Please sign in to comment.