Skip to content

Commit

Permalink
fix(es/transforms/proposal): Fix type detection (#2431)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_proposal:
 - Try the type of the LHS of an assignment pattern. (#2428)
  • Loading branch information
kdy1 authored Oct 14, 2021
1 parent 647d3ed commit f899584
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/transforms/proposal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_proposal"
repository = "https://github.com/swc-project/swc.git"
version = "0.46.0"
version = "0.46.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,9 @@ fn get_type_ann_of_pat(p: &Pat) -> Option<&TsTypeAnn> {
Pat::Array(p) => &p.type_ann,
Pat::Rest(p) => &p.type_ann,
Pat::Object(p) => &p.type_ann,
Pat::Assign(p) => &p.type_ann,
Pat::Assign(p) => {
return p.type_ann.as_ref().or_else(|| get_type_ann_of_pat(&p.left));
}
Pat::Invalid(_) => return None,
Pat::Expr(_) => return None,
}
Expand Down
6 changes: 3 additions & 3 deletions ecmascript/transforms/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5031,7 +5031,7 @@ var _dec9 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "funct
Function,
Boolean,
Boolean,
void 0
String
]), _dec10 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec11 = Decorate();
let Sample = _class = _dec11(_class = _dec10(_class = _dec9(((_class = class Sample {
constructor(private p0: String, p1: Number, p2: 10, p3: "ABC", p4: boolean, p5: string, p6: number, p7: Object, p8: () => any, p9: "abc" | "def", p10: String | Number, p11: Function, p12: null, p13: undefined, p14: any, p15: (abc: any) => void, p16: false, p17: true, p18: string = "abc"){
Expand Down Expand Up @@ -5069,14 +5069,14 @@ let Sample = _class = _dec11(_class = _dec10(_class = _dec9(((_class = class Sam
_dec1,
_dec2
], Object.getOwnPropertyDescriptor(_class.prototype, "method"), _class.prototype), _dec3 = Decorate(), _dec4 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec5 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [
void 0,
typeof Decorate === "undefined" || typeof Decorate.Name === "undefined" ? Object : Decorate.Name,
typeof Decorate === "undefined" || typeof Decorate.Name === "undefined" ? Object : Decorate.Name
]), _applyDecoratedDescriptor(_class.prototype, "method2", [
_dec3,
_dec4,
_dec5
], Object.getOwnPropertyDescriptor(_class.prototype, "method2"), _class.prototype), _dec6 = Decorate(), _dec7 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec8 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [
void 0
String
]), _applyDecoratedDescriptor(_class.prototype, "assignments", [
_dec6,
_dec7,
Expand Down
22 changes: 22 additions & 0 deletions tests/fixture/issue-2428/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": true,
"dynamicImport": false
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
},
"target": "es2016"
},
"module": {
"type": "commonjs",
"strict": false,
"strictMode": true,
"lazy": false,
"noInterop": false
}
}
10 changes: 10 additions & 0 deletions tests/fixture/issue-2428/1/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

class Foo {
async fnName1(
@Arg('GraphQLArgName', { nullable: true }) argName: boolean,
): Promise<number> { }

async fnName2(
@Arg('GraphQLArgName', { nullable: true }) argName: boolean = false,
): Promise<number> { }
}
85 changes: 85 additions & 0 deletions tests/fixture/issue-2428/1/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"use strict";
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
var desc = {
};
Object.keys(descriptor).forEach(function(key) {
desc[key] = descriptor[key];
});
desc.enumerable = !!desc.enumerable;
desc.configurable = !!desc.configurable;
if ("value" in desc || desc.initializer) {
desc.writable = true;
}
desc = decorators.slice().reverse().reduce(function(desc, decorator) {
return decorator ? decorator(target, property, desc) || desc : desc;
}, desc);
if (context && desc.initializer !== void 0) {
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
}
return desc;
}
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
var _class, _dec, _dec1, _dec2, _dec3, _dec4, _dec5;
let Foo1 = ((_class = class Foo {
fnName1(argName) {
return _asyncToGenerator(function*() {
})();
}
fnName2(argName = false) {
return _asyncToGenerator(function*() {
})();
}
}) || _class, _dec = function(target, key) {
return Arg('GraphQLArgName', {
nullable: true
})(target, key, 0);
}, _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec2 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [
Boolean
]), _applyDecoratedDescriptor(_class.prototype, "fnName1", [
_dec,
_dec1,
_dec2
], Object.getOwnPropertyDescriptor(_class.prototype, "fnName1"), _class.prototype), _dec3 = function(target, key) {
return Arg('GraphQLArgName', {
nullable: true
})(target, key, 0);
}, _dec4 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec5 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [
Boolean
]), _applyDecoratedDescriptor(_class.prototype, "fnName2", [
_dec3,
_dec4,
_dec5
], Object.getOwnPropertyDescriptor(_class.prototype, "fnName2"), _class.prototype), _class);

0 comments on commit f899584

Please sign in to comment.