Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Sep 14, 2017
1 parent 1db72d7 commit 3857d55
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 142 deletions.
42 changes: 42 additions & 0 deletions .yarnclean
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# test directories
__tests__
test
tests
powered-test

# asset directories
docs
doc
website
images
assets

# examples
example
examples

# code coverage directories
coverage
.nyc_output

# build scripts
Makefile
Gulpfile.js
Gruntfile.js

# configs
.tern-project
.gitattributes
.editorconfig
.*ignore
.eslintrc
.jshintrc
.flowconfig
.documentup.json
.yarn-metadata.json
.*.yml
*.yml

# misc
*.gz
*.md
14 changes: 12 additions & 2 deletions lib/transforms/babel-plugin-remove-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,30 @@ function PluginRemoveFilteredImports() {
var name = path.node.source.value;

if (filteredImportNames.indexOf(name) !== -1) {
if (filteredImports[name] === true) {
if (filteredImports[name] === true || filteredImports[name] === '*') {
importDeclarationsToRemove.push(path);
} else {
let removables = [];
let imports = path.node.specifiers;
const hasSpecifiers = imports.length > 0;

for (let i = 0; i < imports.length; i++) {
if (imports[i].type === 'ImportNamespaceSpecifier') {
continue;
}

let specifier = imports[i].imported;

if (!specifier) {
debugger;
}

if (filteredImports[name].indexOf(specifier.name) !== -1) {
removables.push(imports[i]);
}
}

if (removables.length === imports.length) {
if (hasSpecifiers && removables.length === imports.length) {
importDeclarationsToRemove.push(path);
} else {
for (let i = 0; i < removables.length; i++) {
Expand Down
87 changes: 70 additions & 17 deletions node-tests/unit/babel-plugin-remove-imports-test.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,110 @@
const BroccoliTestHelper = require('broccoli-test-helper');
const co = require('co');
const Babel = require('broccoli-babel-transpiler');
const chai = require('ember-cli-blueprint-test-helpers/chai');
const babel = require('babel-core');
const stripIndent = require('common-tags').stripIndent;
const StripFilteredImports = require('../../lib/transforms/babel-plugin-remove-imports');

const { expect } = chai;
const createTempDir = BroccoliTestHelper.createTempDir;
const createBuilder = BroccoliTestHelper.createBuilder;

function stripNewlines(str) {
return str.replace(/[\r\n]/g, '');
}

describe('Unit: babel-plugin-remove-filtered-imports', function() {
let plugins, pluginOptions;
let input, output;

function transform(code) {
return babel.transform(code, {
plugins
}).code.trim();
return co.wrap(function* () {
input.write({ 'test.js': code });
let babel = new Babel(input.path(), {
plugins
});

output = createBuilder(babel);

yield output.build();
})();
}

function getOutput() {
const transpiled = output.read();

return stripNewlines(transpiled['test.js']);
}

beforeEach(function() {
beforeEach(co.wrap(function* () {
pluginOptions = {};

plugins = [
[StripFilteredImports, pluginOptions]
];
});

input = yield createTempDir();
}));

afterEach(co.wrap(function* () {
if (input) {
yield input.dispose();
}
if (output) {
yield output.dispose();
}

input = output = undefined;
}));

it('Returns a plugin', function() {
let plugin = StripFilteredImports();
expect(plugin).to.be.ok;
});

it('Does not alter a file if no imports are meant to be filtered', function() {
it('Does not alter a file if no imports are meant to be filtered', co.wrap(function*() {
let input = stripIndent`
import Foo from 'bar';
import { baz } from 'none';
import * as drinks from 'drinks';
import 'bem';
`;

let output = transform(input);
yield transform(input);

expect(output).to.equal(input);
});
expect(getOutput()).to.equal(stripNewlines(input));
}));

it('Strips ', function() {
it('Properly strips desired imports and specifiers', co.wrap(function*() {
let input = stripIndent`
import Foo from 'bar';
import { baz } from 'none';
import { bit } from 'wow';
import { baz, bell } from 'none';
import { foo } from 'happy';
import * as drinks from 'drinks';
import * as dranks from 'dranks';
import 'bem';
import 'bosh';
import 'bell';
`;

pluginOptions.none = ['baz'];
pluginOptions.bar = '*';
pluginOptions.bar = true;
pluginOptions.drinks = '*';
pluginOptions.wow = ['bit'];
pluginOptions.bem = ['biz'];
pluginOptions.bosh = '*';
pluginOptions.dranks = ['bex'];
pluginOptions.bell = true;

let output = transform(input);
let expectedOutput = '';
yield transform(input);
let expectedOutput = stripNewlines(stripIndent`
import { bell } from 'none';
import { foo } from 'happy';
import * as dranks from 'dranks';
import 'bem';
`);

expect(output).to.equal(expectedOutput);
});
expect(getOutput()).to.equal(expectedOutput);
}));
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"license": "MIT",
"dependencies": {
"amd-name-resolver": "0.0.7",
"babel-core": "^6.26.0",
"babel-plugin-feature-flags": "^0.3.1",
"babel-plugin-filter-imports": "^0.3.1",
"babel-plugin-transform-es2015-block-scoping": "^6.24.1",
Expand All @@ -32,8 +31,10 @@
"broccoli-funnel": "^1.2.0",
"broccoli-merge-trees": "^2.0.0",
"broccoli-rollup": "^1.2.0",
"broccoli-test-helper": "^1.2.0",
"calculate-cache-key-for-tree": "^1.1.0",
"chalk": "^1.1.1",
"co": "^4.6.0",
"common-tags": "^1.4.0",
"ember-cli-babel": "^6.4.1",
"ember-cli-path-utils": "^1.0.0",
Expand Down
Loading

0 comments on commit 3857d55

Please sign in to comment.