Skip to content

Commit

Permalink
fix(power-assert): make power assertions work when using autoImport
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Mar 18, 2018
1 parent 6f6d611 commit f2fbf43
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
30 changes: 23 additions & 7 deletions src/assertify-statement.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { NodePath } from '@babel/traverse';
import * as BabelTypes from '@babel/types';
import createEspowerVisitor from 'babel-plugin-espower/create';

import { InternalConfig } from './config';
import generateAssertIdentifier from './generate-assert-identifier';

export default (t: typeof BabelTypes, config: InternalConfig) => (
statementPath: NodePath<BabelTypes.Statement>,
) => {
const statement = statementPath.node;
export default (
babel: { types: typeof BabelTypes },
state: any,
config: InternalConfig,
) => (statementPath: NodePath<BabelTypes.Statement>) => {
const { types: t } = babel;
const { scope, node: statement } = statementPath;

if (t.isExpressionStatement(statement)) {
const origExpr = statement.expression;

const assertIdentifier = generateAssertIdentifier(t, config)(
statementPath.scope,
);
const assertIdentifier = generateAssertIdentifier(t, config)(scope);
assertIdentifier.loc = {
start: origExpr.loc.start,
end: origExpr.loc.start,
Expand All @@ -22,6 +25,19 @@ export default (t: typeof BabelTypes, config: InternalConfig) => (
const newExpr = t.callExpression(assertIdentifier, [origExpr]);
newExpr.loc = origExpr.loc;
statement.expression = newExpr;

// register added import with scope so it is found for the next assertion
// for some reason, this needs to happen after we have referenced the import
// with our call expression, otherwise the import will be removed
(scope as any).crawl();

if (config.powerAssert) {
// Now let espower generate nice power assertions for this assertion
createEspowerVisitor(babel, {
embedAst: true,
patterns: [`${assertIdentifier.name}(value)`],
}).visitor.Program(statementPath, state);
}
} else {
throw statementPath.buildCodeFrameError(
`Expected an expression statement, but got a statement of type ${
Expand Down
3 changes: 1 addition & 2 deletions src/generate-assert-identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ const addImport = (scope: Scope, t: typeof BabelTypes, source: string) => {
t.stringLiteral(source),
),
);
// recrawl scope so the added import is found for the next assertion
(scope as any).crawl();

return id;
};

Expand Down
13 changes: 1 addition & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PluginObj } from '@babel/core';
import * as BabelTypes from '@babel/types';
import createEspowerVisitor from 'babel-plugin-espower/create';
import { NodePath } from 'babel-traverse';

import assertifyStatement from './assertify-statement';
Expand All @@ -9,16 +8,11 @@ import { extractConfigFromState } from './config';
const assertionBlockLabels = ['expect', 'then'];

const plugin = (babel: { types: typeof BabelTypes }): PluginObj => {
const espowerVisitor = createEspowerVisitor(babel, {
embedAst: true,
patterns: ['assert(value)'],
});

return {
visitor: {
LabeledStatement(path, state) {
const config = extractConfigFromState(state);
const assertify = assertifyStatement(babel.types, config);
const assertify = assertifyStatement(babel, state, config);

if (assertionBlockLabels.includes(path.node.label.name)) {
const bodyPath = path.get('body') as NodePath<BabelTypes.Statement>;
Expand All @@ -32,11 +26,6 @@ const plugin = (babel: { types: typeof BabelTypes }): PluginObj => {
default:
assertify(bodyPath);
}

if (config.powerAssert) {
// Now let espower generate nice power assertions for this labeled statement
espowerVisitor.visitor.Program(path, state);
}
}
},
},
Expand Down
14 changes: 14 additions & 0 deletions test/__snapshots__/power-assert.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,17 @@ exports[`supports non-standard JSX syntax 1`] = `
"
`;

exports[`works when using autoImport 1`] = `
" # test.js:1
_assert(1 === 2)
|
false
[number] 2
=> 2
[number] 1
=> 1
"
`;
12 changes: 12 additions & 0 deletions test/power-assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ test('supports non-standard JSX syntax', () => {
}),
).toThrowErrorMatchingSnapshot();
});

test('works when using autoImport', () => {
const { code } = transform(`expect: 1 === 2;`, {
plugins: [plugin],
presets: ['@babel/preset-env'],
filename: 'test.js',
});

expect(() =>
new Function('require', code as string)(require),
).toThrowErrorMatchingSnapshot();
});

0 comments on commit f2fbf43

Please sign in to comment.