Skip to content

Commit

Permalink
feat: add assertFunctionName option
Browse files Browse the repository at this point in the history
  • Loading branch information
iyzana committed Apr 3, 2018
1 parent 2254796 commit 448b277
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface InternalConfig {
powerAssert: boolean;
autoImport: string; // empty => no auto import
staticTruthCheck: boolean;
assertFunctionName: string; // empty => no custom assert function name
}

/**
Expand All @@ -15,6 +16,7 @@ export interface Config {
powerAssert?: boolean;
autoImport?: boolean | string; // false => '', true => 'power-assert'
staticTruthCheck?: boolean;
assertFunctionName?: string;
}

/**
Expand All @@ -24,6 +26,7 @@ export const defaultConfig = {
powerAssert: true,
autoImport: true,
staticTruthCheck: false,
assertFunctionName: '',
};

/**
Expand All @@ -36,7 +39,7 @@ export const minimalConfig = {

// tslint:disable no-parameter-reassignment
export const extractConfigFromState = ({
opts: { powerAssert, autoImport, staticTruthCheck },
opts: { powerAssert, autoImport, staticTruthCheck, assertFunctionName },
}: {
opts: Config;
}): InternalConfig => {
Expand All @@ -61,9 +64,15 @@ export const extractConfigFromState = ({
({ staticTruthCheck } = defaultConfig);
}

// assertFunctionName
if (assertFunctionName === undefined) {
({ assertFunctionName } = defaultConfig);
}

return {
powerAssert,
autoImport,
staticTruthCheck,
assertFunctionName,
};
};
19 changes: 13 additions & 6 deletions src/generate-assert-identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ const findExistingImportFromSource = (
return;
};

const addImport = (scope: Scope, t: typeof BabelTypes, source: string) => {
const addImport = (
scope: Scope,
t: typeof BabelTypes,
source: string,
name: string,
) => {
const program = scope.getProgramParent().path;

// generate default import from source
const id = scope.generateUidIdentifier(ASSERT_IDENTIFIER_NAME);
// generate default or specified import from source
const id = name
? t.identifier(name)
: scope.generateUidIdentifier(ASSERT_IDENTIFIER_NAME);
(program as any).unshiftContainer(
'body',
t.importDeclaration(
Expand All @@ -49,16 +56,16 @@ const addImport = (scope: Scope, t: typeof BabelTypes, source: string) => {

export default (
t: typeof BabelTypes,
{ autoImport: importSource }: InternalConfig,
{ autoImport: importSource, assertFunctionName }: InternalConfig,
) => (scope: Scope) => {
if (importSource) {
const name = findExistingImportFromSource(scope, t, importSource);
if (name) {
return t.identifier(name);
}

return addImport(scope, t, importSource);
return addImport(scope, t, importSource, assertFunctionName);
}

return t.identifier(ASSERT_IDENTIFIER_NAME);
return t.identifier(assertFunctionName || ASSERT_IDENTIFIER_NAME);
};
9 changes: 9 additions & 0 deletions test/__snapshots__/assert-function-name.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generates import with specified name when autoImport is set 1`] = `
"import check from \\"power-assert\\";
expect: check(1 === 1);"
`;

exports[`generates specified assert function 1`] = `"expect: check(1 === 1);"`;
29 changes: 29 additions & 0 deletions test/assert-function-name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { transform } from '@babel/core';

import plugin from '../src';
import { Config, minimalConfig } from '../src/config';

test('generates specified assert function', () => {
const { code } = transform(`expect: 1 === 1;`, {
plugins: [
[plugin, { ...minimalConfig, assertFunctionName: 'check' } as Config],
],
});
expect(code).toMatchSnapshot();
});

test('generates import with specified name when autoImport is set', () => {
const { code } = transform(`expect: 1 === 1;`, {
plugins: [
[
plugin,
{
...minimalConfig,
assertFunctionName: 'check',
autoImport: true,
} as Config,
],
],
});
expect(code).toMatchSnapshot();
});

0 comments on commit 448b277

Please sign in to comment.