From 36cc762c8a3723c4c058aace405ffa35d7f1cf8b Mon Sep 17 00:00:00 2001 From: Patrick McElhaney Date: Sun, 31 Dec 2017 14:21:58 -0500 Subject: [PATCH] Chore: refactor so adding transforms is easier (fixes #9) 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. --- bin/eslint-transforms.js | 2 +- .../new-rule-format.js | 0 .../already-transformed.input.js | 0 .../already-transformed.input.md | 1 + .../already-transformed.output.js | 0 .../new-rule-format/arrow-function.input.js | 0 .../new-rule-format/arrow-function.md | 1 + .../new-rule-format/arrow-function.output.js | 0 .../custom-identifier-context.input.js | 0 .../custom-identifier-context.md | 5 + .../custom-identifier-context.output.js | 0 .../new-rule-format/fixable.input.js | 0 .../transforms/new-rule-format/fixable.md | 1 + .../new-rule-format/fixable.output.js | 0 .../new-rule-format/no-schema.input.js | 0 .../transforms/new-rule-format/no-schema.md | 1 + .../new-rule-format/no-schema.output.js | 0 .../schema-uses-variables.input.js | 0 .../new-rule-format/schema-uses-variables.md | 1 + .../schema-uses-variables.output.js | 0 .../new-rule-format/simple.input.js | 0 .../transforms/new-rule-format/simple.md | 1 + .../new-rule-format/simple.output.js | 0 .../new-rule-format/with-comments.input.js | 0 .../new-rule-format/with-comments.md | 1 + .../new-rule-format/with-comments.output.js | 0 .../wrapped-in-function.input.js | 0 .../new-rule-format/wrapped-in-function.md | 7 ++ .../wrapped-in-function.output.js | 0 .../new-rule-format.js => transforms.js} | 101 ++++++++++++------ 30 files changed, 87 insertions(+), 35 deletions(-) rename lib/{new-rule-format => transforms}/new-rule-format.js (100%) rename tests/fixtures/{lib => transforms}/new-rule-format/already-transformed.input.js (100%) create mode 100644 tests/fixtures/transforms/new-rule-format/already-transformed.input.md rename tests/fixtures/{lib => transforms}/new-rule-format/already-transformed.output.js (100%) rename tests/fixtures/{lib => transforms}/new-rule-format/arrow-function.input.js (100%) create mode 100644 tests/fixtures/transforms/new-rule-format/arrow-function.md rename tests/fixtures/{lib => transforms}/new-rule-format/arrow-function.output.js (100%) rename tests/fixtures/{lib => transforms}/new-rule-format/custom-identifier-context.input.js (100%) create mode 100644 tests/fixtures/transforms/new-rule-format/custom-identifier-context.md rename tests/fixtures/{lib => transforms}/new-rule-format/custom-identifier-context.output.js (100%) rename tests/fixtures/{lib => transforms}/new-rule-format/fixable.input.js (100%) create mode 100644 tests/fixtures/transforms/new-rule-format/fixable.md rename tests/fixtures/{lib => transforms}/new-rule-format/fixable.output.js (100%) rename tests/fixtures/{lib => transforms}/new-rule-format/no-schema.input.js (100%) create mode 100644 tests/fixtures/transforms/new-rule-format/no-schema.md rename tests/fixtures/{lib => transforms}/new-rule-format/no-schema.output.js (100%) rename tests/fixtures/{lib => transforms}/new-rule-format/schema-uses-variables.input.js (100%) create mode 100644 tests/fixtures/transforms/new-rule-format/schema-uses-variables.md rename tests/fixtures/{lib => transforms}/new-rule-format/schema-uses-variables.output.js (100%) rename tests/fixtures/{lib => transforms}/new-rule-format/simple.input.js (100%) create mode 100644 tests/fixtures/transforms/new-rule-format/simple.md rename tests/fixtures/{lib => transforms}/new-rule-format/simple.output.js (100%) rename tests/fixtures/{lib => transforms}/new-rule-format/with-comments.input.js (100%) create mode 100644 tests/fixtures/transforms/new-rule-format/with-comments.md rename tests/fixtures/{lib => transforms}/new-rule-format/with-comments.output.js (100%) rename tests/fixtures/{lib => transforms}/new-rule-format/wrapped-in-function.input.js (100%) create mode 100644 tests/fixtures/transforms/new-rule-format/wrapped-in-function.md rename tests/fixtures/{lib => transforms}/new-rule-format/wrapped-in-function.output.js (100%) rename tests/lib/{new-rule-format/new-rule-format.js => transforms.js} (61%) diff --git a/bin/eslint-transforms.js b/bin/eslint-transforms.js index 3899440..154f717 100755 --- a/bin/eslint-transforms.js +++ b/bin/eslint-transforms.js @@ -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(" ")); diff --git a/lib/new-rule-format/new-rule-format.js b/lib/transforms/new-rule-format.js similarity index 100% rename from lib/new-rule-format/new-rule-format.js rename to lib/transforms/new-rule-format.js diff --git a/tests/fixtures/lib/new-rule-format/already-transformed.input.js b/tests/fixtures/transforms/new-rule-format/already-transformed.input.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/already-transformed.input.js rename to tests/fixtures/transforms/new-rule-format/already-transformed.input.js diff --git a/tests/fixtures/transforms/new-rule-format/already-transformed.input.md b/tests/fixtures/transforms/new-rule-format/already-transformed.input.md new file mode 100644 index 0000000..296a156 --- /dev/null +++ b/tests/fixtures/transforms/new-rule-format/already-transformed.input.md @@ -0,0 +1 @@ +tests that the transform doesn't fail if the rule was already in the new format \ No newline at end of file diff --git a/tests/fixtures/lib/new-rule-format/already-transformed.output.js b/tests/fixtures/transforms/new-rule-format/already-transformed.output.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/already-transformed.output.js rename to tests/fixtures/transforms/new-rule-format/already-transformed.output.js diff --git a/tests/fixtures/lib/new-rule-format/arrow-function.input.js b/tests/fixtures/transforms/new-rule-format/arrow-function.input.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/arrow-function.input.js rename to tests/fixtures/transforms/new-rule-format/arrow-function.input.js diff --git a/tests/fixtures/transforms/new-rule-format/arrow-function.md b/tests/fixtures/transforms/new-rule-format/arrow-function.md new file mode 100644 index 0000000..317c2c3 --- /dev/null +++ b/tests/fixtures/transforms/new-rule-format/arrow-function.md @@ -0,0 +1 @@ +tests that the transform also works when the rule definition is an arrow function \ No newline at end of file diff --git a/tests/fixtures/lib/new-rule-format/arrow-function.output.js b/tests/fixtures/transforms/new-rule-format/arrow-function.output.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/arrow-function.output.js rename to tests/fixtures/transforms/new-rule-format/arrow-function.output.js diff --git a/tests/fixtures/lib/new-rule-format/custom-identifier-context.input.js b/tests/fixtures/transforms/new-rule-format/custom-identifier-context.input.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/custom-identifier-context.input.js rename to tests/fixtures/transforms/new-rule-format/custom-identifier-context.input.js diff --git a/tests/fixtures/transforms/new-rule-format/custom-identifier-context.md b/tests/fixtures/transforms/new-rule-format/custom-identifier-context.md new file mode 100644 index 0000000..b444845 --- /dev/null +++ b/tests/fixtures/transforms/new-rule-format/custom-identifier-context.md @@ -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({ ... }); +``` \ No newline at end of file diff --git a/tests/fixtures/lib/new-rule-format/custom-identifier-context.output.js b/tests/fixtures/transforms/new-rule-format/custom-identifier-context.output.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/custom-identifier-context.output.js rename to tests/fixtures/transforms/new-rule-format/custom-identifier-context.output.js diff --git a/tests/fixtures/lib/new-rule-format/fixable.input.js b/tests/fixtures/transforms/new-rule-format/fixable.input.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/fixable.input.js rename to tests/fixtures/transforms/new-rule-format/fixable.input.js diff --git a/tests/fixtures/transforms/new-rule-format/fixable.md b/tests/fixtures/transforms/new-rule-format/fixable.md new file mode 100644 index 0000000..94958b5 --- /dev/null +++ b/tests/fixtures/transforms/new-rule-format/fixable.md @@ -0,0 +1 @@ +tests that the transform can detect that a rule is fixable \ No newline at end of file diff --git a/tests/fixtures/lib/new-rule-format/fixable.output.js b/tests/fixtures/transforms/new-rule-format/fixable.output.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/fixable.output.js rename to tests/fixtures/transforms/new-rule-format/fixable.output.js diff --git a/tests/fixtures/lib/new-rule-format/no-schema.input.js b/tests/fixtures/transforms/new-rule-format/no-schema.input.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/no-schema.input.js rename to tests/fixtures/transforms/new-rule-format/no-schema.input.js diff --git a/tests/fixtures/transforms/new-rule-format/no-schema.md b/tests/fixtures/transforms/new-rule-format/no-schema.md new file mode 100644 index 0000000..0863055 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/tests/fixtures/lib/new-rule-format/no-schema.output.js b/tests/fixtures/transforms/new-rule-format/no-schema.output.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/no-schema.output.js rename to tests/fixtures/transforms/new-rule-format/no-schema.output.js diff --git a/tests/fixtures/lib/new-rule-format/schema-uses-variables.input.js b/tests/fixtures/transforms/new-rule-format/schema-uses-variables.input.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/schema-uses-variables.input.js rename to tests/fixtures/transforms/new-rule-format/schema-uses-variables.input.js diff --git a/tests/fixtures/transforms/new-rule-format/schema-uses-variables.md b/tests/fixtures/transforms/new-rule-format/schema-uses-variables.md new file mode 100644 index 0000000..288b692 --- /dev/null +++ b/tests/fixtures/transforms/new-rule-format/schema-uses-variables.md @@ -0,0 +1 @@ +tests that the transform can handle rules that have a schema definition that depends on variables that were declared above it \ No newline at end of file diff --git a/tests/fixtures/lib/new-rule-format/schema-uses-variables.output.js b/tests/fixtures/transforms/new-rule-format/schema-uses-variables.output.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/schema-uses-variables.output.js rename to tests/fixtures/transforms/new-rule-format/schema-uses-variables.output.js diff --git a/tests/fixtures/lib/new-rule-format/simple.input.js b/tests/fixtures/transforms/new-rule-format/simple.input.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/simple.input.js rename to tests/fixtures/transforms/new-rule-format/simple.input.js diff --git a/tests/fixtures/transforms/new-rule-format/simple.md b/tests/fixtures/transforms/new-rule-format/simple.md new file mode 100644 index 0000000..762a904 --- /dev/null +++ b/tests/fixtures/transforms/new-rule-format/simple.md @@ -0,0 +1 @@ +tests basic functionality of the transform \ No newline at end of file diff --git a/tests/fixtures/lib/new-rule-format/simple.output.js b/tests/fixtures/transforms/new-rule-format/simple.output.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/simple.output.js rename to tests/fixtures/transforms/new-rule-format/simple.output.js diff --git a/tests/fixtures/lib/new-rule-format/with-comments.input.js b/tests/fixtures/transforms/new-rule-format/with-comments.input.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/with-comments.input.js rename to tests/fixtures/transforms/new-rule-format/with-comments.input.js diff --git a/tests/fixtures/transforms/new-rule-format/with-comments.md b/tests/fixtures/transforms/new-rule-format/with-comments.md new file mode 100644 index 0000000..95a523a --- /dev/null +++ b/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 \ No newline at end of file diff --git a/tests/fixtures/lib/new-rule-format/with-comments.output.js b/tests/fixtures/transforms/new-rule-format/with-comments.output.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/with-comments.output.js rename to tests/fixtures/transforms/new-rule-format/with-comments.output.js diff --git a/tests/fixtures/lib/new-rule-format/wrapped-in-function.input.js b/tests/fixtures/transforms/new-rule-format/wrapped-in-function.input.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/wrapped-in-function.input.js rename to tests/fixtures/transforms/new-rule-format/wrapped-in-function.input.js diff --git a/tests/fixtures/transforms/new-rule-format/wrapped-in-function.md b/tests/fixtures/transforms/new-rule-format/wrapped-in-function.md new file mode 100644 index 0000000..aa8c9a5 --- /dev/null +++ b/tests/fixtures/transforms/new-rule-format/wrapped-in-function.md @@ -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 { ... }; +}); +``` \ No newline at end of file diff --git a/tests/fixtures/lib/new-rule-format/wrapped-in-function.output.js b/tests/fixtures/transforms/new-rule-format/wrapped-in-function.output.js similarity index 100% rename from tests/fixtures/lib/new-rule-format/wrapped-in-function.output.js rename to tests/fixtures/transforms/new-rule-format/wrapped-in-function.output.js diff --git a/tests/lib/new-rule-format/new-rule-format.js b/tests/lib/transforms.js similarity index 61% rename from tests/lib/new-rule-format/new-rule-format.js rename to tests/lib/transforms.js index 0f43504..565658f 100644 --- a/tests/lib/new-rule-format/new-rule-format.js +++ b/tests/lib/transforms.js @@ -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 @@ -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( @@ -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 }, {} @@ -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 from .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" - ]); -});