|
4 | 4 |
|
5 | 5 | import * as path from 'path'; |
6 | 6 | import { fileURLToPath } from 'url'; |
7 | | -import { RunResult, createHelpers } from 'yeoman-test'; |
| 7 | +import { createHelpers } from 'yeoman-test'; |
| 8 | +import { parse } from 'jsonc-parser'; |
8 | 9 | import * as env from '../generators/app/env.js'; |
9 | | -import { cwd } from 'process'; |
10 | | - |
11 | | -function stripComments(content) { |
12 | | - /** |
13 | | - * First capturing group matches double quoted string |
14 | | - * Second matches single quotes string |
15 | | - * Third matches block comments |
16 | | - * Fourth matches line comments |
17 | | - */ |
18 | | - const regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g; |
19 | | - const result = content.replace(regexp, (match, m1, m2, m3, m4) => { |
20 | | - // Only one of m1, m2, m3, m4 matches |
21 | | - if (m3) { |
22 | | - // A block comment. Replace with nothing |
23 | | - return ''; |
24 | | - } else if (m4) { |
25 | | - // A line comment. If it ends in \r?\n then keep it. |
26 | | - const length = m4.length; |
27 | | - if (length > 2 && m4[length - 1] === '\n') { |
28 | | - return m4[length - 2] === '\r' ? '\r\n' : '\n'; |
29 | | - } else { |
30 | | - return ''; |
31 | | - } |
32 | | - } else { |
33 | | - // We match a string |
34 | | - return match; |
35 | | - } |
36 | | - }); |
37 | | - return result; |
38 | | -} |
39 | | - |
40 | 10 |
|
41 | 11 | describe('test code generator', function () { |
42 | 12 | this.timeout(10000); |
@@ -753,7 +723,7 @@ describe('test code generator', function () { |
753 | 723 |
|
754 | 724 | runResult.assertJsonFileContent('testCom/package.json', expectedPackageJSON); |
755 | 725 |
|
756 | | - const tsconfigBody = JSON.parse(stripComments(runResult.fs.read('testCom/tsconfig.json'))); |
| 726 | + const tsconfigBody = parse(runResult.fs.read('testCom/tsconfig.json')); |
757 | 727 | runResult.assertObjectContent(tsconfigBody, expectedTsConfig); |
758 | 728 | } finally { |
759 | 729 | cleanup(runResult); |
@@ -833,7 +803,7 @@ describe('test code generator', function () { |
833 | 803 |
|
834 | 804 | runResult.assertJsonFileContent('testCom/package.json', expectedPackageJSON); |
835 | 805 |
|
836 | | - const tsconfigBody = JSON.parse(stripComments(runResult.fs.read('testCom/tsconfig.json'))); |
| 806 | + const tsconfigBody = parse(runResult.fs.read('testCom/tsconfig.json')); |
837 | 807 | runResult.assertObjectContent(tsconfigBody, expectedTsConfig); |
838 | 808 | } finally { |
839 | 809 | cleanup(runResult); |
@@ -1196,7 +1166,7 @@ describe('test code generator', function () { |
1196 | 1166 | ] |
1197 | 1167 | }; |
1198 | 1168 |
|
1199 | | - const jsconfigBody = JSON.parse(stripComments(runResult.fs.read('testCom/jsconfig.json'))); |
| 1169 | + const jsconfigBody = parse(runResult.fs.read('testCom/jsconfig.json')); |
1200 | 1170 | runResult.assertObjectContent(jsconfigBody, expectedJSConfig); |
1201 | 1171 | } finally { |
1202 | 1172 | cleanup(runResult); |
|
0 commit comments