Skip to content

Commit ca777dd

Browse files
authored
Adds a JSON parsing library for tasks.json (#5954)
* add json with comments parser for tasks.json
1 parent c90c130 commit ca777dd

File tree

7 files changed

+47
-42
lines changed

7 files changed

+47
-42
lines changed

Extension/ThirdPartyNotices.txt

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,33 +1057,30 @@ http://www.gnu.org/licenses/lgpl.html
10571057

10581058
-------------------------------------------------------------------
10591059

1060-
jsonc-parser 2.2.0 - MIT
1061-
https://github.com/microsoft/node-jsonc-parser#readme
1062-
Copyright (c) Microsoft
1063-
Copyright 2018, Microsoft
1064-
Copyright (c) Microsoft Corporation.
1065-
1066-
The MIT License (MIT)
1060+
comment-json 3.0.0 - MIT
1061+
https://github.com/kaelzhang/node-comment-json#readme
10671062

1068-
Copyright (c) Microsoft
1063+
Copyright (c) 2013 kaelzhang <>, contributors
1064+
http://kael.me/
10691065

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

1077-
The above copyright notice and this permission notice shall be included in all
1078-
copies or substantial portions of the Software.
1074+
The above copyright notice and this permission notice shall be
1075+
included in all copies or substantial portions of the Software.
10791076

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

10881085

10891086
-------------------------------------------------------------------

Extension/gulpfile.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ const filter = require('gulp-filter');
2020
const vinyl = require('vinyl');
2121
const parse5 = require('parse5');
2222
const traverse = require('parse5-traverse');
23-
const jsonc = require('jsonc-parser'); // Used to allow comments in nativeStrings.json
23+
const jsonc = require('comment-json'); // Used to allow comments in nativeStrings.json
2424
const crypto = require('crypto');
2525
const https = require('https');
2626

27-
2827
// Patterns to find HTML files
2928
const htmlFilesPatterns = [
3029
"ui/**/*.html"

Extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,7 @@
22342234
"@typescript-eslint/parser": "^2.19.2",
22352235
"async-child-process": "^1.1.1",
22362236
"await-notify": "^1.0.1",
2237+
"comment-json": "^3.0.3",
22372238
"editorconfig": "^0.15.3",
22382239
"escape-string-regexp": "^2.0.0",
22392240
"eslint": "^6.8.0",
@@ -2251,7 +2252,6 @@
22512252
"gulp-typescript": "^5.0.1",
22522253
"http-proxy-agent": "^2.1.0",
22532254
"https-proxy-agent": "^2.2.4",
2254-
"jsonc-parser": "^2.1.1",
22552255
"minimatch": "^3.0.4",
22562256
"minimist": "^1.2.5",
22572257
"mkdirp": "^0.5.1",

Extension/src/Debugger/configurationProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as logger from '../logger';
1616
import * as nls from 'vscode-nls';
1717

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

Extension/src/LanguageServer/configurations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { getCustomConfigProviders } from './customProviders';
1616
import { SettingsPanel } from './settingsPanel';
1717
import * as os from 'os';
1818
import escapeStringRegExp = require('escape-string-regexp');
19-
import * as jsonc from 'jsonc-parser';
19+
import * as jsonc from 'comment-json';
2020
import * as nls from 'vscode-nls';
2121
import which = require('which');
2222

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

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

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

@@ -1617,7 +1617,7 @@ export class CppProperties {
16171617
private writeToJson(): void {
16181618
console.assert(this.propertiesFile);
16191619
if (this.propertiesFile) {
1620-
fs.writeFileSync(this.propertiesFile.fsPath, JSON.stringify(this.configurationJson, null, 4));
1620+
fs.writeFileSync(this.propertiesFile.fsPath, jsonc.stringify(this.configurationJson, null, 4));
16211621
}
16221622
}
16231623

Extension/src/common.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { lookupString } from './nativeStrings';
2323
import * as nls from 'vscode-nls';
2424
import { Readable } from 'stream';
2525
import { PackageManager, IPackage } from './packageManager';
26+
import * as jsonc from 'comment-json';
2627

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

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

126-
await writeFileText(tasksJsonPath, JSON.stringify(rawTasksJson, null, settings.editorTabSize));
125+
await writeFileText(tasksJsonPath, jsonc.stringify(rawTasksJson, null, settings.editorTabSize));
127126
}
128127

129128
export function fileIsCOrCppSource(file: string): boolean {

Extension/yarn.lock

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,16 @@ commander@^2.12.1, commander@^2.19.0, commander@^2.20.0:
12401240
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
12411241
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
12421242

1243+
comment-json@^3.0.3:
1244+
version "3.0.3"
1245+
resolved "https://registry.yarnpkg.com/comment-json/-/comment-json-3.0.3.tgz#0cadacd6278602b57b8c51b1814dc5d311d228c4"
1246+
integrity sha512-P7XwYkC3qjIK45EAa9c5Y3lR7SMXhJqwFdWg3niAIAcbk3zlpKDdajV8Hyz/Y3sGNn3l+YNMl8A2N/OubSArHg==
1247+
dependencies:
1248+
core-util-is "^1.0.2"
1249+
esprima "^4.0.1"
1250+
has-own-prop "^2.0.0"
1251+
repeat-string "^1.6.1"
1252+
12431253
comment-parser@^0.7.2:
12441254
version "0.7.2"
12451255
resolved "https://registry.yarnpkg.com/comment-parser/-/comment-parser-0.7.2.tgz#baf6d99b42038678b81096f15b630d18142f4b8a"
@@ -1330,7 +1340,7 @@ core-js@^2.4.0:
13301340
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.11.tgz#38831469f9922bded8ee21c9dc46985e0399308c"
13311341
integrity sha512-5wjnpaT/3dV+XB4borEsnAYQchn00XSgTAWKDkEqv+K8KevjbzmofK6hfJ9TZIlpj2N0xQpazy7PiRQiWHqzWg==
13321342

1333-
core-util-is@~1.0.0:
1343+
core-util-is@^1.0.2, core-util-is@~1.0.0:
13341344
version "1.0.2"
13351345
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
13361346
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=
@@ -1921,7 +1931,7 @@ espree@^6.1.2:
19211931
acorn-jsx "^5.1.0"
19221932
eslint-visitor-keys "^1.1.0"
19231933

1924-
esprima@^4.0.0:
1934+
esprima@^4.0.0, esprima@^4.0.1:
19251935
version "4.0.1"
19261936
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
19271937
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
@@ -2642,6 +2652,11 @@ has-flag@^4.0.0:
26422652
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
26432653
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==
26442654

2655+
has-own-prop@^2.0.0:
2656+
version "2.0.0"
2657+
resolved "https://registry.yarnpkg.com/has-own-prop/-/has-own-prop-2.0.0.tgz#f0f95d58f65804f5d218db32563bb85b8e0417af"
2658+
integrity sha512-Pq0h+hvsVm6dDEa8x82GnLSYHOzNDt7f0ddFa3FqcQlgzEiptPqL+XrOJNavjOzSYiYWIrgeVYYgGlLmnxwilQ==
2659+
26452660
has-symbols@^1.0.0, has-symbols@^1.0.1:
26462661
version "1.0.1"
26472662
resolved "https://registry.yarnpkg.com/has-symbols/-/has-symbols-1.0.1.tgz#9f5214758a44196c406d9bd76cebf81ec2dd31e8"
@@ -3173,11 +3188,6 @@ json5@^1.0.1:
31733188
dependencies:
31743189
minimist "^1.2.0"
31753190

3176-
jsonc-parser@^2.1.1:
3177-
version "2.2.0"
3178-
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-2.2.0.tgz#f206f87f9d49d644b7502052c04e82dd6392e9ef"
3179-
integrity sha512-4fLQxW1j/5fWj6p78vAlAafoCKtuBm6ghv+Ij5W2DrDx0qE+ZdEl2c6Ko1mgJNF5ftX1iEWQQ4Ap7+3GlhjkOA==
3180-
31813191
jsonfile@^4.0.0:
31823192
version "4.0.0"
31833193
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"

0 commit comments

Comments
 (0)