Skip to content

Commit

Permalink
fix: extract imports from dt files
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Aug 5, 2020
1 parent c6820b9 commit ce239d0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
17 changes: 10 additions & 7 deletions core/instrument/src/babel/extract-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { sourceLocation } from '../misc/source-location';
export interface ExportType {
name: string;
internalName: string;
loc: CodeLocation;
loc?: CodeLocation;
/**
* in case of export { Button } from './button-named-export';
* specifies the import from statememnt
Expand Down Expand Up @@ -34,20 +34,23 @@ export const traverseExports = (results: ExportTypes) => {
path: any,
declaration: any,
): ExportType | undefined => {
if (declaration.init && declaration.id.name) {
if (declaration.id && declaration.id.name) {
const name = declaration.id.name;
const exportType: ExportType = {
node: declaration,
path,
loc: sourceLocation(
name,
internalName: name,
};
if (declaration.init) {
exportType.loc = sourceLocation(
declaration.init.body &&
declaration.init.type !== 'ArrowFunctionExpression'
? declaration.init.body.loc
: declaration.init.loc,
),
name,
internalName: name,
};
);
}

return exportType;
}
return undefined;
Expand Down
40 changes: 40 additions & 0 deletions core/instrument/test/__snapshots__/extract-exports.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,43 @@ Object {
},
}
`;

exports[`extract-exports theme-ui-style.ts 1`] = `
Object {
"default": undefined,
"named": Object {
"BoxOwnProps": Object {
"internalName": "BoxOwnProps",
"loc": Object {
"end": Object {
"column": 1,
"line": 21,
},
"start": Object {
"column": 60,
"line": 17,
},
},
"name": "BoxOwnProps",
},
"Button": Object {
"internalName": "Button",
"name": "Button",
},
"ButtonProps": Object {
"internalName": "ButtonProps",
"loc": Object {
"end": Object {
"column": 71,
"line": 24,
},
"start": Object {
"column": 69,
"line": 24,
},
},
"name": "ButtonProps",
},
},
}
`;
29 changes: 29 additions & 0 deletions core/instrument/test/fixtures/exports/theme-ui-style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as React from 'react';
import { InterpolationWithTheme } from '@emotion/core';
import { SpaceProps, ColorProps } from 'styled-system';

type Assign<T, U> = {
[P in keyof (T & U)]: P extends keyof T
? T[P]
: P extends keyof U
? U[P]
: never;
};

type ForwardRef<T, P> = React.ForwardRefExoticComponent<
React.PropsWithoutRef<P> & React.RefAttributes<T>
>;

export interface BoxOwnProps extends SpaceProps, ColorProps {
as?: React.ElementType;
variant?: string;
css?: InterpolationWithTheme<any>;
}

export interface ButtonProps
extends Assign<React.ComponentPropsWithRef<'button'>, BoxOwnProps> {}
/**
* Primitive button component with variants
* @see https://theme-ui.com/components/button
*/
export const Button: ForwardRef<HTMLButtonElement, ButtonProps>;

0 comments on commit ce239d0

Please sign in to comment.