Skip to content

Commit 914d76f

Browse files
committed
feat(as): pass jsx namespace as "as" to directive
in order to support aliased directives ref Xiphe/patternson-project#18
1 parent 2554444 commit 914d76f

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

src/attributesToObject.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ export default function attributesToObject(t, someAttributes, directives) {
1616

1717
const attributes = someAttributes
1818
.filter((attribute) => {
19-
return t.isJSXSpreadAttribute(attribute.node) ||
20-
attributeDirectives.indexOf(attribute.get('name.name').node) === -1;
19+
if (t.isJSXSpreadAttribute(attribute.node)) {
20+
return true;
21+
}
22+
23+
const name = t.isJSXNamespacedName(attribute.get('name'))
24+
? attribute.get('name.name.name').node
25+
: attribute.get('name.name').node;
26+
27+
return attributeDirectives.indexOf(name) === -1;
2128
})
2229
.map((attribute) => {
2330
if (t.isJSXSpreadAttribute(attribute.node)) {

src/babelPluginTransformJsxDirectives.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default function babelPluginTransformJsxDirectives(babel) {
4040
memo,
4141
{
4242
name: directiveName,
43+
as,
4344
source,
4445
options,
4546
globalOptions,
@@ -63,7 +64,8 @@ export default function babelPluginTransformJsxDirectives(babel) {
6364
t.jSXExpressionContainer(newElm),
6465
newProps,
6566
options,
66-
globalOptions
67+
globalOptions,
68+
as
6769
),
6870
Elm: newElm,
6971
props: newProps,
@@ -80,7 +82,8 @@ export default function babelPluginTransformJsxDirectives(babel) {
8082
: t.stringLiteral(name),
8183
attributes,
8284
options,
83-
globalOptions
85+
globalOptions,
86+
as
8487
),
8588
};
8689
}, {

src/createDirective.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export default function createDirective(
77
Elm,
88
props,
99
options,
10-
globalOptions
10+
globalOptions,
11+
as
1112
) {
1213
const targetAttributes = [
1314
t.jSXAttribute(
@@ -27,6 +28,13 @@ export default function createDirective(
2728
));
2829
}
2930

31+
if (as) {
32+
targetAttributes.push(t.jSXAttribute(
33+
t.jSXIdentifier('as'),
34+
t.StringLiteral(as)
35+
));
36+
}
37+
3038
if (globalOptions) {
3139
const buildGlobalOptionsNode = template(`var x = ${JSON.stringify(globalOptions)};`);
3240
const globalOptionsNode = buildGlobalOptionsNode().declarations[0].init;

src/getApplicableDirectives.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@ export default function getApplicableDirectives(babel, path, directives) {
1010
if (t.isJSXSpreadAttribute(attribute.node)) {
1111
return null;
1212
}
13-
return attribute.get('name.name').node;
13+
14+
if (t.isJSXNamespacedName(attribute.get('name'))) {
15+
return {
16+
name: attribute.get('name.name.name').node,
17+
as: attribute.get('name.namespace.name').node,
18+
value: attribute.get('value').node,
19+
};
20+
}
21+
22+
return {
23+
name: attribute.get('name.name').node,
24+
value: attribute.get('value').node,
25+
};
1426
}).filter(a => a);
1527

1628
return directives.reduce(
@@ -32,7 +44,7 @@ export default function getApplicableDirectives(babel, path, directives) {
3244
name === directiveName
3345
) || (
3446
viaAttribute &&
35-
attributes.indexOf(directiveName) !== -1
47+
attributes.map(({ name: n }) => n).indexOf(directiveName) !== -1
3648
)
3749
) {
3850
const directive = {
@@ -43,11 +55,9 @@ export default function getApplicableDirectives(babel, path, directives) {
4355
};
4456

4557
if (viaAttribute) {
46-
const options = path.get('attributes').find((attribute) => {
47-
return !t.isJSXSpreadAttribute(attribute.node) &&
48-
attribute.get('name.name').node === directiveName;
49-
}).get('value').node;
58+
const { value: options, as } = attributes.find(({ name: n }) => n === directiveName);
5059

60+
directive.as = as;
5161
directive.options = transformOptions
5262
? transformOptions(babel, options)
5363
: options;

test/__snapshots__/spec.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ import _bazDirective from \\"./test/directives/baz.js\\";
6666
}} options=\\"bar\\" next={(_Elm2, _props2) => <_bazDirective Elm={_Elm2} props={_props2} options=\\"qux\\" next={(_Elm, _props) => <_Elm {..._props} />} />} />;"
6767
`;
6868

69+
exports[`babel-plugin-transform-jsx-directives provides attribute namespace to directive 1`] = `
70+
"import _fooDirective from \\"foo.js\\";
71+
72+
<_fooDirective Elm=\\"div\\" props={{}} options=\\"baz\\" as=\\"bar\\" next={(_Elm, _props) => <_Elm {..._props} />} />;"
73+
`;
74+
6975
exports[`babel-plugin-transform-jsx-directives provides global options to directive 1`] = `
7076
"import _fooDirective from \\"foo.js\\";
7177

test/spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,15 @@ describe('babel-plugin-transform-jsx-directives', () => {
236236
transform('<foo />', { directives: [['conventional-changelog-core', { foo: true }, 'extra']] });
237237
}).toThrow(errorMatching('Unexpected directive declaration'));
238238
});
239+
240+
it('provides attribute namespace to directive', () => {
241+
const code = transform(
242+
`
243+
<div bar:foo="baz" />
244+
`,
245+
{ directives: [{ name: 'foo', source: 'foo.js' }] }
246+
);
247+
248+
expect(code).toMatchSnapshot();
249+
});
239250
});

0 commit comments

Comments
 (0)