Skip to content

Commit

Permalink
Correctly visit param decorators to prevent their imports being remov…
Browse files Browse the repository at this point in the history
…ed in typescript transform (#8738)

* Remove types on program exit in typescript transform

* Revert changes to typescript transform

* Correctly add visitors for param decorators

* Fix plugin for node 6
  • Loading branch information
yuri-karadzhov authored and danez committed Jan 24, 2019
1 parent 46ba594 commit 854313a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { NgModule, Optional, SkipSelf } from '@angular/core';

@NgModule({})
export class CoreModule {
constructor(@Optional() @SkipSelf() parentModule) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"plugins": [
"transform-typescript",
[
"proposal-decorators",
{
"legacy": true
}
],
"./plugin"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var _dec, _class;

import { NgModule, Optional, SkipSelf } from '@angular/core';
export let CoreModule = (_dec = NgModule({}), _dec(_class = class CoreModule {
constructor(_parentModule) {
var _parentModule2 = Optional()(SkipSelf()(_parentModule));
}

}) || _class);
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict";
// https://github.com/benderTheCrime/babel-plugin-transform-function-parameter-decorators/

Object.defineProperty(exports, "__esModule", {
value: true,
});

exports.default = function(_ref) {
var types = _ref.types;

return {
visitor: {
Function: function parseFunctionPath(path) {
(path.get("params") || [])
.slice()
.reverse()
.forEach(function(param) {
var name = param.node.name;
var paramUidName = path.scope.generateUidIdentifier(name).name;
var resultantDecorator = void 0;

(param.node.decorators || [])
.slice()
.reverse()
.forEach(function(decorator) {
resultantDecorator = types.callExpression(
decorator.expression,
[resultantDecorator || types.Identifier(paramUidName)]
);
});

if (resultantDecorator) {
var decoratedParamUidName = path.scope.generateUidIdentifier(name)
.name;

path.scope.rename(name, decoratedParamUidName);
param.parentPath
.get("body")
.unshiftContainer(
"body",
types.variableDeclaration("var", [
types.variableDeclarator(
types.Identifier(decoratedParamUidName),
resultantDecorator
),
])
);
param.replaceWith(types.Identifier(paramUidName));
}
});
},
},
};
};
2 changes: 1 addition & 1 deletion packages/babel-types/src/definitions/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export const patternLikeCommon = {

defineType("Identifier", {
builder: ["name"],
visitor: ["typeAnnotation"],
visitor: ["typeAnnotation", "decorators" /* for legacy param decorators */],
aliases: ["Expression", "PatternLike", "LVal", "TSEntityName"],
fields: {
...patternLikeCommon,
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-types/src/definitions/es2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "./core";

defineType("AssignmentPattern", {
visitor: ["left", "right"],
visitor: ["left", "right", "decorators" /* for legacy param decorators */],
builder: ["left", "right"],
aliases: ["Pattern", "PatternLike", "LVal"],
fields: {
Expand Down Expand Up @@ -472,7 +472,11 @@ defineType("ClassMethod", {
});

defineType("ObjectPattern", {
visitor: ["properties", "typeAnnotation"],
visitor: [
"properties",
"typeAnnotation",
"decorators" /* for legacy param decorators */,
],
builder: ["properties"],
aliases: ["Pattern", "PatternLike", "LVal"],
fields: {
Expand Down

0 comments on commit 854313a

Please sign in to comment.