Skip to content

Commit 080468c

Browse files
committed
✨ Macro support
1 parent 3a52ff7 commit 080468c

File tree

5 files changed

+77
-26
lines changed

5 files changed

+77
-26
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@
6565
"@babel/types": "^7.6.3",
6666
"@favoloso/conventional-changelog-emoji": "^0.10.0",
6767
"@release-it/conventional-changelog": "^1.1.0",
68+
"@types/babel-plugin-macros": "^2.8.5",
6869
"@types/jest": "^27.4.1",
70+
"babel-plugin-macros": "^3.1.0",
6971
"babel-test": "^0.2.3",
7072
"conventional-changelog-cli": "^2.0.25",
7173
"husky": "^4.2.3",
@@ -80,7 +82,8 @@
8082
},
8183
"peerDependencies": {
8284
"@babel/core": "^7",
83-
"@babel/traverse": "^7"
85+
"@babel/traverse": "^7",
86+
"babel-plugin-macros": "^3.1.0"
8487
},
8588
"peerDependenciesMeta": {
8689
"@babel/core": {
@@ -90,4 +93,4 @@
9093
"optional": true
9194
}
9295
}
93-
}
96+
}

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: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@
963963
"@babel/helper-validator-option" "^7.16.7"
964964
"@babel/plugin-transform-typescript" "^7.16.7"
965965

966-
"@babel/runtime@^7.8.4":
966+
"@babel/runtime@^7.12.5", "@babel/runtime@^7.8.4":
967967
version "7.17.2"
968968
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.17.2.tgz#66f68591605e59da47523c631416b18508779941"
969969
integrity sha512-hzeyJyMA1YGdJTuWU0e/j4wKXrU4OMFvY2MSlaI9B7VQb0r5cxTE3EAIS2Q7Tn2RIcDkRvTA/v2JsAEhxe99uw==
@@ -1424,9 +1424,16 @@
14241424
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
14251425
integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==
14261426

1427-
"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.14":
1427+
"@types/babel-plugin-macros@^2.8.5":
1428+
version "2.8.5"
1429+
resolved "https://registry.npmjs.org/@types/babel-plugin-macros/-/babel-plugin-macros-2.8.5.tgz#04474f9898aa9112afc22fa0e7e53a898fcaba4c"
1430+
integrity sha512-+NcIm/VBaSb4xaycov9f4Vmk4hMVPrgISoHEk+kalgVK6BlkwDZbXkW9kt1jCXVczTKXldL5RHIacExaWzxAkA==
1431+
dependencies:
1432+
"@types/babel__core" "*"
1433+
1434+
"@types/babel__core@*", "@types/babel__core@^7.0.0", "@types/babel__core@^7.1.14":
14281435
version "7.1.18"
1429-
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.18.tgz#1a29abcc411a9c05e2094c98f9a1b7da6cdf49f8"
1436+
resolved "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.18.tgz#1a29abcc411a9c05e2094c98f9a1b7da6cdf49f8"
14301437
integrity sha512-S7unDjm/C7z2A2R9NzfKCK1I+BAALDtxEmsJBwlB3EzNfb929ykjL++1CK9LO++EIp2fQrC8O+BwjKvz6UeDyQ==
14311438
dependencies:
14321439
"@babel/parser" "^7.1.0"
@@ -1766,6 +1773,15 @@ babel-plugin-jest-hoist@^27.5.1:
17661773
"@types/babel__core" "^7.0.0"
17671774
"@types/babel__traverse" "^7.0.6"
17681775

1776+
babel-plugin-macros@^3.1.0:
1777+
version "3.1.0"
1778+
resolved "https://registry.npmjs.org/babel-plugin-macros/-/babel-plugin-macros-3.1.0.tgz#9ef6dc74deb934b4db344dc973ee851d148c50c1"
1779+
integrity sha512-Cg7TFGpIr01vOQNODXOOaGz2NpCU5gl8x1qJFbb6hbZxR7XrcE2vtbAsTAbJ7/xwJtUuJEw8K8Zr/AE0LHlesg==
1780+
dependencies:
1781+
"@babel/runtime" "^7.12.5"
1782+
cosmiconfig "^7.0.0"
1783+
resolve "^1.19.0"
1784+
17691785
babel-plugin-polyfill-corejs2@^0.3.0:
17701786
version "0.3.1"
17711787
resolved "https://registry.yarnpkg.com/babel-plugin-polyfill-corejs2/-/babel-plugin-polyfill-corejs2-0.3.1.tgz#440f1b70ccfaabc6b676d196239b138f8a2cfba5"
@@ -3451,7 +3467,7 @@ is-ci@2.0.0, is-ci@^2.0.0:
34513467

34523468
is-core-module@^2.8.1:
34533469
version "2.8.1"
3454-
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.8.1.tgz#f59fdfca701d5879d0a6b100a40aa1560ce27211"
3470+
resolved "https://registry.npmjs.org/is-core-module/-/is-core-module-2.8.1.tgz#f59fdfca701d5879d0a6b100a40aa1560ce27211"
34553471
integrity sha512-SdNCUs284hr40hFTFP6l0IfZ/RSrMXF3qgoRHd3/79unUTvrFO/JoXwkGm+5J/Oe3E/b5GsnG330uUNgRpu1PA==
34563472
dependencies:
34573473
has "^1.0.3"
@@ -4874,7 +4890,7 @@ path-key@^3.0.0, path-key@^3.1.0:
48744890

48754891
path-parse@^1.0.6, path-parse@^1.0.7:
48764892
version "1.0.7"
4877-
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
4893+
resolved "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
48784894
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==
48794895

48804896
path-type@^1.0.0:
@@ -5336,7 +5352,7 @@ resolve@^1.1.6, resolve@^1.10.0:
53365352
dependencies:
53375353
path-parse "^1.0.6"
53385354

5339-
resolve@^1.14.2, resolve@^1.20.0, resolve@^1.3.2:
5355+
resolve@^1.14.2, resolve@^1.19.0, resolve@^1.20.0, resolve@^1.3.2:
53405356
version "1.22.0"
53415357
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.0.tgz#5e0b8c67c15df57a89bdbabe603a002f21731198"
53425358
integrity sha512-Hhtrw0nLeSrFQ7phPp4OOcVjLPIeMnRlr5mcnVuMe7M/7eBn98A3hmFRLoFo3DLZkivSYwhRUJTyPyWAk56WLw==
@@ -5752,7 +5768,7 @@ supports-hyperlinks@^2.0.0:
57525768

57535769
supports-preserve-symlinks-flag@^1.0.0:
57545770
version "1.0.0"
5755-
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
5771+
resolved "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
57565772
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==
57575773

57585774
symbol-tree@^3.2.4:

0 commit comments

Comments
 (0)