I've encountered a strange issue where the transpiler is splitting a single method into two parts:
Before transpiling:
MemberSchema.methods.addAccount = (account: IAccount): void => {
this.account = new Account({
...account,
isActive: true
});
bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function(err: Error, salt: any){
if(err) throw err;
bcrypt.hash(this.account.password, salt, null, function(err: Error, hash: any){
if(err) throw err;
this.account.password = hash;
});
});
};
After transpiling:
exports.MemberSchema.methods.addAccount = (account) => {
this.account = new account_1.Account({}, ...account, isActive, true);
};
bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function (err, salt) {
if (err)
throw err;
bcrypt.hash(this.account.password, salt, null, function (err, hash) {
if (err)
throw err;
this.account.password = hash;
});
});
tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "ES6",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types": ["reflect-metadata"],
"lib": ["ES6"],
"sourceMap": true,
"inlineSources": true,
"pretty": true,
"outDir": "dist",
"rootDir": "src",
"noLib": false,
"declaration": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}
Any insights on what may be causing this issue? Is it a configuration adjustment that needs to be made or does it require modifications to the TypeScript file?
Update:
Member.ts:
import { Schema, Model } from "mongoose";
import mongoose = require("mongoose");
import {Account, AccountSchema} from "../Account/account";
import {IAccount} from "../Account/iaccount";
import {isNullOrUndefined} from "util";
import {IMemberModel} from "./imembermodel";
export let MemberSchema: Schema = new Schema({
account: AccountSchema,
permissions: [{type: Schema.Types.String, enum: ["regularuser", "admin", "owner"]}],
isActive: Schema.Types.Boolean
});
MemberSchema.pre("save", next => {
this.isActive = true;
next();
return this;
});
MemberSchema.methods.addAccount = (account: IAccount): void => {
***this.account = new Account({
...account,
registrationComplete: true
});***
bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function(err: Error, salt: any){
if(err) throw err;
bcrypt.hash(this.account.password, salt, null, function(err: Error, hash: any){
if(err) throw err;
this.account.password = hash;
});
});
};
MemberSchema.methods.updateAccount = (account: IAccount): void => {
if (!isNullOrUndefined(account.password)){
bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function(err: Error, salt: any){
if(err) throw err;
bcrypt.hash(this.account.password, salt, null, function(err: Error, hash: any){
if(err) throw err;
this.account.password = hash;
});
});
}
};
export const Member: Model<IMemberModel> = mongoose.model<IMemberModel>("Member", MemberSchema);
Member.js:
"use strict";
const mongoose_1 = require("mongoose");
const mongoose = require("mongoose");
const account_1 = require("../Account/account");
const util_1 = require("util");
exports.MemberSchema = new mongoose_1.Schema({
account: account_1.AccountSchema,
permissions: [{ type: mongoose_1.Schema.Types.String, enum: ["regularuser", "admin", "owner"] }],
isActive: mongoose_1.Schema.Types.Boolean
});
exports.MemberSchema.pre("save", next => {
this.isActive = true;
next();
return this;
});
exports.MemberSchema.methods.addAccount = (account) => {
this.account = new account_1.Account({}, ...account, registrationComplete, true);
};
;
bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function (err, salt) {
if (err)
throw err;
bcrypt.hash(this.account.password, salt, null, function (err, hash) {
if (err)
throw err;
this.account.password = hash;
});
});
;
exports.MemberSchema.methods.updateAccount = (account) => {
if (!util_1.isNullOrUndefined(account.password)) {
bcrypt.genSalt(parseInt(process.env.SALT_WORK_FACTOR), function (err, salt) {
if (err)
throw err;
bcrypt.hash(this.account.password, salt, null, function (err, hash) {
if (err)
throw err;
this.account.password = hash;
});
});
}
};
exports.Member = mongoose.model("Member", exports.MemberSchema);
//# sourceMappingURL=member.js.map
Update 2: The issue seems to be related to the spread operator being used.
When I commented out this line:
this.account = new Account({
**//...account,**
registrationComplete: true
});
It worked as expected. If the spread operator is standard ES6 syntax, why would it cause such a significant transpiling problem?