Skip to content

Commit

Permalink
Adds a JSON parsing library for tasks.json (#5954)
Browse files Browse the repository at this point in the history
* add json with comments parser for tasks.json
  • Loading branch information
dan-shaw authored Sep 1, 2020
1 parent c90c130 commit ca777dd
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 42 deletions.
43 changes: 20 additions & 23 deletions Extension/ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1057,33 +1057,30 @@ http://www.gnu.org/licenses/lgpl.html

-------------------------------------------------------------------

jsonc-parser 2.2.0 - MIT
https://github.com/microsoft/node-jsonc-parser#readme
Copyright (c) Microsoft
Copyright 2018, Microsoft
Copyright (c) Microsoft Corporation.

The MIT License (MIT)
comment-json 3.0.0 - MIT
https://github.com/kaelzhang/node-comment-json#readme

Copyright (c) Microsoft
Copyright (c) 2013 kaelzhang <>, contributors
http://kael.me/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


-------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions Extension/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ const filter = require('gulp-filter');
const vinyl = require('vinyl');
const parse5 = require('parse5');
const traverse = require('parse5-traverse');
const jsonc = require('jsonc-parser'); // Used to allow comments in nativeStrings.json
const jsonc = require('comment-json'); // Used to allow comments in nativeStrings.json
const crypto = require('crypto');
const https = require('https');


// Patterns to find HTML files
const htmlFilesPatterns = [
"ui/**/*.html"
Expand Down
2 changes: 1 addition & 1 deletion Extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,7 @@
"@typescript-eslint/parser": "^2.19.2",
"async-child-process": "^1.1.1",
"await-notify": "^1.0.1",
"comment-json": "^3.0.3",
"editorconfig": "^0.15.3",
"escape-string-regexp": "^2.0.0",
"eslint": "^6.8.0",
Expand All @@ -2251,7 +2252,6 @@
"gulp-typescript": "^5.0.1",
"http-proxy-agent": "^2.1.0",
"https-proxy-agent": "^2.2.4",
"jsonc-parser": "^2.1.1",
"minimatch": "^3.0.4",
"minimist": "^1.2.5",
"mkdirp": "^0.5.1",
Expand Down
2 changes: 1 addition & 1 deletion Extension/src/Debugger/configurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as logger from '../logger';
import * as nls from 'vscode-nls';

import { IConfiguration, IConfigurationSnippet, DebuggerType, MIConfigurations, WindowsConfigurations, WSLConfigurations, PipeTransportConfigurations } from './configurations';
import { parse } from 'jsonc-parser';
import { parse } from 'comment-json';
import { PlatformInformation } from '../platform';
import { Environment, ParsedEnvironmentFile } from './ParsedEnvironmentFile';

Expand Down
6 changes: 3 additions & 3 deletions Extension/src/LanguageServer/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getCustomConfigProviders } from './customProviders';
import { SettingsPanel } from './settingsPanel';
import * as os from 'os';
import escapeStringRegExp = require('escape-string-regexp');
import * as jsonc from 'jsonc-parser';
import * as jsonc from 'comment-json';
import * as nls from 'vscode-nls';
import which = require('which');

Expand Down Expand Up @@ -950,7 +950,7 @@ export class CppProperties {
settings.update("default.configurationProvider", undefined); // delete the setting
}

await util.writeFileText(fullPathToFile, JSON.stringify(this.configurationJson, null, 4));
await util.writeFileText(fullPathToFile, jsonc.stringify(this.configurationJson, null, 4));

this.propertiesFile = vscode.Uri.file(path.join(this.configFolder, "c_cpp_properties.json"));

Expand Down Expand Up @@ -1617,7 +1617,7 @@ export class CppProperties {
private writeToJson(): void {
console.assert(this.propertiesFile);
if (this.propertiesFile) {
fs.writeFileSync(this.propertiesFile.fsPath, JSON.stringify(this.configurationJson, null, 4));
fs.writeFileSync(this.propertiesFile.fsPath, jsonc.stringify(this.configurationJson, null, 4));
}
}

Expand Down
9 changes: 4 additions & 5 deletions Extension/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { lookupString } from './nativeStrings';
import * as nls from 'vscode-nls';
import { Readable } from 'stream';
import { PackageManager, IPackage } from './packageManager';
import * as jsonc from 'comment-json';

nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
Expand Down Expand Up @@ -71,11 +72,10 @@ export function getRawTasksJson(): Promise<any> {
if (!exists) {
return resolve({});
}
let fileContents: string = fs.readFileSync(path).toString();
fileContents = fileContents.replace(/^\s*\/\/.*$/gm, ""); // Remove start of line // comments.
const fileContents: string = fs.readFileSync(path).toString();
let rawTasks: any = {};
try {
rawTasks = JSON.parse(fileContents);
rawTasks = jsonc.parse(fileContents);
} catch (error) {
return reject(new Error(failedToParseTasksJson));
}
Expand Down Expand Up @@ -116,14 +116,13 @@ export async function ensureBuildTaskExists(taskName: string): Promise<void> {
rawTasksJson.tasks.push(task);
}

// TODO: It's dangerous to overwrite this file. We could be wiping out comments.
const settings: OtherSettings = new OtherSettings();
const tasksJsonPath: string | undefined = getTasksJsonPath();
if (!tasksJsonPath) {
throw new Error("Failed to get tasksJsonPath in ensureBuildTaskExists()");
}

await writeFileText(tasksJsonPath, JSON.stringify(rawTasksJson, null, settings.editorTabSize));
await writeFileText(tasksJsonPath, jsonc.stringify(rawTasksJson, null, settings.editorTabSize));
}

export function fileIsCOrCppSource(file: string): boolean {
Expand Down
24 changes: 17 additions & 7 deletions Extension/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,16 @@ commander@^2.12.1, commander@^2.19.0, commander@^2.20.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==

comment-json@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/comment-json/-/comment-json-3.0.3.tgz#0cadacd6278602b57b8c51b1814dc5d311d228c4"
integrity sha512-P7XwYkC3qjIK45EAa9c5Y3lR7SMXhJqwFdWg3niAIAcbk3zlpKDdajV8Hyz/Y3sGNn3l+YNMl8A2N/OubSArHg==
dependencies:
core-util-is "^1.0.2"
esprima "^4.0.1"
has-own-prop "^2.0.0"
repeat-string "^1.6.1"

comment-parser@^0.7.2:
version "0.7.2"
resolved "https://registry.yarnpkg.com/comment-parser/-/comment-parser-0.7.2.tgz#baf6d99b42038678b81096f15b630d18142f4b8a"
Expand Down Expand Up @@ -1330,7 +1340,7 @@ core-js@^2.4.0:
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.11.tgz#38831469f9922bded8ee21c9dc46985e0399308c"
integrity sha512-5wjnpaT/3dV+XB4borEsnAYQchn00XSgTAWKDkEqv+K8KevjbzmofK6hfJ9TZIlpj2N0xQpazy7PiRQiWHqzWg==

core-util-is@~1.0.0:
core-util-is@^1.0.2, core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=
Expand Down Expand Up @@ -1921,7 +1931,7 @@ espree@^6.1.2:
acorn-jsx "^5.1.0"
eslint-visitor-keys "^1.1.0"

esprima@^4.0.0:
esprima@^4.0.0, esprima@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
Expand Down Expand Up @@ -2642,6 +2652,11 @@ has-flag@^4.0.0:
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==

has-own-prop@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-own-prop/-/has-own-prop-2.0.0.tgz#f0f95d58f65804f5d218db32563bb85b8e0417af"
integrity sha512-Pq0h+hvsVm6dDEa8x82GnLSYHOzNDt7f0ddFa3FqcQlgzEiptPqL+XrOJNavjOzSYiYWIrgeVYYgGlLmnxwilQ==

has-symbols@^1.0.0, has-symbols@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/has-symbols/-/has-symbols-1.0.1.tgz#9f5214758a44196c406d9bd76cebf81ec2dd31e8"
Expand Down Expand Up @@ -3173,11 +3188,6 @@ json5@^1.0.1:
dependencies:
minimist "^1.2.0"

jsonc-parser@^2.1.1:
version "2.2.0"
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-2.2.0.tgz#f206f87f9d49d644b7502052c04e82dd6392e9ef"
integrity sha512-4fLQxW1j/5fWj6p78vAlAafoCKtuBm6ghv+Ij5W2DrDx0qE+ZdEl2c6Ko1mgJNF5ftX1iEWQQ4Ap7+3GlhjkOA==

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
Expand Down

0 comments on commit ca777dd

Please sign in to comment.