Skip to content

Commit 76d8a98

Browse files
committed
feat(transformOptions): support directive specific transformations
a directive can now specify a transformOptions method to prepare its own options in order to move logic from runtime into the compiler
1 parent 0498bfa commit 76d8a98

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/getApplicableDirectives.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function getApplicableDirectives(babel, path, directives) {
1414
}).filter(a => a);
1515

1616
return directives.reduce(
17-
(memo, { name: directiveName, type, source }) => {
17+
(memo, { name: directiveName, type, source, transformOptions }) => {
1818
const viaAttribute = type === 'attribute';
1919

2020
if (
@@ -33,10 +33,14 @@ export default function getApplicableDirectives(babel, path, directives) {
3333
};
3434

3535
if (viaAttribute) {
36-
directive.options = path.get('attributes').find((attribute) => {
36+
const options = path.get('attributes').find((attribute) => {
3737
return !t.isJSXSpreadAttribute(attribute.node) &&
3838
attribute.get('name.name').node === directiveName;
3939
}).get('value').node;
40+
41+
directive.options = transformOptions
42+
? transformOptions(babel, options)
43+
: options;
4044
}
4145

4246
memo.push(directive);

test/__snapshots__/spec.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ import _actionDirective from \\"./test/directives/action.js\\";
2222
<_htmlDirective Elm=\\"html\\" props={{}} next={(_Elm2, _props2) => <_actionDirective Elm={_Elm2} props={_props2} options={woosa} next={(_Elm, _props) => <_Elm {..._props} />} />} />;"
2323
`;
2424

25+
exports[`babel-plugin-transform-jsx-directives applies transformOptions 1`] = `
26+
"import _fooDirective from \\"foo.js\\";
27+
28+
<_fooDirective Elm=\\"div\\" props={{}} options={{
29+
value: \\"Bar\\"
30+
}} next={(_Elm, _props) => <_Elm {..._props} />} />;"
31+
`;
32+
2533
exports[`babel-plugin-transform-jsx-directives converts jsx spread operators to object spread 1`] = `
2634
"import _htmlDirective from \\"./test/directives/html.js\\";
2735

test/spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,32 @@ describe('babel-plugin-transform-jsx-directives', () => {
154154
transform('<foo />', { directives: ['foo-module'] });
155155
}).toThrow(errorMatching('Cannot find module'));
156156
});
157+
158+
it('applies transformOptions', () => {
159+
const code = transform(
160+
`
161+
<div foo="Bar" />
162+
`,
163+
{
164+
directives: [
165+
{
166+
name: 'foo',
167+
transformOptions({ types: t }, node) {
168+
return t.jSXExpressionContainer(
169+
t.objectExpression([
170+
t.objectProperty(
171+
t.identifier('value'),
172+
node
173+
),
174+
])
175+
);
176+
},
177+
source: 'foo.js',
178+
},
179+
],
180+
}
181+
);
182+
183+
expect(code).toMatchSnapshot();
184+
});
157185
});

0 commit comments

Comments
 (0)