Skip to content

Commit

Permalink
Chore: avoid monkeypatching Linter instances in RuleTester (#9276)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Sep 11, 2017
1 parent 28929cb commit 61f1093
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions lib/testers/rule-tester.js
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(rule);

Expand Down Expand Up @@ -341,35 +351,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);
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

0 comments on commit 61f1093

Please sign in to comment.