Generate wrong code when handle const enum in the first time
LiJinyao opened this issue · 2 comments
Please clone this repo to check this issue: https://github.com/LiJinyao/issue-espower-typescript
The issue
compile wrong code when handle const enum
in the first time.
I think the problem might caused by TypeScriptSimple
the enum file is very simple:
export const enum COLOR { RED, BLUE }
and the test code is:
import * as assert from 'assert'
import { COLOR } from '../src/enumTest'
describe('enum test', () => {
it('should return 0', () => {
const result: number = COLOR.RED // in the first time this line will compile to
// const result = enumTest_1.COLOR.RED;
// which is not as expected.
assert(result === 0)
})
})
Step to reproduce the bug
clone this repo and run:
npm install
npm run test
npm run build # to check the compiled test code (which is right).
The code in test
folder is same, but the enum test 2
will pass, enum test
will fail.
I checked the complied code by add a console.log(result)
in espower-typescript package's index.js
line 23, and I found that the compile result of enumTest.test.ts
is not right.
The enumTest.test.ts
is compile to:
Object.defineProperty(exports, '__esModule', { value: true });
const assert = require('power-assert');
const enumTest_1 = require('../src/enumTest'); // this line should not be there, tsc won't add this line because const enum is only exist in typescript
describe('enum test', () => {
it('should return 0', () => {
var _rec1 = new _PowerAssertRecorder1();
const result = enumTest_1.COLOR.RED; // this is not right, it should be a number since COLOR is a const enum.
assert(_rec1._expr(_rec1._capt(_rec1._capt(result, 'arguments/0/left') === 0, 'arguments/0'), {
content: 'assert(result === 0)',
filepath: 'test\\enumTest.test.ts',
line: 11
}));
});
});
The enumTest2.test.ts
is compile to:
Object.defineProperty(exports, '__esModule', { value: true });
const assert = require('power-assert');
// there no require('../src/enumTest'), which is same as tsc compile result.
describe('enum test 2', () => {
it('should return 0', () => {
var _rec1 = new _PowerAssertRecorder1();
const result = 0; // this is right, it is a number since COLOR is a const enum
assert(_rec1._expr(_rec1._capt(_rec1._capt(result, 'arguments/0/left') === 0, 'arguments/0'), {
content: 'assert(result === 0)',
filepath: 'test\\enumTest2.test.ts',
line: 11
}));
});
});
What is expected
All test should pass.
full out put of the compile result
'use strict';
var _PowerAssertRecorder1 = function () {
function PowerAssertRecorder() {
this.captured = [];
}
PowerAssertRecorder.prototype._capt = function _capt(value, espath) {
this.captured.push({
value: value,
espath: espath
});
return value;
};
PowerAssertRecorder.prototype._expr = function _expr(value, source) {
var capturedValues = this.captured;
this.captured = [];
return {
powerAssertContext: {
value: value,
events: capturedValues
},
source: source
};
};
return PowerAssertRecorder;
}();
Object.defineProperty(exports, '__esModule', { value: true });
const assert = require('power-assert');
const enumTest_1 = require('../src/enumTest');
describe('enum test', () => {
it('should return 0', () => {
var _rec1 = new _PowerAssertRecorder1();
const result = enumTest_1.COLOR.RED;
assert(_rec1._expr(_rec1._capt(_rec1._capt(result, 'arguments/0/left') === 0, 'arguments/0'), {
content: 'assert(result === 0)',
filepath: 'test\\enumTest.test.ts',
line: 11
}));
});
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZW51bVRlc3QudGVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7
Ozs7OztBQUlBLE1BQUEsTUFBQSxHQUFBLE9BQUEsQ0FBQSxjQUFBLENBQUE7QUFDQSxNQUFBLFVBQUEsR0FBQSxPQUFBLENBQUEsaUJBQUEsQ0FBQTtBQUVBLFFBQUEsQ0FBUyxXQUFULEVBQXNCLE1BQUE7QUFBQSxJQUNsQixFQUFBLENBQUcsaUJB
QUgsRUFBc0IsTUFBQTtBQUFBLFFBRVgsSUFBQSxLQUFBLE9BQUEscUJBQUEsR0FGVztBQUFBLFFBQ2xCLE1BQU0sTUFBQSxHQUFpQixVQUFBLENBQUEsS0FBQSxDQUFNLEdBQTdCLENBRGtCO0FBQUEsUUFFbEIsTUFBQSxDQUFPLEtBQUEsQ0FBQSxL
QUFBLENBQUEsS0FBQSxDQUFBLEtBQUEsQ0FBQSxLQUFBLENBQUEsS0FBQSxDQUFBLE1BQUEsMEJBQVcsQ0FBWDtBQUFBLFlBQUEsT0FBQTtBQUFBLFlBQUEsUUFBQTtBQUFBLFlBQUEsSUFBQTtBQUFBLFVBQVAsRUFGa0I7QUFBQSxLQUF0QixFQURr
QjtBQUFBLENBQXRCIiwic291cmNlUm9vdCI6IkM6L1VzZXJzL2ppbnlhb2xpLlRFTkNFTlQvRG9jdW1lbnRzL0dpdEh1Yi9pc3N1ZS1lc3Bvd2VyLXR5cGVzY3JpcHQvIn0=
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZW51bVRlc3QuanMiLCJzb3VyY2VSb290IjoiQzovVXNlcnMvamlueWFvbGkuVEVOQ0VOVC9Eb2N1bWVudHMvR2l0SHViL2lzc3VlLWVzcG93ZX
ItdHlwZXNjcmlwdC8iLCJzb3VyY2VzIjpbInNyYy9lbnVtVGVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
'use strict';
var _PowerAssertRecorder1 = function () {
function PowerAssertRecorder() {
this.captured = [];
}
PowerAssertRecorder.prototype._capt = function _capt(value, espath) {
this.captured.push({
value: value,
espath: espath
});
return value;
};
PowerAssertRecorder.prototype._expr = function _expr(value, source) {
var capturedValues = this.captured;
this.captured = [];
return {
powerAssertContext: {
value: value,
events: capturedValues
},
source: source
};
};
return PowerAssertRecorder;
}();
Object.defineProperty(exports, '__esModule', { value: true });
const assert = require('power-assert');
describe('enum test 2', () => {
it('should return 0', () => {
var _rec1 = new _PowerAssertRecorder1();
const result = 0;
assert(_rec1._expr(_rec1._capt(_rec1._capt(result, 'arguments/0/left') === 0, 'arguments/0'), {
content: 'assert(result === 0)',
filepath: 'test\\enumTest2.test.ts',
line: 11
}));
});
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZW51bVRlc3QyLnRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7
Ozs7Ozs7QUFJQSxNQUFBLE1BQUEsR0FBQSxPQUFBLENBQUEsY0FBQSxDQUFBO0FBR0EsUUFBQSxDQUFTLGFBQVQsRUFBd0IsTUFBQTtBQUFBLElBQ3BCLEVBQUEsQ0FBRyxpQkFBSCxFQUFzQixNQUFBO0FBQUEsUUFFWCxJQUFBLEtBQUEsT0FBQSxx
QkFBQSxHQUZXO0FBQUEsUUFDbEIsTUFBTSxNQUFBLEdBQU0sQ0FBWixDQURrQjtBQUFBLFFBRWxCLE1BQUEsQ0FBTyxLQUFBLENBQUEsS0FBQSxDQUFBLEtBQUEsQ0FBQSxLQUFBLENBQUEsS0FBQSxDQUFBLEtBQUEsQ0FBQSxNQUFBLDBCQUFXLENB
QVg7QUFBQSxZQUFBLE9BQUE7QUFBQSxZQUFBLFFBQUE7QUFBQSxZQUFBLElBQUE7QUFBQSxVQUFQLEVBRmtCO0FBQUEsS0FBdEIsRUFEb0I7QUFBQSxDQUF4QiIsInNvdXJjZVJvb3QiOiJDOi9Vc2Vycy9qaW55YW9saS5URU5DRU5UL0RvY3VtZW50
cy9HaXRIdWIvaXNzdWUtZXNwb3dlci10eXBlc2NyaXB0LyJ9
@LiJinyao Thanks for detail report!
Yes, it's typescript-simple's problem.
typescript-simple parses typescript source per file and doesn't resolve the dependencies itself, Node.js does.
So after once Node.js loader loads enumTest.ts, it's cached in the TypeScriptSimple
instance and resolved as a number literal.
The behavior is by the basic design, it's hard to resolve... I'll try it in the future.
fixed in v9