You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
exportconstenumCOLOR{RED,BLUE}
and the test code is:
import*asassertfrom'assert'import{COLOR}from'../src/enumTest'describe('enum test',()=>{it('should return 0',()=>{constresult: 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});constassert=require('power-assert');constenumTest_1=require('../src/enumTest');// this line should not be there, tsc won't add this line because const enum is only exist in typescriptdescribe('enum test',()=>{it('should return 0',()=>{var_rec1=new_PowerAssertRecorder1();constresult=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});constassert=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();constresult=0;// this is right, it is a number since COLOR is a const enumassert(_rec1._expr(_rec1._capt(_rec1._capt(result,'arguments/0/left')===0,'arguments/0'),{content: 'assert(result === 0)',filepath: 'test\\enumTest2.test.ts',line: 11}));});});
LiJinyao
changed the title
compile wrong code when handle const enum in the first time
Generate wrong code when handle const enum in the first time
Aug 29, 2017
@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.
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:
and the test code is:
Step to reproduce the bug
clone this repo and run:
The code in
test
folder is same, but theenum test 2
will pass,enum test
will fail.I checked the complied code by add a
console.log(result)
in espower-typescript package'sindex.js
line 23, and I found that the compile result ofenumTest.test.ts
is not right.The
enumTest.test.ts
is compile to:The
enumTest2.test.ts
is compile to:What is expected
All test should pass.
full out put of the compile result
The text was updated successfully, but these errors were encountered: