Skip to content

Commit

Permalink
Merge pull request #11 from salsify/babel-7-support
Browse files Browse the repository at this point in the history
Support Babel 7
  • Loading branch information
dfreeman authored Sep 5, 2018
2 parents 84b22d9 + 6f16298 commit 0654f97
Show file tree
Hide file tree
Showing 4 changed files with 1,099 additions and 356 deletions.
22 changes: 15 additions & 7 deletions lib/babel-plugin-strip-milestones.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,22 @@ module.exports = (ui) => {
}

function issueWarning(path, pass, message) {
try {
// file.wrap gives the thrown error its `codeFrame` property
pass.file.wrap(pass.file.code, () => {
throw path.buildCodeFrameError(message);
});
} catch (error) {
ui.writeWarnLine(`${error.message}\n${error.codeFrame}\n`); // eslint-disable-line no-console
let errorWithCodeFrame;
if (typeof pass.file.wrap === 'function') {
// In Babel 6, file.wrap gives the thrown error its `codeFrame` property
try {
pass.file.wrap(pass.file.code, () => {
throw path.buildCodeFrameError(message);
});
} catch (error) {
errorWithCodeFrame = `${error.message}\n${error.codeFrame}\n`;
}
} else {
// In Babel 7, we can just build the error directly
errorWithCodeFrame = path.buildCodeFrameError(message).message;
}

ui.writeWarnLine(errorWithCodeFrame);
}

function removeMilestoneCalls(importSpecifier, scope, pass) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
"test:all": "ember try:each"
},
"dependencies": {
"ember-cli-babel": "^6.12.0",
"ember-cli-babel": "^7.1.0",
"ember-debug-logger": "^1.1.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@types/ember": "^2.8.22",
"@types/ember-qunit": "^3.0.2",
"@types/ember-test-helpers": "^0.7.1",
Expand Down
219 changes: 114 additions & 105 deletions tests-node/unit/babel-plugin-strip-milestones-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,130 @@ const QUnit = require('qunit'), test = QUnit.test;
const stripIndent = require('common-tags').stripIndent;
const sinon = require('sinon');
const plugin = require('../../lib/babel-plugin-strip-milestones');
const babel = require('babel-core');

QUnit.module('Unit | babel-plugin-strip-milestones', (hooks) => {
let ui = {};
hooks.beforeEach(() => {
ui.writeWarnLine = sinon.spy();
});

function transform(input) {
return babel.transform(input, {
filename: 'input.js',
highlightCode: false,
plugins: [plugin(ui)]
});
}

test('stripping milestones with callbacks', (assert) => {
let source = stripIndent`
import { milestone } from 'ember-milestones';
async function foo() {
let result = await milestone('bar', () => 'hello');
console.log(result);
}
`;

let expected = stripIndent`
async function foo() {
let result = await (() => 'hello')();
console.log(result);
}
`;

assert.equal(transform(source).code.trim(), expected.trim());
assert.ok(ui.writeWarnLine.notCalled);
QUnit.module('Babel 6', function() {
testPlugin(require('babel-core'));
});

test('stripping milestones without callbacks', (assert) => {
let source = stripIndent`
import { milestone } from 'ember-milestones';
async function foo() {
let result = await milestone('bar');
console.log(result);
}
`;

let expected = stripIndent`
async function foo() {
let result = await undefined;
console.log(result);
}
`;

assert.equal(transform(source).code.trim(), expected.trim());
assert.ok(ui.writeWarnLine.notCalled);
QUnit.module('Babel 7', function() {
testPlugin(require('@babel/core'));
});

test('warning about unremovable imports', (assert) => {
let source = stripIndent`
import { milestone, advanceTo } from 'ember-milestones';
async function foo() {
let result = await milestone('bar');
console.log(result);
}
advanceTo('bar');
`;

let expected = stripIndent`
import { milestone, advanceTo } from 'ember-milestones';
async function foo() {
let result = await undefined;
console.log(result);
}
advanceTo('bar');
`;

assert.equal(transform(source).code.trim(), expected.trim());
assert.equal(ui.writeWarnLine.lastCall.args[0].trim(), stripIndent`
input.js: Unable to safely remove an import referencing more than just \`milestone\`.
> 1 | import { milestone, advanceTo } from 'ember-milestones';
| ^
2 |\x20
3 | async function foo() {
4 | let result = await milestone('bar');
`);
});
function testPlugin(babel) {
function transform(input) {
return babel.transform(input, {
filename: '/input.js',
highlightCode: false,
plugins: [plugin(ui)]
});
}

test('stripping milestones with callbacks', (assert) => {
let source = stripIndent`
import { milestone } from 'ember-milestones';
async function foo() {
let result = await milestone('bar', () => 'hello');
console.log(result);
}
`;

let expected = stripIndent`
async function foo() {
let result = await (() => 'hello')();
console.log(result);
}
`;

assert.equal(transform(source).code.trim(), expected.trim());
assert.ok(ui.writeWarnLine.notCalled);
});

test('warning about unremovable milestones', (assert) => {
let source = stripIndent`
import { milestone } from 'ember-milestones';
async function foo() {
console.log(milestone);
}
`;

let expected = stripIndent`
async function foo() {
console.log(milestone);
}
`;

assert.equal(transform(source).code.trim(), expected.trim());
assert.equal(ui.writeWarnLine.lastCall.args[0].trim(), stripIndent`
input.js: Unable to safely strip invalid \`milestone\` usage.
2 |\x20
3 | async function foo() {
> 4 | console.log(milestone);
| ^
5 | }
`);
});
test('stripping milestones without callbacks', (assert) => {
let source = stripIndent`
import { milestone } from 'ember-milestones';
async function foo() {
let result = await milestone('bar');
console.log(result);
}
`;

let expected = stripIndent`
async function foo() {
let result = await undefined;
console.log(result);
}
`;

assert.equal(transform(source).code.trim(), expected.trim());
assert.ok(ui.writeWarnLine.notCalled);
});

test('warning about unremovable imports', (assert) => {
let source = stripIndent`
import { milestone, advanceTo } from 'ember-milestones';
async function foo() {
let result = await milestone('bar');
console.log(result);
}
advanceTo('bar');
`;

let expected = stripIndent`
import { milestone, advanceTo } from 'ember-milestones';
async function foo() {
let result = await undefined;
console.log(result);
}
advanceTo('bar');
`;

assert.equal(transform(source).code.trim(), expected.trim());
assert.equal(ui.writeWarnLine.lastCall.args[0].trim(), stripIndent`
/input.js: Unable to safely remove an import referencing more than just \`milestone\`.
> 1 | import { milestone, advanceTo } from 'ember-milestones';
| ^
2 |\x20
3 | async function foo() {
4 | let result = await milestone('bar');
`);
});

test('warning about unremovable milestones', (assert) => {
let source = stripIndent`
import { milestone } from 'ember-milestones';
async function foo() {
console.log(milestone);
}
`;

let expected = stripIndent`
async function foo() {
console.log(milestone);
}
`;

assert.equal(transform(source).code.trim(), expected.trim());
assert.equal(ui.writeWarnLine.lastCall.args[0].trim(), stripIndent`
/input.js: Unable to safely strip invalid \`milestone\` usage.
2 |\x20
3 | async function foo() {
> 4 | console.log(milestone);
| ^
5 | }
`);
});
}
});
Loading

0 comments on commit 0654f97

Please sign in to comment.