Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Permit single-line JSON comments #1361

Merged
merged 1 commit into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/cmd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {createWriteStream} from 'fs';

import {fs} from 'mz';
import parseJSON from 'parse-json';
import stripJsonComments from 'strip-json-comments';
import defaultEventToPromise from 'event-to-promise';

import defaultSourceWatcher from '../watcher';
Expand Down Expand Up @@ -71,14 +72,14 @@ export async function getDefaultLocalizedName(
let extensionName: string = manifestData.name;

try {
messageContents = await fs.readFile(messageFile);
messageContents = await fs.readFile(messageFile, {encoding: 'utf-8'});
} catch (error) {
throw new UsageError(
`Error reading messages.json file at ${messageFile}: ${error}`);
}

try {
messageData = parseJSON(String(messageContents), messageFile);
messageData = parseJSON(stripJsonComments(messageContents), messageFile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rob--W Do you mind to add an additional test case for this fix in 'tests/unit/test-cmd/test.build.js'?

} catch (error) {
throw new UsageError(
`Error parsing messages.json ${error}`);
Expand Down
5 changes: 2 additions & 3 deletions src/util/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default async function getValidatedManifest(
let manifestContents;

try {
manifestContents = await fs.readFile(manifestFile);
manifestContents = await fs.readFile(manifestFile, {encoding: 'utf-8'});
} catch (error) {
throw new InvalidManifest(
`Could not read manifest.json file at ${manifestFile}: ${error}`);
Expand All @@ -47,8 +47,7 @@ export default async function getValidatedManifest(
let manifestData;

try {
manifestData = parseJSON(stripJsonComments(manifestContents.toString()),
manifestFile);
manifestData = parseJSON(stripJsonComments(manifestContents), manifestFile);
} catch (error) {
throw new InvalidManifest(
`Error parsing manifest.json at ${manifestFile}: ${error}`);
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test-cmd/test.build.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ describe('build', () => {
);
});

it('handles comments in messages.json', () => {
return withTempDir(
(tmpDir) => {
const messageFileName = path.join(tmpDir.path(), 'messages.json');
fs.writeFileSync(
messageFileName,
`{"extensionName": {
"message": "example extension", // comments
"description": "example with comments"
}
}`
);

return getDefaultLocalizedName({
messageFile: messageFileName,
manifestData: {
name: '__MSG_extensionName__',
version: '0.0.1',
},
})
.then((result) => {
assert.match(result, /example extension/);
});
}
);
});

it('checks locale file for malformed json', () => {
return withTempDir(
(tmpDir) => {
Expand Down