Skip to content

Commit fe63657

Browse files
committed
✨ Macro support
1 parent ce4710a commit fe63657

File tree

5 files changed

+114
-17
lines changed

5 files changed

+114
-17
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
"@babel/types": "^7.6.3",
6464
"@favoloso/conventional-changelog-emoji": "^0.10.0",
6565
"@release-it/conventional-changelog": "^1.1.0",
66+
"@types/babel-plugin-macros": "^2.8.5",
6667
"@types/jest": "^24.0.19",
68+
"babel-plugin-macros": "^3.1.0",
6769
"babel-test": "^0.2.3",
6870
"conventional-changelog-cli": "^2.0.25",
6971
"husky": "^4.2.3",
@@ -72,6 +74,9 @@
7274
"release-it": "13.7.1",
7375
"typescript": "^3.6.4"
7476
},
77+
"peerDependencies": {
78+
"babel-plugin-macros": "^3.1.0"
79+
},
7580
"dependencies": {
7681
"@babel/helper-plugin-utils": "^7.0.0"
7782
}

src/macro.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createMacro, MacroHandler } from 'babel-plugin-macros';
2+
import { transform } from './transform';
3+
4+
const metadataMacro: MacroHandler = ({ references }) => {
5+
references.default.forEach(reference => {
6+
const decorator = reference.findParent(parent => parent.isDecorator())
7+
if (!decorator) {
8+
throw new Error("Metadata macro should be used as class decorator");
9+
}
10+
const classDeclaration = decorator.findParent(parent => parent.isClassDeclaration());
11+
if (!classDeclaration) {
12+
throw new Error("Metadata macro should be used as class decorator");
13+
}
14+
if (classDeclaration.isClassDeclaration()) {
15+
if (classDeclaration.node.decorators) {
16+
classDeclaration.node.decorators = classDeclaration.node.decorators.filter(it => it !== decorator.node);
17+
}
18+
transform(classDeclaration);
19+
}
20+
})
21+
};
22+
23+
export default createMacro(metadataMacro);

src/plugin.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PluginObj } from '@babel/core';
22
import { declare } from '@babel/helper-plugin-utils';
33
import { parameterVisitor } from './parameter/parameterVisitor';
44
import { metadataVisitor } from './metadata/metadataVisitor';
5+
import { transform } from './transform';
56

67
export default declare(
78
(api: any): PluginObj => {
@@ -20,23 +21,7 @@ export default declare(
2021
*/
2122
programPath.traverse({
2223
ClassDeclaration(path) {
23-
for (const field of path.get('body').get('body')) {
24-
if (
25-
field.type !== 'ClassMethod' &&
26-
field.type !== 'ClassProperty'
27-
)
28-
continue;
29-
30-
parameterVisitor(path, field as any);
31-
metadataVisitor(path, field as any);
32-
}
33-
34-
/**
35-
* We need to keep binding in order to let babel know where imports
36-
* are used as a Value (and not just as a type), so that
37-
* `babel-transform-typescript` do not strip the import.
38-
*/
39-
(path.parentPath.scope as any).crawl();
24+
transform(path);
4025
}
4126
});
4227
}

src/transform.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { NodePath } from '@babel/traverse';
2+
import { ClassDeclaration } from '@babel/types';
3+
import { metadataVisitor } from './metadata/metadataVisitor';
4+
import { parameterVisitor } from './parameter/parameterVisitor';
5+
6+
export function transform(path: NodePath<ClassDeclaration>) {
7+
for (const field of path.get('body').get('body')) {
8+
if (
9+
field.type !== 'ClassMethod' &&
10+
field.type !== 'ClassProperty'
11+
)
12+
continue;
13+
14+
parameterVisitor(path, field as any);
15+
metadataVisitor(path, field as any);
16+
}
17+
18+
/**
19+
* We need to keep binding in order to let babel know where imports
20+
* are used as a Value (and not just as a type), so that
21+
* `babel-transform-typescript` do not strip the import.
22+
*/
23+
(path.parentPath.scope as any).crawl();
24+
}

yarn.lock

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,13 @@
844844
"@babel/helper-plugin-utils" "^7.10.4"
845845
"@babel/plugin-transform-typescript" "^7.10.4"
846846

847+
"@babel/runtime@^7.12.5":
848+
version "7.17.2"
849+
resolved "https://registry.npmjs.org/@babel/runtime/-/runtime-7.17.2.tgz#66f68591605e59da47523c631416b18508779941"
850+
integrity sha512-hzeyJyMA1YGdJTuWU0e/j4wKXrU4OMFvY2MSlaI9B7VQb0r5cxTE3EAIS2Q7Tn2RIcDkRvTA/v2JsAEhxe99uw==
851+
dependencies:
852+
regenerator-runtime "^0.13.4"
853+
847854
"@babel/runtime@^7.8.4":
848855
version "7.11.2"
849856
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736"
@@ -1209,6 +1216,24 @@
12091216
dependencies:
12101217
defer-to-connect "^2.0.0"
12111218

1219+
"@types/babel-plugin-macros@^2.8.5":
1220+
version "2.8.5"
1221+
resolved "https://registry.npmjs.org/@types/babel-plugin-macros/-/babel-plugin-macros-2.8.5.tgz#04474f9898aa9112afc22fa0e7e53a898fcaba4c"
1222+
integrity sha512-+NcIm/VBaSb4xaycov9f4Vmk4hMVPrgISoHEk+kalgVK6BlkwDZbXkW9kt1jCXVczTKXldL5RHIacExaWzxAkA==
1223+
dependencies:
1224+
"@types/babel__core" "*"
1225+
1226+
"@types/babel__core@*":
1227+
version "7.1.18"
1228+
resolved "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.18.tgz#1a29abcc411a9c05e2094c98f9a1b7da6cdf49f8"
1229+
integrity sha512-S7unDjm/C7z2A2R9NzfKCK1I+BAALDtxEmsJBwlB3EzNfb929ykjL++1CK9LO++EIp2fQrC8O+BwjKvz6UeDyQ==
1230+
dependencies:
1231+
"@babel/parser" "^7.1.0"
1232+
"@babel/types" "^7.0.0"
1233+
"@types/babel__generator" "*"
1234+
"@types/babel__template" "*"
1235+
"@types/babel__traverse" "*"
1236+
12121237
"@types/babel__core@^7.1.0":
12131238
version "7.1.10"
12141239
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.10.tgz#ca58fc195dd9734e77e57c6f2df565623636ab40"
@@ -1590,6 +1615,15 @@ babel-plugin-jest-hoist@^24.9.0:
15901615
dependencies:
15911616
"@types/babel__traverse" "^7.0.6"
15921617

1618+
babel-plugin-macros@^3.1.0:
1619+
version "3.1.0"
1620+
resolved "https://registry.npmjs.org/babel-plugin-macros/-/babel-plugin-macros-3.1.0.tgz#9ef6dc74deb934b4db344dc973ee851d148c50c1"
1621+
integrity sha512-Cg7TFGpIr01vOQNODXOOaGz2NpCU5gl8x1qJFbb6hbZxR7XrcE2vtbAsTAbJ7/xwJtUuJEw8K8Zr/AE0LHlesg==
1622+
dependencies:
1623+
"@babel/runtime" "^7.12.5"
1624+
cosmiconfig "^7.0.0"
1625+
resolve "^1.19.0"
1626+
15931627
babel-preset-jest@^24.9.0:
15941628
version "24.9.0"
15951629
resolved "https://registry.yarnpkg.com/babel-preset-jest/-/babel-preset-jest-24.9.0.tgz#192b521e2217fb1d1f67cf73f70c336650ad3cdc"
@@ -3485,6 +3519,13 @@ is-ci@2.0.0, is-ci@^2.0.0:
34853519
dependencies:
34863520
ci-info "^2.0.0"
34873521

3522+
is-core-module@^2.8.1:
3523+
version "2.8.1"
3524+
resolved "https://registry.npmjs.org/is-core-module/-/is-core-module-2.8.1.tgz#f59fdfca701d5879d0a6b100a40aa1560ce27211"
3525+
integrity sha512-SdNCUs284hr40hFTFP6l0IfZ/RSrMXF3qgoRHd3/79unUTvrFO/JoXwkGm+5J/Oe3E/b5GsnG330uUNgRpu1PA==
3526+
dependencies:
3527+
has "^1.0.3"
3528+
34883529
is-data-descriptor@^0.1.4:
34893530
version "0.1.4"
34903531
resolved "https://registry.yarnpkg.com/is-data-descriptor/-/is-data-descriptor-0.1.4.tgz#0b5ee648388e2c860282e793f1856fec3f301b56"
@@ -5145,6 +5186,11 @@ path-parse@^1.0.6:
51455186
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
51465187
integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==
51475188

5189+
path-parse@^1.0.7:
5190+
version "1.0.7"
5191+
resolved "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
5192+
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==
5193+
51485194
path-type@^1.0.0:
51495195
version "1.1.0"
51505196
resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441"
@@ -5706,6 +5752,15 @@ resolve@^1.1.6, resolve@^1.10.0, resolve@^1.3.2:
57065752
dependencies:
57075753
path-parse "^1.0.6"
57085754

5755+
resolve@^1.19.0:
5756+
version "1.22.0"
5757+
resolved "https://registry.npmjs.org/resolve/-/resolve-1.22.0.tgz#5e0b8c67c15df57a89bdbabe603a002f21731198"
5758+
integrity sha512-Hhtrw0nLeSrFQ7phPp4OOcVjLPIeMnRlr5mcnVuMe7M/7eBn98A3hmFRLoFo3DLZkivSYwhRUJTyPyWAk56WLw==
5759+
dependencies:
5760+
is-core-module "^2.8.1"
5761+
path-parse "^1.0.7"
5762+
supports-preserve-symlinks-flag "^1.0.0"
5763+
57095764
responselike@^1.0.2:
57105765
version "1.0.2"
57115766
resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"
@@ -6229,6 +6284,11 @@ supports-color@^6.1.0:
62296284
dependencies:
62306285
has-flag "^3.0.0"
62316286

6287+
supports-preserve-symlinks-flag@^1.0.0:
6288+
version "1.0.0"
6289+
resolved "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
6290+
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==
6291+
62326292
symbol-tree@^3.2.2:
62336293
version "3.2.4"
62346294
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2"

0 commit comments

Comments
 (0)