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

stripComments: remove trailing comma #151941

Merged
merged 2 commits into from Jun 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions build/lib/i18n.js
Expand Up @@ -295,9 +295,10 @@ function stripComments(content) {
// Second group matches a single quoted string
// Third group matches a multi line comment
// Forth group matches a single line comment
const regexp = /("[^"\\]*(?:\\.[^"\\]*)*")|('[^'\\]*(?:\\.[^'\\]*)*')|(\/\*[^\/\*]*(?:(?:\*|\/)[^\/\*]*)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
const result = content.replace(regexp, (match, _m1, _m2, m3, m4) => {
// Only one of m1, m2, m3, m4 matches
// Fifth group matches a trailing comma
const regexp = /("[^"\\]*(?:\\.[^"\\]*)*")|('[^'\\]*(?:\\.[^'\\]*)*')|(\/\*[^\/\*]*(?:(?:\*|\/)[^\/\*]*)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))|(,\s*[}\]])/g;
const result = content.replace(regexp, (match, _m1, _m2, m3, m4, m5) => {
// Only one of m1, m2, m3, m4, m5 matches
if (m3) {
// A block comment. Replace with nothing
return '';
Expand All @@ -313,6 +314,10 @@ function stripComments(content) {
return '';
}
}
else if (m5) {
// Remove the trailing comma
return match.substring(1);
}
else {
// We match a string
return match;
Expand Down
10 changes: 7 additions & 3 deletions build/lib/i18n.ts
Expand Up @@ -412,9 +412,10 @@ function stripComments(content: string): string {
// Second group matches a single quoted string
// Third group matches a multi line comment
// Forth group matches a single line comment
const regexp = /("[^"\\]*(?:\\.[^"\\]*)*")|('[^'\\]*(?:\\.[^'\\]*)*')|(\/\*[^\/\*]*(?:(?:\*|\/)[^\/\*]*)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
const result = content.replace(regexp, (match, _m1: string, _m2: string, m3: string, m4: string) => {
// Only one of m1, m2, m3, m4 matches
// Fifth group matches a trailing comma
const regexp = /("[^"\\]*(?:\\.[^"\\]*)*")|('[^'\\]*(?:\\.[^'\\]*)*')|(\/\*[^\/\*]*(?:(?:\*|\/)[^\/\*]*)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))|(,\s*[}\]])/g;
const result = content.replace(regexp, (match, _m1: string, _m2: string, m3: string, m4: string, m5: string) => {
// Only one of m1, m2, m3, m4, m5 matches
if (m3) {
// A block comment. Replace with nothing
return '';
Expand All @@ -427,6 +428,9 @@ function stripComments(content: string): string {
} else {
return '';
}
} else if (m5) {
// Remove the trailing comma
return match.substring(1);
} else {
// We match a string
return match;
Expand Down
10 changes: 7 additions & 3 deletions src/vs/base/common/stripComments.js
Expand Up @@ -13,16 +13,17 @@
// Second group matches a single quoted string
// Third group matches a multi line comment
// Forth group matches a single line comment
const regexp = /("[^"\\]*(?:\\.[^"\\]*)*")|('[^'\\]*(?:\\.[^'\\]*)*')|(\/\*[^\/\*]*(?:(?:\*|\/)[^\/\*]*)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
// Fifth group matches a trailing comma
const regexp = /("[^"\\]*(?:\\.[^"\\]*)*")|('[^'\\]*(?:\\.[^'\\]*)*')|(\/\*[^\/\*]*(?:(?:\*|\/)[^\/\*]*)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))|(,\s*[}\]])/g;

/**
*
* @param {string} content
* @returns {string}
*/
function stripComments(content) {
return content.replace(regexp, function (match, _m1, _m2, m3, m4) {
// Only one of m1, m2, m3, m4 matches
return content.replace(regexp, function (match, _m1, _m2, m3, m4, m5) {
// Only one of m1, m2, m3, m4, m5 matches
if (m3) {
// A block comment. Replace with nothing
return '';
Expand All @@ -36,6 +37,9 @@
else {
return '';
}
} else if (m5) {
// Remove the trailing comma
return match.substring(1);
} else {
// We match a string
return match;
Expand Down
22 changes: 22 additions & 0 deletions src/vs/base/test/common/stripComments.test.ts
Expand Up @@ -122,4 +122,26 @@ suite('Strip Comments', () => {
].join('\n');
assert.strictEqual(stripComments(content), expected);
});
test('Trailing comma in object', () => {
const content: string = [
"{",
` "a": 10,`,
"}"
].join('\n');
const expected: string = [
"{",
` "a": 10`,
"}"
].join('\n');
assert.strictEqual(stripComments(content), expected);
});
test('Trailing comma in array', () => {
const content: string = [
`[ "a", "b", "c", ]`
].join('\n');
const expected: string = [
`[ "a", "b", "c" ]`
].join('\n');
assert.strictEqual(stripComments(content), expected);
});
});