From 8d4235bd20bd60f7a0c3218fac80bab6341a6618 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 27 Mar 2017 11:13:33 -0700 Subject: [PATCH 1/3] Use "multidep" to install "babel-core" --- .gitignore | 1 + node-tests/multidep.json | 6 ++++++ node-tests/strip-data-test-properties-plugin-test.js | 9 +++++---- package.json | 4 +++- 4 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 node-tests/multidep.json 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/node-tests/multidep.json b/node-tests/multidep.json new file mode 100644 index 00000000..0e8d835c --- /dev/null +++ b/node-tests/multidep.json @@ -0,0 +1,6 @@ +{ + "path": "node-tests/multidep_modules", + "versions": { + "babel-core": ["5.8.33"] + } +} diff --git a/node-tests/strip-data-test-properties-plugin-test.js b/node-tests/strip-data-test-properties-plugin-test.js index 20eac368..5e31af07 100644 --- a/node-tests/strip-data-test-properties-plugin-test.js +++ b/node-tests/strip-data-test-properties-plugin-test.js @@ -1,8 +1,9 @@ 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 StripDataTestPropertiesPlugin5 = require('../strip-data-test-properties-plugin'); function testFixture(name) { it('fixture: ' + name, function() { @@ -10,8 +11,8 @@ function testFixture(name) { 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()); diff --git a/package.json b/package.json index ef3bbca2..8465a2ca 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "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" }, @@ -48,7 +49,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" From 4dd26090a3bf81d01ab3eb7300c1760ff24f991d Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 27 Mar 2017 11:45:30 -0700 Subject: [PATCH 2/3] Port "strip-data-test-properties-plugin" to Babel 6 --- node-tests/fixtures/default/expected6.js | 6 +++++ node-tests/multidep.json | 2 +- .../strip-data-test-properties-plugin-test.js | 18 ++++++++++++- strip-data-test-properties-plugin6.js | 27 +++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 node-tests/fixtures/default/expected6.js create mode 100644 strip-data-test-properties-plugin6.js 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 index 0e8d835c..819af88d 100644 --- a/node-tests/multidep.json +++ b/node-tests/multidep.json @@ -1,6 +1,6 @@ { "path": "node-tests/multidep_modules", "versions": { - "babel-core": ["5.8.33"] + "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 5e31af07..295d67ff 100644 --- a/node-tests/strip-data-test-properties-plugin-test.js +++ b/node-tests/strip-data-test-properties-plugin-test.js @@ -3,10 +3,13 @@ var assert = require('assert'); var multidepRequire = require('multidep')('node-tests/multidep.json'); 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'; @@ -17,6 +20,19 @@ function testFixture(name) { 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()); + }); } describe('StripDataTestProperties plugin', function() { 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; From 6406c08113b16434614f9e31a0028d33d29fb69e Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 27 Mar 2017 12:08:56 -0700 Subject: [PATCH 3/3] Conditionally add Babel 5 or Babel 6 plugin to the host app --- index.js | 13 ++++++++++++- package.json | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) 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/package.json b/package.json index 8465a2ca..6f71c5bd 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "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",