From adab63590f283c48cb4f12d2470c8f2d2de9cc51 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 27 Mar 2017 15:20:14 -0700 Subject: [PATCH] Add Babel 6 compatibility (#86) * Use "multidep" to install "babel-core" * Port "strip-data-test-properties-plugin" to Babel 6 * Conditionally add Babel 5 or Babel 6 plugin to the host app --- .gitignore | 1 + index.js | 13 ++++++++- node-tests/fixtures/default/expected6.js | 6 +++++ node-tests/multidep.json | 6 +++++ .../strip-data-test-properties-plugin-test.js | 27 +++++++++++++++---- package.json | 7 +++-- strip-data-test-properties-plugin6.js | 27 +++++++++++++++++++ 7 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 node-tests/fixtures/default/expected6.js create mode 100644 node-tests/multidep.json create mode 100644 strip-data-test-properties-plugin6.js diff --git a/.gitignore b/.gitignore index 5ad14dd6..558809d8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ # dependencies /node_modules /bower_components +/node-tests/multidep_modules # misc /.sass-cache diff --git a/index.js b/index.js index e584de7d..85cc1628 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,8 @@ /* eslint-env node */ +const VersionChecker = require('ember-cli-version-checker'); + module.exports = { name: 'ember-test-selectors', @@ -54,11 +56,20 @@ module.exports = { // add the StripDataTestPropertiesPlugin to the list of plugins used by // the `ember-cli-babel` addon if (this._stripTestSelectors && !this._registeredWithBabel) { + let checker = new VersionChecker(this).for('ember-cli-babel', 'npm'); + app.options = app.options || {}; app.options.babel = app.options.babel || {}; app.options.babel.plugins = app.options.babel.plugins || []; - app.options.babel.plugins.push(require('./strip-data-test-properties-plugin')); + if (checker.satisfies('^5.0.0')) { + app.options.babel.plugins.push(require('./strip-data-test-properties-plugin')); + } else if (checker.satisfies('^6.0.0-beta.1')) { + app.options.babel.plugins.push(require('./strip-data-test-properties-plugin6')); + } else { + this.ui.writeWarnLine('ember-test-selectors: You are using an unsupported ember-cli-babel version. data-test ' + + 'properties are not automatically stripped from your JS code.'); + } this._registeredWithBabel = true; } diff --git a/node-tests/fixtures/default/expected6.js b/node-tests/fixtures/default/expected6.js new file mode 100644 index 00000000..06b896fa --- /dev/null +++ b/node-tests/fixtures/default/expected6.js @@ -0,0 +1,6 @@ +import Ember from 'ember'; + +export default Ember.Component.extend({ + foo: 'foo', + 'data-test': 'test' +}); diff --git a/node-tests/multidep.json b/node-tests/multidep.json new file mode 100644 index 00000000..819af88d --- /dev/null +++ b/node-tests/multidep.json @@ -0,0 +1,6 @@ +{ + "path": "node-tests/multidep_modules", + "versions": { + "babel-core": ["5.8.33", "6.24.0"] + } +} diff --git a/node-tests/strip-data-test-properties-plugin-test.js b/node-tests/strip-data-test-properties-plugin-test.js index 20eac368..295d67ff 100644 --- a/node-tests/strip-data-test-properties-plugin-test.js +++ b/node-tests/strip-data-test-properties-plugin-test.js @@ -1,17 +1,34 @@ var fs = require('fs'); var assert = require('assert'); +var multidepRequire = require('multidep')('node-tests/multidep.json'); -var babel = require('babel-core'); -var StripDataTestPropertiesPlugin = require('../strip-data-test-properties-plugin'); +var babel5 = multidepRequire('babel-core', '5.8.33'); +var babel6 = multidepRequire('babel-core', '6.24.0'); + +var StripDataTestPropertiesPlugin5 = require('../strip-data-test-properties-plugin'); +var StripDataTestPropertiesPlugin6 = require('../strip-data-test-properties-plugin6'); function testFixture(name) { - it('fixture: ' + name, function() { + it('Babel5: fixture: ' + name, function() { var fixturePath = __dirname + '/fixtures/' + name + '/fixture.js'; var expectedPath = __dirname + '/fixtures/' + name + '/expected.js'; var expected = fs.readFileSync(expectedPath).toString(); - var result = babel.transformFileSync(fixturePath, { - plugins: [StripDataTestPropertiesPlugin], + var result = babel5.transformFileSync(fixturePath, { + plugins: [StripDataTestPropertiesPlugin5], + }); + + assert.strictEqual(result.code.trim(), expected.trim()); + }); + + it('Babel6: fixture: ' + name, function() { + var fixturePath = __dirname + '/fixtures/' + name + '/fixture.js'; + var expectedPath = __dirname + '/fixtures/' + name + '/expected6.js'; + + var expected = fs.readFileSync(expectedPath).toString(); + + var result = babel6.transformFileSync(fixturePath, { + plugins: [StripDataTestPropertiesPlugin6], }); assert.strictEqual(result.code.trim(), expected.trim()); diff --git a/package.json b/package.json index ef3bbca2..6f71c5bd 100644 --- a/package.json +++ b/package.json @@ -19,11 +19,13 @@ "test": "npm run test:keep && npm run test:strip", "test:all": "ember try:each", "test:keep": "ember test", + "pretest:node": "multidep node-tests/multidep.json", "test:node": "mocha node-tests", "test:strip": "STRIP_TEST_SELECTORS=true ember test" }, "dependencies": { - "ember-cli-babel": "^5.1.7" + "ember-cli-babel": "^5.1.7", + "ember-cli-version-checker": "^1.2.0" }, "devDependencies": { "broccoli-asset-rev": "^2.4.5", @@ -48,7 +50,8 @@ "eslint-plugin-ember": "^3.0.1", "eslint-plugin-qunit": "^2.3.0", "loader.js": "^4.0.10", - "mocha": "^3.2.0" + "mocha": "^3.2.0", + "multidep": "^2.0.2" }, "engines": { "node": ">= 4" diff --git a/strip-data-test-properties-plugin6.js b/strip-data-test-properties-plugin6.js new file mode 100644 index 00000000..ad129f82 --- /dev/null +++ b/strip-data-test-properties-plugin6.js @@ -0,0 +1,27 @@ +'use strict'; + +/* eslint-env node */ + +let TEST_SELECTOR_PREFIX = /data-test-.*/; + +function StripDataTestPropertiesPlugin() { + return { + visitor: { + Property(path) { + if (TEST_SELECTOR_PREFIX.test(path.node.key.value)) { + path.remove(); + } + }, + }, + }; +} + +StripDataTestPropertiesPlugin.baseDir = function() { + return __dirname; +}; + +StripDataTestPropertiesPlugin.cacheKey = function() { + return 'ember-test-selectors.strip-data-test-properties'; +}; + +module.exports = StripDataTestPropertiesPlugin;