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

Chore: avoid monkeypatching Linter instances in RuleTester #9276

Merged
merged 1 commit into from
Sep 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 16 additions & 30 deletions lib/testers/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,17 @@ class RuleTester {
config.rules[ruleName] = 1;
}

linter.defineRule(ruleName, rule);
linter.defineRule(ruleName, Object.assign({}, rule, {

// Create a wrapper rule that freezes the `context` properties.
create(context) {
freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);

return (typeof rule === "function" ? rule : rule.create)(context);
}
}));

const schema = validator.getRuleOptionsSchema(ruleName, linter.rules);

Expand Down Expand Up @@ -343,35 +353,11 @@ class RuleTester {
}
}));

// Freezes rule-context properties.
const originalGet = linter.rules.get;

try {
linter.rules.get = function(ruleId) {
const originalRule = originalGet.call(linter.rules, ruleId);

return {
meta: originalRule.meta,
create(context) {
Object.freeze(context);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Object.freeze(context) line was removed above because rule contexts have already been frozen since ESLint v2.0.0.

freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);

return originalRule.create(context);
}
};

};

return {
messages: linter.verify(code, config, filename, true),
beforeAST,
afterAST: cloneDeeplyExcludesParent(afterAST)
};
} finally {
linter.rules.get = originalGet;
}
return {
messages: linter.verify(code, config, filename, true),
beforeAST,
afterAST: cloneDeeplyExcludesParent(afterAST)
};
}

/**
Expand Down