Skip to content

Commit 2554444

Browse files
committed
feat(globalOptions): add feature
so that options can be provided globally to all directives in order to reduce boilerplate code ref Xiphe/patternson-project#18
1 parent 1d2d654 commit 2554444

File tree

8 files changed

+113
-49
lines changed

8 files changed

+113
-49
lines changed

package-lock.json

Lines changed: 13 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"semantic-release": "8.0.3"
6565
},
6666
"dependencies": {
67-
"babel-plugin-syntax-jsx": "^6.18.0"
67+
"babel-plugin-syntax-jsx": "^6.18.0",
68+
"babel-template": "6.26.0"
6869
}
6970
}

src/babelPluginTransformJsxDirectives.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ export default function babelPluginTransformJsxDirectives(babel) {
3838

3939
const { inner } = directives.reduce((
4040
memo,
41-
{ name: directiveName, source, options },
41+
{
42+
name: directiveName,
43+
source,
44+
options,
45+
globalOptions,
46+
},
4247
i
4348
) => {
4449
const localName = importDirective(babel, path, directiveName, source);
@@ -57,7 +62,8 @@ export default function babelPluginTransformJsxDirectives(babel) {
5762
memo,
5863
t.jSXExpressionContainer(newElm),
5964
newProps,
60-
options
65+
options,
66+
globalOptions
6167
),
6268
Elm: newElm,
6369
props: newProps,
@@ -73,7 +79,8 @@ export default function babelPluginTransformJsxDirectives(babel) {
7379
? t.jSXExpressionContainer(t.identifier(name))
7480
: t.stringLiteral(name),
7581
attributes,
76-
options
82+
options,
83+
globalOptions
7784
),
7885
};
7986
}, {

src/createDirective.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import template from 'babel-template';
2+
13
export default function createDirective(
24
t,
35
directiveName,
46
inner,
57
Elm,
68
props,
7-
options
9+
options,
10+
globalOptions
811
) {
912
const targetAttributes = [
1013
t.jSXAttribute(
@@ -24,6 +27,16 @@ export default function createDirective(
2427
));
2528
}
2629

30+
if (globalOptions) {
31+
const buildGlobalOptionsNode = template(`var x = ${JSON.stringify(globalOptions)};`);
32+
const globalOptionsNode = buildGlobalOptionsNode().declarations[0].init;
33+
34+
targetAttributes.push(t.jSXAttribute(
35+
t.jSXIdentifier('globalOptions'),
36+
t.JSXExpressionContainer(globalOptionsNode)
37+
));
38+
}
39+
2740
targetAttributes.push(t.jSXAttribute(
2841
t.jSXIdentifier('next'),
2942
t.jSXExpressionContainer(t.arrowFunctionExpression(

src/getApplicableDirectives.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default function getApplicableDirectives(babel, path, directives) {
2121
type,
2222
source,
2323
transformOptions,
24+
globalOptions,
2425
}
2526
) => {
2627
const viaAttribute = type === 'attribute';
@@ -36,6 +37,7 @@ export default function getApplicableDirectives(babel, path, directives) {
3637
) {
3738
const directive = {
3839
name: directiveName,
40+
globalOptions,
3941
type,
4042
source,
4143
};

src/normalizeDirectives.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,35 @@ function resolveSource(file, source) {
4646
return path.resolve(path.dirname(file), source);
4747
}
4848

49-
function prepare(someDirectives) {
50-
if (typeof someDirectives === 'string') {
51-
const { file, directives } = load(someDirectives);
49+
function getGlobalOptions(directive) {
50+
if (Array.isArray(directive)) {
51+
if (directive.length > 2) {
52+
throw new Error(`Unexpected directive declaration ${JSON.stringify(directive)}`);
53+
}
54+
55+
return {
56+
directive: directive[0],
57+
globalOptions: directive[1],
58+
};
59+
}
60+
61+
return { directive };
62+
}
63+
64+
function prepare(directiveWithOptions) {
65+
const { directive: someDirective, globalOptions } = getGlobalOptions(directiveWithOptions);
66+
67+
if (typeof someDirective === 'string') {
68+
const { file, directives } = load(someDirective);
5269
return toArray(directives).map((directive) => {
5370
return Object.assign({}, directive, {
5471
source: resolveSource(file, directive.source),
72+
globalOptions,
5573
});
5674
});
5775
}
5876

59-
return toArray(someDirectives);
77+
return [Object.assign({}, { globalOptions }, someDirective)];
6078
}
6179

6280
function addDefaults(directive) {
@@ -71,8 +89,8 @@ export default function normalizeDirectives(directives) {
7189
return [];
7290
}
7391

74-
return directives.reduce((memo, someDirectives) => {
75-
return memo.concat(prepare(someDirectives).map(addDefaults));
92+
return directives.reduce((memo, directive) => {
93+
return memo.concat(prepare(directive).map(addDefaults));
7694
}, []).sort(({ priority }, { priority: otherPriority }) => {
7795
return priority - otherPriority;
7896
});

test/__snapshots__/spec.js.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ 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 global options to directive 1`] = `
70+
"import _fooDirective from \\"foo.js\\";
71+
72+
<_fooDirective Elm=\\"div\\" props={{}} options=\\"Bar\\" globalOptions={{
73+
\\"baz\\": \\"qux\\"
74+
}} next={(_Elm, _props) => <_Elm {..._props} />} />;"
75+
`;
76+
77+
exports[`babel-plugin-transform-jsx-directives provides global options to directive imported from string 1`] = `
78+
"import _changelogDirective from \\"conventional-changelog-core/conventionalChangelogDirective.js\\";
79+
<_changelogDirective Elm=\\"changelog\\" props={{}} globalOptions={{
80+
\\"foo\\": true
81+
}} next={(_Elm, _props) => <_Elm {..._props} />} />;"
82+
`;
83+
6984
exports[`babel-plugin-transform-jsx-directives transforms an atrribute directive 1`] = `
7085
"import _actionDirective from \\"./test/directives/action.js\\";
7186
<_actionDirective Elm={Button} props={{

0 commit comments

Comments
 (0)