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

Ensure that comments are stripped before reading tasks.json file #1075

Merged

Conversation

DustinCampbell
Copy link
Member

Fixes #1074

@DustinCampbell DustinCampbell added this to the 1.6 milestone Dec 22, 2016
@DustinCampbell DustinCampbell changed the base branch from master to release December 22, 2016 18:19
Copy link
Contributor

@rajkumar42 rajkumar42 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@gregg-miskelly gregg-miskelly left a comment

Choose a reason for hiding this comment

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

Otherwise looks good to me.

@@ -357,7 +358,7 @@ function getBuildOperations(tasksJsonPath: string) {
}

const text = buffer.toString();
const tasksJson: tasks.TaskConfiguration = JSON.parse(text);
const tasksJson: tasks.TaskConfiguration = tolerantParse(text);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this have a try/catch around it?

Copy link
Member Author

Choose a reason for hiding this comment

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

It should, but it didn't before. I'll add one.

}

export function tolerantParse(text: string) {
text = stripComments(text);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we ignore the UTF8-bom while we are at it too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we should. Also, I'd like to add tolerance around trailing commas as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK. Added code to be tolerant against both byte order marks and trailing comments in object member or array element lists.

let nextCode = peekPastWhitespace();
if (nextCode === CharCode.closeBrace || nextCode === CharCode.closeBracket) {
parts.push(text.substring(partStart, index - 1));
partStart = index;
Copy link
Contributor

Choose a reason for hiding this comment

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

don't you need to know the index of the comma in the case that peekPastWhitespace did something?

Copy link
Member Author

Choose a reason for hiding this comment

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

peekPastWhitespace doesn't do anything but look ahead. It doesn't modify index

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, never mind. I was thinking that partStart was going to get the wrong value, but this works.

let result = JSON.stringify(json, null, 4);

result.should.equal(expected);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test for something where the extra , is preceeded by whitespace? (ex:

{
   "obj" : { "a": 1 , }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. Done.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just thought of an issue. This won't work when a trailing comma has a comment at the end of the line. Fixing...

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I'm going to let that go for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

It'll require significant refactoring

Copy link
Member Author

Choose a reason for hiding this comment

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

Filed #1076 to track this.

@gregg-miskelly
Copy link
Contributor

LGTM

@DustinCampbell DustinCampbell merged commit 3ff9564 into dotnet:release Dec 22, 2016
@DustinCampbell DustinCampbell deleted the parse-json-with-comments branch January 31, 2017 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants