Skip to content

Commit

Permalink
fix(plugin): add imports when downleveled (transpilation)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 3, 2020
1 parent 4a2c734 commit acf5179
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/plugin/visitors/model-class.visitor.ts
Expand Up @@ -266,10 +266,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
if (ts.isAsExpression(initializer)) {
initializer = initializer.expression;
}
return ts.createPropertyAssignment(
key,
ts.createIdentifier(initializer.getText())
);
return ts.createPropertyAssignment(key, ts.getMutableClone(initializer));
}

createValidationPropertyAssignments(
Expand Down Expand Up @@ -320,12 +317,11 @@ export class ModelClassVisitor extends AbstractFileVisitor {
return;
}
const argument: ts.Expression = head(getDecoratorArguments(decoratorRef));
assignments.push(
ts.createPropertyAssignment(
propertyKey,
ts.createIdentifier(argument && argument.getText())
)
);
if (argument) {
assignments.push(
ts.createPropertyAssignment(propertyKey, ts.getMutableClone(argument))
);
}
}

addClassMetadata(
Expand Down
39 changes: 39 additions & 0 deletions test/plugin/fixtures/es5-class.dto.ts
@@ -0,0 +1,39 @@
export const es5CreateCatDtoText = `
import { IsInt, IsString } from 'class-validator';
import { Status } from './status';
import { CONSTANT_STRING, CONSTANT_OBJECT, MIN_VAL } from './constants';
export class CreateCatDtoEs5 {
name: string = CONSTANT_STRING;
status: Status = Status.ENABLED;
obj = CONSTANT_OBJECT;
@Min(MIN_VAL)
@Max(10)
age: number = 3;
}
`;

export const es5CreateCatDtoTextTranspiled = `\"use strict\";
Object.defineProperty(exports, \"__esModule\", { value: true });
var openapi = require(\"@nestjs/swagger\");
var status_1 = require(\"./status\");
var constants_1 = require(\"./constants\");
var CreateCatDtoEs5 = /** @class */ (function () {
function CreateCatDtoEs5() {
this.name = constants_1.CONSTANT_STRING;
this.status = status_1.Status.ENABLED;
this.obj = constants_1.CONSTANT_OBJECT;
this.age = 3;
}
CreateCatDtoEs5._OPENAPI_METADATA_FACTORY = function () {
return { name: { required: true, type: function () { return String; }, default: constants_1.CONSTANT_STRING }, status: { required: true, type: function () { return Object; }, default: status_1.Status.ENABLED }, obj: { required: true, type: function () { return Object; }, default: constants_1.CONSTANT_OBJECT }, age: { required: true, type: function () { return Number; }, default: 3, minimum: constants_1.MIN_VAL, maximum: 10 } };
};
__decorate([
Min(constants_1.MIN_VAL),
Max(10)
], CreateCatDtoEs5.prototype, \"age\", void 0);
return CreateCatDtoEs5;
}());
exports.CreateCatDtoEs5 = CreateCatDtoEs5;
`;
23 changes: 23 additions & 0 deletions test/plugin/model-class-visitor.spec.ts
Expand Up @@ -12,6 +12,10 @@ import {
createCatDtoText,
createCatDtoTextTranspiled
} from './fixtures/create-cat.dto';
import {
es5CreateCatDtoText,
es5CreateCatDtoTextTranspiled
} from './fixtures/es5-class.dto';

describe('API model properties', () => {
it('should add the metadata factory when no decorators exist', () => {
Expand Down Expand Up @@ -70,4 +74,23 @@ describe('API model properties', () => {
});
expect(result.outputText).toEqual(createCatDtoTextAlt2Transpiled);
});

it('should manage imports statements when code "downleveled"', () => {
const options: ts.CompilerOptions = {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES5,
noEmitHelpers: true
};
const filename = 'es5-class.dto.ts';
const fakeProgram = ts.createProgram([filename], options);

const result = ts.transpileModule(es5CreateCatDtoText, {
compilerOptions: options,
fileName: filename,
transformers: {
before: [before({ classValidatorShim: true }, fakeProgram)]
}
});
expect(result.outputText).toEqual(es5CreateCatDtoTextTranspiled);
});
});

0 comments on commit acf5179

Please sign in to comment.