|
1 | 1 | import { |
2 | | - createCompilerHost, createProgram, |
3 | | - getDefaultCompilerOptions, |
4 | | - JsxEmit, |
5 | | - ModuleKind, |
6 | | - ModuleResolutionKind, |
7 | | - ScriptTarget |
| 2 | + createCompilerHost, |
| 3 | + createProgram, |
| 4 | + getDefaultCompilerOptions, |
| 5 | + JsxEmit, |
| 6 | + ModuleKind, |
| 7 | + ModuleResolutionKind, |
| 8 | + NewLineKind, |
| 9 | + ScriptTarget |
8 | 10 | } from 'typescript' |
9 | | -import { sync as globSync } from 'glob' |
| 11 | +import {sync as globSync} from 'glob' |
10 | 12 | import transform from '../src' |
11 | | -import { resolve, basename } from 'path' |
12 | | -import { readFileSync, writeFileSync } from 'fs' |
13 | | -// @ts-ignore |
14 | | -import { mkdirpSync } from 'fs-extra' |
| 13 | +import {basename, resolve} from 'path' |
| 14 | +import {readFileSync, writeFileSync} from 'fs' |
| 15 | +import {mkdirpSync} from 'fs-extra' |
15 | 16 |
|
16 | 17 | // Target is ES5 and module is UMD |
17 | 18 | const config = { |
18 | | - ...getDefaultCompilerOptions(), |
19 | | - experimentalDecorators: true, |
20 | | - jsx: JsxEmit.Preserve, |
21 | | - module: ModuleKind.CommonJS, |
22 | | - moduleResolution: ModuleResolutionKind.NodeJs, |
23 | | - noEmitOnError: false, |
24 | | - noUnusedLocals: true, |
25 | | - noUnusedParameters: true, |
26 | | - stripInternal: true, |
27 | | - target: ScriptTarget.ES5, |
28 | | - compilerOptions: { |
29 | | - importsNotUsedAsValues: ['remove'], |
30 | | - } |
| 19 | + ...getDefaultCompilerOptions(), |
| 20 | + experimentalDecorators: true, |
| 21 | + jsx: JsxEmit.Preserve, |
| 22 | + module: ModuleKind.CommonJS, |
| 23 | + moduleResolution: ModuleResolutionKind.NodeJs, |
| 24 | + noEmitOnError: false, |
| 25 | + noUnusedLocals: true, |
| 26 | + noUnusedParameters: true, |
| 27 | + stripInternal: true, |
| 28 | + target: ScriptTarget.ES5, |
| 29 | + newLine: NewLineKind.LineFeed |
31 | 30 | } |
32 | 31 |
|
33 | 32 | // Target is ES2015 (same as ES6) |
34 | 33 | const configES6 = { |
35 | | - ...getDefaultCompilerOptions(), |
36 | | - experimentalDecorators: true, |
37 | | - jsx: JsxEmit.Preserve, |
38 | | - noEmitOnError: false, |
39 | | - noUnusedLocals: true, |
40 | | - noUnusedParameters: true, |
41 | | - stripInternal: true, |
42 | | - target: ScriptTarget.ES2015, |
43 | | - compilerOptions: { |
44 | | - importsNotUsedAsValues: ['remove'], |
45 | | - } |
| 34 | + ...getDefaultCompilerOptions(), |
| 35 | + experimentalDecorators: true, |
| 36 | + jsx: JsxEmit.Preserve, |
| 37 | + noEmitOnError: false, |
| 38 | + noUnusedLocals: true, |
| 39 | + noUnusedParameters: true, |
| 40 | + stripInternal: true, |
| 41 | + target: ScriptTarget.ES2015, |
| 42 | + newLine: NewLineKind.LineFeed |
46 | 43 | } |
47 | 44 |
|
48 | 45 | function compile(path: string, callback) { |
49 | | - const files = globSync(path) |
50 | | - const compilerHost = createCompilerHost(config) |
51 | | - const program = createProgram(files, config, compilerHost) |
| 46 | + const files = globSync(path) |
| 47 | + const compilerHost = createCompilerHost(config) |
| 48 | + const program = createProgram(files, config, compilerHost) |
52 | 49 |
|
53 | | - program.emit(undefined, compare(), undefined, undefined, { |
54 | | - before: [transform()], |
55 | | - }) |
| 50 | + program.emit(undefined, compare(), undefined, undefined, { |
| 51 | + after: [transform()], |
| 52 | + }) |
56 | 53 |
|
57 | | - callback(files, 'ES5') |
| 54 | + callback(files, 'ES5') |
58 | 55 | } |
59 | 56 |
|
60 | 57 | function compileES6(path: string, callback) { |
61 | | - const files = globSync(path) |
62 | | - const compilerHost = createCompilerHost(configES6) |
63 | | - const program = createProgram(files, configES6, compilerHost) |
| 58 | + const files = globSync(path) |
| 59 | + const compilerHost = createCompilerHost(configES6) |
| 60 | + const program = createProgram(files, configES6, compilerHost) |
64 | 61 |
|
65 | | - program.emit(undefined, compare('ES6'), undefined, undefined, { |
66 | | - before: [transform()], |
67 | | - }) |
| 62 | + program.emit(undefined, compare('ES6'), undefined, undefined, { |
| 63 | + after: [transform()], |
| 64 | + }) |
68 | 65 |
|
69 | | - callback(files, 'ES6') |
| 66 | + callback(files, 'ES6') |
70 | 67 | } |
71 | 68 |
|
72 | 69 | let failedTestsEs5 = [] |
73 | 70 | let failedTestsEs6 = [] |
74 | 71 | mkdirpSync(resolve(__dirname, 'temp/')) |
75 | 72 | mkdirpSync(resolve(__dirname, 'tempES6/')) |
76 | 73 |
|
77 | | -function compare (target?: string) { |
78 | | - return (filePath: string, output: string) => { |
79 | | - const fileBasename = basename(filePath) |
80 | | - const referenceFilePath = resolve(`${__dirname}`, `references${target ?? ''}/` + fileBasename) |
| 74 | +function compare(target?: string) { |
| 75 | + return (filePath: string, output: string) => { |
| 76 | + const fileBasename = basename(filePath) |
| 77 | + const referenceFilePath = resolve(`${__dirname}`, `references${target ?? ''}/` + fileBasename) |
81 | 78 |
|
82 | | - const tempFilePath = resolve(__dirname, `temp${target ?? ''}/` + fileBasename) |
| 79 | + const tempFilePath = resolve(__dirname, `temp${target ?? ''}/` + fileBasename) |
83 | 80 |
|
84 | | - try { |
85 | | - const fileData = readFileSync(referenceFilePath, 'utf8') |
86 | | - if (fileData !== output) { |
87 | | - writeFileSync(tempFilePath, output, 'utf8'); |
88 | | - (target === 'ES6' ? failedTestsEs6 : failedTestsEs5).push(fileBasename) |
89 | | - } |
90 | | - } catch (error) { |
91 | | - writeFileSync(tempFilePath, output, 'utf8'); |
92 | | - (target === 'ES6' ? failedTestsEs6 : failedTestsEs5).push(fileBasename) |
| 81 | + try { |
| 82 | + const fileData = readFileSync(referenceFilePath, 'utf8') |
| 83 | + if (fileData !== output) { |
| 84 | + writeFileSync(tempFilePath, output, 'utf8'); |
| 85 | + (target === 'ES6' ? failedTestsEs6 : failedTestsEs5).push(fileBasename) |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + writeFileSync(tempFilePath, output, 'utf8'); |
| 89 | + (target === 'ES6' ? failedTestsEs6 : failedTestsEs5).push(fileBasename) |
| 90 | + } |
93 | 91 | } |
94 | | - } |
95 | 92 | } |
96 | 93 |
|
97 | 94 | function printResult(files: string[], target: string) { |
98 | | - console.info(target ? `\n\n${target ?? ''}` : '') |
99 | | - if ((target === 'ES6' ? failedTestsEs6 : failedTestsEs5).length) { |
100 | | - console.log( |
101 | | - `${files.length - (target === 'ES6' ? failedTestsEs6 : failedTestsEs5).length}/${files.length} cases passed` |
102 | | - ) |
103 | | - console.log('Following tests failed:'); |
104 | | - (target === 'ES6' ? failedTestsEs6 : failedTestsEs5).map(test => console.log(test)) |
105 | | - console.log(`Please look in the test/temp${target === 'ES6' ? target : ''} folder and verify output`) |
106 | | - console.log('When verified use the command: npm run overwrite-references') |
107 | | - // throw 'Failed tests...' |
108 | | - } else { |
109 | | - console.log(`All cases (${files.length}) successfully passed`) |
110 | | - } |
| 95 | + console.info(target ? `\n\n${target ?? ''}` : '') |
| 96 | + if ((target === 'ES6' ? failedTestsEs6 : failedTestsEs5).length) { |
| 97 | + console.log( |
| 98 | + `${files.length - (target === 'ES6' ? failedTestsEs6 : failedTestsEs5).length}/${files.length} cases passed` |
| 99 | + ) |
| 100 | + console.log('Following tests failed:'); |
| 101 | + (target === 'ES6' ? failedTestsEs6 : failedTestsEs5).map(test => console.log(test)) |
| 102 | + console.log(`Please look in the test/temp${target === 'ES6' ? target : ''} folder and verify output`) |
| 103 | + console.log('When verified use the command: npm run overwrite-references') |
| 104 | + // throw 'Failed tests...' |
| 105 | + } else { |
| 106 | + console.log(`All cases (${files.length}) successfully passed`) |
| 107 | + } |
111 | 108 | } |
112 | 109 |
|
113 | 110 | function printFinalResult() { |
114 | | - console.info('\n\nResults:') |
115 | | - if (failedTestsEs5.length || failedTestsEs6.length) { |
116 | | - console.log(`${failedTestsEs5.length} ES5 cases failed`) |
117 | | - console.log(`${failedTestsEs6.length} ES6 cases failed`) |
118 | | - } else { |
119 | | - console.log(`All ES5 and ES6 cases successfully passed`) |
120 | | - } |
| 111 | + console.info('\n\nResults:') |
| 112 | + if (failedTestsEs5.length || failedTestsEs6.length) { |
| 113 | + console.log(`${failedTestsEs5.length} ES5 cases failed`) |
| 114 | + console.log(`${failedTestsEs6.length} ES6 cases failed`) |
| 115 | + } else { |
| 116 | + console.log(`All ES5 and ES6 cases successfully passed`) |
| 117 | + } |
121 | 118 | } |
122 | 119 |
|
123 | 120 | console.time('compile time') |
124 | | -compile('tests/cases/*.tsx', printResult) |
125 | | -compileES6('tests/cases/*.tsx', printResult) |
| 121 | +compile('tests/cases/**.tsx', printResult) |
| 122 | +compileES6('tests/cases/**.tsx', printResult) |
126 | 123 | printFinalResult() |
127 | 124 | console.timeEnd('compile time') |
0 commit comments