Skip to content

Commit acef712

Browse files
hiikezoerpl
authored andcommitted
fix: Allow comments in manifest.json (#1294)
1 parent d4d436e commit acef712

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"sign-addon": "0.3.0",
7676
"source-map-support": "0.5.3",
7777
"stream-to-promise": "2.2.0",
78+
"strip-json-comments": "2.0.1",
7879
"tmp": "0.0.33",
7980
"update-notifier": "2.3.0",
8081
"watchpack": "1.5.0",

src/util/manifest.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'path';
33

44
import {fs} from 'mz';
55
import parseJSON from 'parse-json';
6+
import stripJsonComments from 'strip-json-comments';
67

78
import {InvalidManifest} from '../errors';
89
import {createLogger} from './logger';
@@ -46,7 +47,8 @@ export default async function getValidatedManifest(
4647
let manifestData;
4748

4849
try {
49-
manifestData = parseJSON(manifestContents, manifestFile);
50+
manifestData = parseJSON(stripJsonComments(manifestContents.toString()),
51+
manifestFile);
5052
} catch (error) {
5153
throw new InvalidManifest(
5254
`Error parsing manifest.json at ${manifestFile}: ${error}`);

tests/unit/test-util/test.manifest.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,38 @@ describe('util/manifest', () => {
158158
}
159159
));
160160

161+
it('allows comments in manifest JSON', () =>
162+
withTempDir(async (tmpDir) => {
163+
const manifestWithComments = `{
164+
"name": "the extension",
165+
"version": "0.0.1" // comments
166+
}`;
167+
const manifestFile = path.join(tmpDir.path(), 'manifest.json');
168+
await fs.writeFile(manifestFile, manifestWithComments);
169+
const manifestData = await getValidatedManifest(tmpDir.path());
170+
171+
assert.deepEqual(manifestData, manifestWithoutApps);
172+
})
173+
);
174+
175+
it('reports an error with line number in manifest JSON with comments', () =>
176+
withTempDir(async (tmpDir) => {
177+
const invalidManifestWithComments = `{
178+
// a comment in its own line
179+
// another comment on its own line
180+
"name": "I'm an invalid JSON Manifest
181+
}`;
182+
const manifestFile = path.join(tmpDir.path(), 'manifest.json');
183+
await fs.writeFile(manifestFile, invalidManifestWithComments);
184+
const promise = getValidatedManifest(tmpDir.path());
185+
186+
const error = await assert.isRejected(promise, InvalidManifest);
187+
await assert.isRejected(promise, /Error parsing manifest\.json at /);
188+
assert.include(error.message, 'in JSON at position 133');
189+
assert.include(error.message, manifestFile);
190+
})
191+
);
192+
161193
});
162194

163195
describe('getManifestId', () => {

0 commit comments

Comments
 (0)