Correctly visit param decorators to prevent their imports being removed 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
This commit is contained in:
Yuri Karadzhov 2019-01-25 00:07:56 +01:00 committed by Daniel Tschinder
parent 46ba5940c2
commit 854313a759
6 changed files with 88 additions and 3 deletions

View File

@ -0,0 +1,6 @@
import { NgModule, Optional, SkipSelf } from '@angular/core';
@NgModule({})
export class CoreModule {
constructor(@Optional() @SkipSelf() parentModule) { }
}

View File

@ -0,0 +1,12 @@
{
"plugins": [
"transform-typescript",
[
"proposal-decorators",
{
"legacy": true
}
],
"./plugin"
]
}

View File

@ -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);

View File

@ -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));
}
});
},
},
};
};

View File

@ -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,

View File

@ -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: {
@ -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: {