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:
parent
46ba5940c2
commit
854313a759
@ -0,0 +1,6 @@
|
||||
import { NgModule, Optional, SkipSelf } from '@angular/core';
|
||||
|
||||
@NgModule({})
|
||||
export class CoreModule {
|
||||
constructor(@Optional() @SkipSelf() parentModule) { }
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
{
|
||||
"plugins": [
|
||||
"transform-typescript",
|
||||
[
|
||||
"proposal-decorators",
|
||||
{
|
||||
"legacy": true
|
||||
}
|
||||
],
|
||||
"./plugin"
|
||||
]
|
||||
}
|
||||
@ -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);
|
||||
54
packages/babel-plugin-transform-typescript/test/fixtures/imports/parameter-decorators/plugin.js
vendored
Normal file
54
packages/babel-plugin-transform-typescript/test/fixtures/imports/parameter-decorators/plugin.js
vendored
Normal 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));
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
@ -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,
|
||||
|
||||
@ -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: {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user