Skip to content

Commit

Permalink
run set rules once
Browse files Browse the repository at this point in the history
  • Loading branch information
fa93hws committed Jun 21, 2020
1 parent 902e55f commit 66d011f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
46 changes: 23 additions & 23 deletions lib/rule-tester/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,6 @@ class RuleTester {
* @type {Object}
*/
this.rules = {};

/**
* Linter map, cwd -> Linter instance
* @type {Map<string | undefined, Linter>}
*/
this.linterMap = new Map();
}

/**
Expand Down Expand Up @@ -422,22 +416,30 @@ class RuleTester {
* @param {Function} rule The rule to test.
* @param {{
* valid: (ValidTestCase | string)[],
* invalid: InvalidTestCase[]
* invalid: InvalidTestCase[],
* linterMapDevOnly?: Map<string | undefined, Linter>
* }} test The collection of tests to run.
* test.linterMapDevOnly is for testing purpose
* @returns {void}
*/
run(ruleName, rule, test) {

const testerConfig = this.testerConfig,
requiredScenarios = ["valid", "invalid"],
scenarioErrors = [],
linterMap = this.linterMap,
rules = this.rules;

if (lodash.isNil(test) || typeof test !== "object") {
throw new TypeError(`Test Scenarios for rule ${ruleName} : Could not find test scenario object`);
}

/**
* Linter map, cwd -> Linter instance
* Injection of value in `test` is for test purpose.
* @type {Map<string | undefined, Linter>}
*/
const { linterMapDevOnly: linterMap = new Map() } = test;

requiredScenarios.forEach(scenarioType => {
if (lodash.isNil(test[scenarioType])) {
scenarioErrors.push(`Could not find any ${scenarioType} test scenarios`);
Expand All @@ -460,23 +462,21 @@ class RuleTester {
if (!linterMap.has(cwd)) {
const linter = new Linter({ cwd });

linterMap.set(cwd, linter);
}
const linter = linterMap.get(cwd);
linter.defineRule(ruleName, Object.assign({}, 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);

// 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);
}
}));
linter.defineRules(rules);
return linter;
return (typeof rule === "function" ? rule : rule.create)(context);
}
}));
linter.defineRules(rules);
linterMap.set(cwd, linter);
}
return linterMap.get(cwd);
}

/**
Expand Down
19 changes: 10 additions & 9 deletions tests/lib/rule-tester/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const sinon = require("sinon"),
EventEmitter = require("events"),
{ RuleTester } = require("../../../lib/rule-tester"),
{ Linter } = require("../../../lib/linter"),
assert = require("chai").assert,
nodeAssert = require("assert"),
{ noop } = require("lodash");
Expand Down Expand Up @@ -751,16 +752,15 @@ describe("RuleTester", () => {
});

it("should pass-through the parser to the rule", () => {

// To generate the default linter
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: ["Eval(foo)"],
invalid: []
});

const linter = ruleTester.linterMap.get();
const linter = new Linter();
const spy = sinon.spy(linter, "verify");
const linterMap = new Map([
// eslint-disable-next-line no-undefined
[undefined, linter]
]);

// ruleTester = new RuleTester({ linterMapDevOnly: linterMap });
ruleTester = new RuleTester();
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
{
Expand All @@ -773,7 +773,8 @@ describe("RuleTester", () => {
parser: require.resolve("esprima"),
errors: [{ line: 1 }]
}
]
],
linterMapDevOnly: linterMap
});
assert.strictEqual(spy.args[1][1].parser, require.resolve("esprima"));
});
Expand Down

0 comments on commit 66d011f

Please sign in to comment.