Skip to content

Commit

Permalink
Chore: refactor so adding transforms is easier (fixes eslint#9)
Browse files Browse the repository at this point in the history
To add a new transform:

1. Create a directory under tests/fixtures/transforms
2. Add the test fixtures (*.input.js, *.output.js, and *.md)
3. Implement the transform in lib/transforms

There's no need to create new (Mocha) test file. At this point, documenting the transform is still manual, but it could be easily automated.
  • Loading branch information
Patrick McElhaney authored and Patrick McElhaney committed Dec 31, 2017
1 parent 1dc288b commit a4514ad
Show file tree
Hide file tree
Showing 30 changed files with 87 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bin/eslint-transforms.js
Expand Up @@ -39,6 +39,6 @@ function execWithNodeModules(cmd) {
execWithNodeModules([
"jscodeshift",
"-t",
path.resolve(__dirname, "../lib/" + transform + "/" + transform + ".js"),
path.resolve(__dirname, "../lib/transforms/" + transform + ".js"),
args.join(" ")
].join(" "));
File renamed without changes.
@@ -0,0 +1 @@
tests that the transform doesn't fail if the rule was already in the new format
@@ -0,0 +1 @@
tests that the transform also works when the rule definition is an arrow function
@@ -0,0 +1,5 @@
tests that the transform can handle rules that use an identifier for the "context" object that is not "context", e.g.:

```js
customContextName.report({ ... });
```
1 change: 1 addition & 0 deletions tests/fixtures/transforms/new-rule-format/fixable.md
@@ -0,0 +1 @@
tests that the transform can detect that a rule is fixable
1 change: 1 addition & 0 deletions tests/fixtures/transforms/new-rule-format/no-schema.md
@@ -0,0 +1 @@
tests that the transform can handle rules that have no schema being exported
@@ -0,0 +1 @@
tests that the transform can handle rules that have a schema definition that depends on variables that were declared above it
1 change: 1 addition & 0 deletions tests/fixtures/transforms/new-rule-format/simple.md
@@ -0,0 +1 @@
tests basic functionality of the transform
1 change: 1 addition & 0 deletions tests/fixtures/transforms/new-rule-format/with-comments.md
@@ -0,0 +1 @@
tests that the transform can handle comments in different nodes that will be moved around
@@ -0,0 +1,7 @@
tests that the transform works when the rule definition is wrapped in a function:

```js
module.exports = doSomething(function(context) {
return { ... };
});
```
Expand Up @@ -13,7 +13,8 @@ var os = require("os");
var path = require("path");
var expect = require("chai").expect;

var newRuleFormatTransform = require("../../../lib/new-rule-format/new-rule-format");
var fixturesRootPath = path.join(__dirname, "../fixtures/transforms");


/**
* Returns a new string with all the EOL markers from the string passed in
Expand All @@ -40,7 +41,7 @@ function normalizeLineEndngs(input) {
* @private
*/
function testTransformWithFixture(transform, transformFixturePrefix) {
var fixtureDir = path.join(__dirname, "../../fixtures/lib/new-rule-format");
var fixtureDir = path.join(fixturesRootPath, transform.name);
var inputPath = path.join(fixtureDir, transformFixturePrefix + ".input.js");
var source = fs.readFileSync(inputPath, "utf8");
var expectedOutput = fs.readFileSync(
Expand All @@ -50,7 +51,7 @@ function testTransformWithFixture(transform, transformFixturePrefix) {

it("transforms correctly using \"" + transformFixturePrefix + "\" fixture", function() {

var output = transform(
var output = transform.fn(
{ path: inputPath, source: source },
{ jscodeshift: jscodeshift },
{}
Expand Down Expand Up @@ -82,41 +83,73 @@ function testTransformWithFixtures(transform, fixtures) {
});
}

describe("New Rule Format transform", function() {
testTransformWithFixtures(newRuleFormatTransform, [
// tests basic functionality of the transform
"simple",

// tests that the transform can detect that a rule is fixable
"fixable",
/**
* Determines if the file name is *.input.js
*
* @param {string} name - file name
* @returns {boolean} - does it match?
* @private
*/
function inputsFilter(name) {
return /\.input\.js/.test(name);
}

// tests that the transform can handle rules that have no schema being exported
"no-schema",
/**
* extracts <test-name> from <test-name>.input.js
*
* @param {string} name - file name
* @returns {string} - the test name
* @private
*/
function extractTestName(name) {
return name.split(".").shift();
}

// tests that the transform can handle rules that use an identifier for the
// "context" object that is not "context", e.g.:
// customContextName.report({ ... });
"custom-identifier-context",

// tests that the transform can handle rules that have a schema definition that
// depends on variables that were declared above it
"schema-uses-variables",
/**
* Loads the transform module
*
* @param {string} name - the transform name
* @returns {function} - the module
* @private
*/
function loadTransformFn(name) {
return require("../../lib/transforms/" + name);
}

// tests that the transform can handle comments in different nodes that will be
// moved around
"with-comments",
/**
* loads a transform object
*
* @param {string} name - the name of the tranform
* @returns {object} - a transform object ({name, fn})
* @private
*/
function loadTransform(name) {
return {
name: name,
fn: loadTransformFn(name)
};
}

// tests that the transform doesn't fail if the rule was already in the new format
"already-transformed",
/**
* defines a test suite for a transform
*
* @param {object} transform - {name, fn}
* @returns {void}
*/
function describeTransform(transform) {
describe(transform.name, function() {
var fixtureDir = path.join(fixturesRootPath, transform.name);
var tests = fs
.readdirSync(fixtureDir)
.filter(inputsFilter)
.map(extractTestName);
testTransformWithFixtures(transform, tests);
});
}

// tests that the transform also works when the rule definition is an arrow function
"arrow-function",
fs
.readdirSync(fixturesRootPath)
.map(loadTransform)
.forEach(describeTransform);

// tests that the transform works when the rule definition is wrapped in a function:
//
// module.exports = doSomething(function(context) {
// return { ... };
// });
"wrapped-in-function"
]);
});

0 comments on commit a4514ad

Please sign in to comment.