Skip to content

Commit f50bf28

Browse files
authored
Use jsonc to parse nls manifest (#838)
* remove pre-commit hooks * use jsonc to parse manifests fixes #833 * revert using jsonc for package.json * Update src/test/fixtures/nls/package.json
1 parent 1492bca commit f50bf28

File tree

6 files changed

+35
-21
lines changed

6 files changed

+35
-21
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"commander": "^6.1.0",
4545
"glob": "^7.0.6",
4646
"hosted-git-info": "^4.0.2",
47+
"jsonc-parser": "^3.2.0",
4748
"leven": "^3.1.0",
4849
"markdown-it": "^12.3.2",
4950
"mime": "^1.3.4",
@@ -90,4 +91,4 @@
9091
"watch-files": "src/**",
9192
"spec": "src/test/**/*.ts"
9293
}
93-
}
94+
}

src/package.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import { detectYarn, getDependencies } from './npm';
2424
import * as GitHost from 'hosted-git-info';
2525
import parseSemver from 'parse-semver';
26+
import * as jsonc from 'jsonc-parser';
2627

2728
const MinimatchOptions: minimatch.IOptions = { dot: true };
2829

@@ -1323,7 +1324,8 @@ export function readManifest(cwd = process.cwd(), nls = true): Promise<Manifest>
13231324
try {
13241325
return Promise.resolve(JSON.parse(manifestStr));
13251326
} catch (e) {
1326-
return Promise.reject(`Error parsing 'package.json' manifest file: not a valid JSON file.`);
1327+
console.error(`Error parsing 'package.json' manifest file: not a valid JSON file.`);
1328+
throw e;
13271329
}
13281330
})
13291331
.then(validateManifest);
@@ -1337,9 +1339,10 @@ export function readManifest(cwd = process.cwd(), nls = true): Promise<Manifest>
13371339
.catch<string>(err => (err.code !== 'ENOENT' ? Promise.reject(err) : Promise.resolve('{}')))
13381340
.then<ITranslations>(raw => {
13391341
try {
1340-
return Promise.resolve(JSON.parse(raw));
1342+
return Promise.resolve(jsonc.parse(raw));
13411343
} catch (e) {
1342-
return Promise.reject(`Error parsing JSON manifest translations file: ${manifestNLSPath}`);
1344+
console.error(`Error parsing JSON manifest translations file: ${manifestNLSPath}`);
1345+
throw e;
13431346
}
13441347
});
13451348

src/test/fixtures/nls/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
"variables": {
9191
"PickProcess": "extension.pickNodeProcess"
9292
},
93-
"aiKey": "AIF-d9b70cd4-b9f9-4d70-929b-a071c400b217",
9493
"initialConfigurations": "extension.provideInitialConfigurations",
9594
"configurationAttributes": {
9695
"launch": {

src/test/fixtures/nls/package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"node.label": "Node.js",
55

6+
// Example comment
67
"node.sourceMaps.description": "Use JavaScript source maps (if they exist).",
78
"node.outDir.description": "If source maps are enabled, the generated code is expected in this directory. Deprecated: use 'outFiles' attribute instead.",
89
"node.outFiles.description": "If source maps are enabled, these glob patterns specify the generated JavaScript files. If a pattern starts with '!' the files are excluded. If not specified, the generated code is expected in the same directory as its source.",

src/test/package.test.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { spawnSync } from 'child_process';
2323
import { XMLManifest, parseXmlManifest, parseContentTypes } from '../xml';
2424
import { flatten } from '../util';
2525
import { validatePublisher } from '../validation';
26+
import * as jsonc from 'jsonc-parser';
2627

2728
// don't warn in tests
2829
console.warn = () => null;
@@ -244,28 +245,26 @@ describe('collect', function () {
244245
});
245246

246247
describe('readManifest', () => {
247-
it('should patch NLS', () => {
248+
it('should patch NLS', async function () {
248249
const cwd = fixture('nls');
249-
const raw = require(path.join(cwd, 'package.json'));
250-
const translations = require(path.join(cwd, 'package.nls.json'));
250+
const raw = JSON.parse(await fs.promises.readFile(path.join(cwd, 'package.json'), 'utf8'));
251+
const translations = jsonc.parse(await fs.promises.readFile(path.join(cwd, 'package.nls.json'), 'utf8'));
252+
const manifest = await readManifest(cwd);
251253

252-
return readManifest(cwd).then((manifest: any) => {
253-
assert.strictEqual(manifest.name, raw.name);
254-
assert.strictEqual(manifest.description, translations['extension.description']);
255-
assert.strictEqual(manifest.contributes.debuggers[0].label, translations['node.label']);
256-
});
254+
assert.strictEqual(manifest.name, raw.name);
255+
assert.strictEqual(manifest.description, translations['extension.description']);
256+
assert.strictEqual(manifest.contributes!.debuggers[0].label, translations['node.label']);
257257
});
258258

259-
it('should not patch NLS if required', () => {
259+
it('should not patch NLS if required', async function () {
260260
const cwd = fixture('nls');
261-
const raw = require(path.join(cwd, 'package.json'));
262-
const translations = require(path.join(cwd, 'package.nls.json'));
261+
const raw = JSON.parse(await fs.promises.readFile(path.join(cwd, 'package.json'), 'utf8'));
262+
const translations = jsonc.parse(await fs.promises.readFile(path.join(cwd, 'package.nls.json'), 'utf8'));
263+
const manifest = await readManifest(cwd, false);
263264

264-
return readManifest(cwd, false).then((manifest: any) => {
265-
assert.strictEqual(manifest.name, raw.name);
266-
assert.notEqual(manifest.description, translations['extension.description']);
267-
assert.notEqual(manifest.contributes.debuggers[0].label, translations['node.label']);
268-
});
265+
assert.strictEqual(manifest.name, raw.name);
266+
assert.notStrictEqual(manifest.description, translations['extension.description']);
267+
assert.notStrictEqual(manifest.contributes!.debuggers[0].label, translations['node.label']);
269268
});
270269
});
271270

0 commit comments

Comments
 (0)