Skip to content

Commit

Permalink
feat: babel 7 support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Jan 27, 2018
1 parent 8a05b2c commit 830465b
Show file tree
Hide file tree
Showing 13 changed files with 965 additions and 1,269 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<a name="0.1.0"></a>
# 0.1.0 (2017-11-29)

# 0.1.0 (2017-11-29)

### Features

Expand Down
24 changes: 0 additions & 24 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,6 @@
We use [yarn](https://yarnpkg.org) as our package manager. Run `yarn` to install
the dependencies.

## Note: Current babel version situation

At the moment, we need both babel 6 (peer/dev) and babel 7 beta (dev) installed
in the project directory.

Babel 6 is used by production code and tests, while babel 7 is used to compile
the production code and tests. The former cannot be upgraded because
`babel-plugin-espower` does not yet support babel 7, the latter cannot be
downgraded because we need `@babel/preset-typescript`, which is 7+ only.

Due to babel's change from regular to scoped packages, this is generally not a
problem, because the production code and tests can just import the regular
packages with version 6, while our `.babelrc` uses the scoped packages with
version 7.

However, `babel-jest` currently requires `babel-core@7.0.0-bridge.0` to be
installed in order to work with babel 7, so for `babel-core` we use yarn's alias
feature to install `babel-core@^6.0.0` as `babel-core-old` and use that module
name for imports in our production code and tests, while the bridge package for
Jest is installed under its regular name.

This workaround also makes yarn absolutely mandatory for hacking on this package
at the moment.

## Scripts

* `yarn all` - run all checks and build
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ Babel (e.g. for
Once Babel is set up for your test files, simply add `"babel-plugin-spock"` to
the `plugins` array in your babel configuration and you're good to go.

**Note:** The Babel 7 beta is not yet supported, but may work if you disable
power assertions (see [Configuration](#Configuration)).
**Note:** The last version of this plugin that supports Babel 6 is `0.1.0`

## Usage

### Assertion blocks

**Note:** At the moment, your test files need to `import assert from
'power-assert';` for this to work. In the future, the plugin will be able to
handle this for you automatically.
**Note:** At the moment, your test files need to `import assert from 'power-assert';`
for this to work. In the future, the plugin will be able to handle this for you automatically.

Inside of a block labeled with `expect:` or `then:`, all statements will be
considered assertions and evaluated to check for truth:
Expand Down
30 changes: 14 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,29 @@
"trailingComma": "all"
},
"dependencies": {
"babel-plugin-espower": "2.3.2"
"babel-plugin-espower": "3.0.0-beta.1"
},
"peerDependencies": {
"babel-core": "^6.0.0",
"babel-core": "^7.0.0-beta",
"power-assert": "^1.4.4"
},
"devDependencies": {
"@babel/cli": "7.0.0-beta.32",
"@babel/core": "7.0.0-beta.32",
"@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.32",
"@babel/preset-env": "7.0.0-beta.32",
"@babel/preset-typescript": "7.0.0-beta.32",
"@babel/cli": "7.0.0-beta.38",
"@babel/core": "7.0.0-beta.38",
"@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.38",
"@babel/preset-env": "7.0.0-beta.38",
"@babel/preset-react": "7.0.0-beta.38",
"@babel/preset-typescript": "7.0.0-beta.38",
"@babel/types": "7.0.0-beta.38",
"@types/babel-core": "^6.25.3",
"@types/babel-traverse": "^6.25.2",
"@types/babel-types": "^6.25.1",
"@types/jest": "^21.1.8",
"@types/node": "^8.0.53",
"@types/babel-types": "^7.0.0",
"@types/jest": "^22.0.0",
"@types/node": "^9.3.0",
"babel-core": "7.0.0-bridge.0",
"babel-core-old": "npm:babel-core@^6.0.0",
"babel-jest": "21.3.0-beta.10",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-types": "^6.26.0",
"babel-jest": "^22.0.4",
"copyfiles": "^1.2.0",
"jest": "^21.2.1",
"jest": "^22.0.4",
"live-server": "^1.2.0",
"npm-run-all": "^4.0.2",
"power-assert": "^1.4.4",
Expand Down
4 changes: 2 additions & 2 deletions src/assertify-statement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NodePath } from 'babel-traverse';
import * as BabelTypes from 'babel-types';
import { NodePath } from '@babel/traverse';
import * as BabelTypes from '@babel/types';

export default (t: typeof BabelTypes) => (
statementPath: NodePath<BabelTypes.Statement>,
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PluginObj } from 'babel-core-old';
import { PluginObj } from '@babel/core';
import * as BabelTypes from '@babel/types';
import createEspowerVisitor from 'babel-plugin-espower/create';
import { NodePath } from 'babel-traverse';
import * as BabelTypes from 'babel-types';

import assertifyStatement from './assertify-statement';
import { extractConfigFromState } from './config';
Expand Down
18 changes: 6 additions & 12 deletions test/__snapshots__/power-assert.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`leaves unrelated assert statements untouched 1`] = `
"'use strict';
"const assert = require('power-assert');
var _powerAssert = require('power-assert');
var _powerAssert2 = _interopRequireDefault(_powerAssert);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
(0, _powerAssert2.default)(1 === 2);"
assert(1 === 2);"
`;

exports[`prints a nice error for an "expected"-labeled expression statement 1`] = `
" # unknown:2
" # test.js:2
assert(1 === 2)
|
Expand All @@ -27,7 +21,7 @@ exports[`prints a nice error for an "expected"-labeled expression statement 1`]
`;

exports[`still works if babel-plugin-espower is used for other assertions in the file 1`] = `
" # unknown:3
" # test.js:3
assert(x > 0)
| |
Expand All @@ -36,7 +30,7 @@ exports[`still works if babel-plugin-espower is used for other assertions in the
`;

exports[`still works if babel-plugin-espower is used for other assertions in the file 2`] = `
" # unknown:2
" # test.js:2
assert(x >= 0)
| |
Expand All @@ -46,7 +40,7 @@ exports[`still works if babel-plugin-espower is used for other assertions in the
`;

exports[`supports non-standard JSX syntax 1`] = `
" # unknown:2
" # test.js:2
assert(<div></div>.prop === 'expected')
| |
Expand Down
15 changes: 7 additions & 8 deletions test/__snapshots__/simple-assertion.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ exports[`assertifies a "then"-labeled expression statement 1`] = `"then: assert(

exports[`assertifies all expression statements in a labeled block statement 1`] = `
"expect: {
assert(1 === 1);
assert(0.5 * 4 < 3);
assert(abc.xyz() === 'result');
assert(((x, y) => x === y)(42, 42));
assert(1 === 1);
assert(0.5 * 4 < 3);
assert(abc.xyz() === 'result');
assert(((x, y) => x === y)(42, 42));
}"
`;

Expand All @@ -23,9 +23,8 @@ exports[`assertifies inside an if / else statement 1`] = `

exports[`does not assertify a "when"-labeled expression statement 1`] = `"when: 1 === 1;"`;

exports[`throws if a statement is not an expression statement 1`] = `"unknown: Expected an expression statement, but got a statement of type FunctionDeclaration"`;

exports[`throws if a statement is not an expression statement 2`] = `
"> 1 | expect: function f() {}
exports[`throws if a statement is not an expression statement 1`] = `
"undefined: Expected an expression statement, but got a statement of type IfStatement
> 1 | expect: if(true);
| ^"
`;
23 changes: 12 additions & 11 deletions test/power-assert.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { transform } from 'babel-core-old';
import { transform } from '@babel/core';

import plugin from '../src';

test('prints a nice error for an "expected"-labeled expression statement', () => {
const { code } = transform(
`import assert from 'power-assert';
`const assert = require('power-assert');
expect: 1 === 2;`,
{
plugins: [plugin],
presets: ['env'],
filename: 'test.js',
},
);

Expand All @@ -19,11 +19,11 @@ test('prints a nice error for an "expected"-labeled expression statement', () =>

test('passes a truthy expression', () => {
const { code } = transform(
`import assert from 'power-assert';
`const assert = require('power-assert');
expect: 2 === 2;`,
{
plugins: [plugin],
presets: ['env'],
filename: 'test.js',
},
);

Expand All @@ -32,11 +32,11 @@ test('passes a truthy expression', () => {

test('leaves unrelated assert statements untouched', () => {
const { code } = transform(
`import assert from 'power-assert';
`const assert = require('power-assert');
assert(1 === 2);`,
{
plugins: [plugin],
presets: ['env'],
filename: 'test.js',
},
);

Expand All @@ -45,12 +45,12 @@ test('leaves unrelated assert statements untouched', () => {

test('still works if babel-plugin-espower is used for other assertions in the file', () => {
const { code } = transform(
`import assert from 'power-assert';
`const assert = require('power-assert');
assert(x >= 0);
expect: x > 0;`,
{
plugins: [plugin, 'espower'],
presets: ['env'],
filename: 'test.js',
},
);

Expand All @@ -71,11 +71,12 @@ test('supports non-standard JSX syntax', () => {
.mockImplementationOnce(tagName => ({ prop: tagName }));

const { code } = transform(
`import assert from 'power-assert';
`const assert = require('power-assert');
expect: (<div></div>).prop === 'expected';`,
{
plugins: [plugin],
presets: ['env', 'react'],
presets: ['@babel/preset-react'],
filename: 'test.js',
},
);

Expand Down
14 changes: 5 additions & 9 deletions test/simple-assertion.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from 'babel-core-old';
import { transform } from '@babel/core';

import plugin from '../src';
import { Config } from '../src/config';
Expand Down Expand Up @@ -51,14 +51,10 @@ test('assertifies inside an if / else statement', () => {
});

test('throws if a statement is not an expression statement', () => {
expect.assertions(2);
try {
transform(`expect: function f() {}`, {
expect(() =>
transform(`expect: if(true);`, {
plugins: [[plugin, { powerAssert: false } as Config]],
highlightCode: false,
});
} catch (err) {
expect(err.message).toMatchSnapshot();
expect(err.codeFrame).toMatchSnapshot();
}
}),
).toThrowErrorMatchingSnapshot();
});
3 changes: 0 additions & 3 deletions typings/babel-core-old.d.ts

This file was deleted.

11 changes: 11 additions & 0 deletions typings/babel.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare module '@babel/core' {
export * from 'babel-core';
}

declare module '@babel/types' {
export * from 'babel-types';
}

declare module '@babel/traverse' {
export * from 'babel-traverse';
}

0 comments on commit 830465b

Please sign in to comment.