Skip to content

Commit eb0d4b6

Browse files
author
Jack Ellis
committed
feat: add support for react-jpex encase method
1 parent a0ad469 commit eb0d4b6

14 files changed

Lines changed: 185 additions & 28 deletions

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
"@typescript-eslint/parser": "^4.2.0",
5555
"ava": "^3.12.1",
5656
"eslint": "^7.7.0",
57-
"jpex": "^4.0.0",
5857
"rollup": "^2.26.6",
5958
"rollup-plugin-babel": "^4.4.0",
6059
"rollup-plugin-node-resolve": "^5.2.0",

rollup.config.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import babel from 'rollup-plugin-babel';
22
import localResolve from 'rollup-plugin-node-resolve';
33

4-
export default {
4+
const src = {
55
input: 'src/index.ts',
66
output: {
77
file: 'dist/index.js',
@@ -23,3 +23,16 @@ export default {
2323
'path',
2424
],
2525
};
26+
27+
const helpers = {
28+
input: 'src/common/index.ts',
29+
output: {
30+
file: 'dist/helpers.js',
31+
format: 'cjs',
32+
exports: 'named',
33+
},
34+
plugins: src.plugins,
35+
external: src.external,
36+
};
37+
38+
export default [ src, helpers ];

src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { resolve } from 'path';
1+
import { NodePath, Visitor } from '@babel/core';
22
import { declare } from '@babel/helper-plugin-utils';
3-
import handleFactoryCalls from './factories';
4-
import handleResolveCall from './resolve';
5-
import handleResolveWithCall from './resolveWith';
3+
import { resolve } from 'path';
64
import handleAliasCall from './alias';
5+
import handleClearCache from './clearCache';
76
import handleEncaseCall from './encase';
7+
import handleFactoryCalls from './factories';
88
import handleInferCall from './infer';
99
import handleRawCall from './raw';
10-
import handleUseResolve from './useResolve';
11-
import handleClearCache from './clearCache';
12-
import { Visitor, NodePath } from '@babel/core';
10+
import handleResolveCall from './resolve';
11+
import handleResolveWithCall from './resolveWith';
12+
import reactJpex from './react-jpex';
1313

1414
declare const require: any;
1515
declare const process: any;
@@ -53,7 +53,7 @@ const mainVisitor: Visitor<{
5353
handleInferCall(programPath, path, opts);
5454
handleRawCall(programPath, path, opts);
5555
handleClearCache(programPath, path, opts);
56-
handleUseResolve(programPath, path, opts);
56+
reactJpex(programPath, path, opts);
5757
},
5858
};
5959

src/react-jpex/encase.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { NodePath, types as t, Visitor } from '@babel/core';
2+
import {
3+
extractFunctionParameterTypes,
4+
State,
5+
} from '../common';
6+
7+
const importVisitor: Visitor<{
8+
found: boolean,
9+
}> = {
10+
ImportSpecifier(path, state) {
11+
if (path.node.imported.name === 'encase') {
12+
if (path.node.local.name === 'encase') {
13+
// @ts-ignore
14+
if (path.parent.source.value === 'react-jpex') {
15+
state.found = true;
16+
}
17+
}
18+
}
19+
},
20+
};
21+
22+
const encase = (
23+
programPath: NodePath<t.Program>,
24+
path: NodePath<any>,
25+
{
26+
filename,
27+
publicPath,
28+
}: State,
29+
) => {
30+
const callee = path.node.callee;
31+
const args = path.node.arguments;
32+
33+
if (callee?.name !== 'encase') {
34+
return;
35+
}
36+
const state = { found: false };
37+
programPath.traverse(importVisitor, state);
38+
39+
if (!state.found) {
40+
return;
41+
}
42+
43+
if (args.length > 0 && t.isStringLiteral(args[0])) {
44+
return;
45+
}
46+
47+
const arg = path.get('arguments.0') as NodePath<any>;
48+
const deps = extractFunctionParameterTypes(programPath, arg, filename, publicPath);
49+
path.node.arguments.splice(0, 0, t.arrayExpression(deps.map((dep) => t.stringLiteral(dep))));
50+
};
51+
52+
export default encase;

src/react-jpex/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NodePath, types as t } from '@babel/core';
2+
import { State } from '../common';
3+
import encase from './encase';
4+
import useResolve from './useResolve';
5+
6+
const reactJpex = (
7+
programPath: NodePath<t.Program>,
8+
path: NodePath<any>,
9+
state: State,
10+
) => {
11+
encase(programPath, path, state);
12+
useResolve(programPath, path, state);
13+
};
14+
15+
export default reactJpex;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { types as t, Visitor, NodePath } from '@babel/core';
1+
import { NodePath, types as t, Visitor } from '@babel/core';
22
import {
33
getConcreteTypeName,
4-
State,
54
getTypeParameter,
6-
} from './common';
5+
State,
6+
} from '../common';
77

88
const importVisitor: Visitor<{
99
found: boolean,

tests/encase.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { transformAsync } from '@babel/core';
2+
import test from 'ava';
3+
4+
test('encase', async(t) => {
5+
const code = `
6+
import jpex from 'jpex';
7+
8+
type Foo = string;
9+
type Bar = number;
10+
11+
const fn = jpex.encase((foo: Foo, bar: Bar) => (seed: string) => {
12+
return seed + foo + bar;
13+
});
14+
`;
15+
const { code: actual } = await transformAsync(code, {
16+
filename: './code.ts',
17+
babelrc: false,
18+
configFile: false,
19+
presets: [
20+
'@babel/preset-typescript',
21+
],
22+
plugins: [
23+
[
24+
'./dist',
25+
],
26+
],
27+
});
28+
29+
t.snapshot(actual);
30+
});

tests/react-jpex.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { transformAsync } from '@babel/core';
2+
import test from 'ava';
3+
4+
test('encase', async(t) => {
5+
const code = `
6+
import { useResolve, encase } from 'react-jpex';
7+
8+
type Foo = string;
9+
type Bar = number;
10+
type Baz = string;
11+
12+
const Component = encase((foo: Foo, bar: Bar) => (props: {}) => {
13+
const baz = useResolve<Baz>();
14+
15+
return foo + bar + baz;
16+
})
17+
`;
18+
const { code: actual } = await transformAsync(code, {
19+
filename: './code.ts',
20+
babelrc: false,
21+
configFile: false,
22+
presets: [
23+
'@babel/preset-typescript',
24+
],
25+
plugins: [
26+
[
27+
'./dist',
28+
],
29+
],
30+
});
31+
32+
t.snapshot(actual);
33+
});

tests/snapshots/encase.ts.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Snapshot report for `tests/encase.ts`
2+
3+
The actual snapshot is saved in `encase.ts.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## encase
8+
9+
> Snapshot 1
10+
11+
`import jpex from 'jpex';␊
12+
const fn = jpex.encase(["type:/code/Foo", "type:/code/Bar"], (foo, bar) => seed => {␊
13+
return seed + foo + bar;␊
14+
});`

tests/snapshots/encase.ts.snap

209 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)