Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: don't mutate user-provided configs (fixes #329) #330

Merged
merged 1 commit into from
Apr 4, 2017
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
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({}) }));
});

});