-
-
Notifications
You must be signed in to change notification settings - Fork 217
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
Issue #43 - Customizable operation parse error #174
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Looks good, I've just left a few suggestions that I think will improve the code a bit.
Also, to demonstrate that you've fixed the issue describe in #43, how about adding a test to test-errors.js
?
src/MatchResult.js
Outdated
@@ -171,6 171,31 @@ MatchFailure.prototype.getRightmostFailures = function() { | |||
return this._failures; | |||
}; | |||
|
|||
MatchFailure.prototype.getMessage = function(config) { | |||
var source = this.state.inputStream.source; | |||
if (typeof source !== 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need this if
. If you copied this from somewhere else, it's left over from a time when we matching on arrays as well as strings. Now, we just support strings, so there's no need to check.
src/MatchResult.js
Outdated
@@ -171,6 171,31 @@ MatchFailure.prototype.getRightmostFailures = function() { | |||
return this._failures; | |||
}; | |||
|
|||
MatchFailure.prototype.getMessage = function(config) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add a short comment above this function explaining what options can be passed in the config
arg.
src/MatchResult.js
Outdated
var detail = 'Expected ' this.getExpectedText(); | ||
|
||
// use default values if not defined | ||
var includeSource = typeof config !== 'undefined' && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a cleaner to do this slightly differently. First, rename the argument optConfig
, and then, at the top of the function, add the following:
var config = optConfig || {};
Then, define includeSource
and includeLineNumbers
like so:
var includeLineNumbers = config.includeLineNumbers == null ? true : config.includeLineNumbers;
var includeSource = config.includeSource == null ? true : config.includeSource;
src/Semantics.js
Outdated
@@ -340,7 340,7 @@ function parseSignature(signature, type) { | |||
signature, | |||
type === 'operation' ? 'OperationSignature' : 'AttributeSignature'); | |||
if (r.failed()) { | |||
throw new Error(r.message); | |||
throw new Error(r.getMessage({includeSource: true, includeLineNumbers: true})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want includeLineNumbers
to be false
here?
src/util.js
Outdated
@@ -93,7 93,10 @@ exports.getLineAndColumn = function(str, offset) { | |||
|
|||
// Return a nicely-formatted string describing the line and column for the | |||
// given offset in `str`. | |||
exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) { | |||
|
|||
exports.getOptionalLineAndColumnMessage = function(str, offset, includeLineNumbers /* ranges */) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that the line numbers are optional, I think we could pick a better name for this. Maybe 'describeSourceLocation'?
src/util.js
Outdated
|
||
exports.getOptionalLineAndColumnMessage = function(str, offset, includeLineNumbers /* ranges */) { | ||
|
||
var _includeLineNumbers = typeof includeLineNumbers !== 'undefined' ? includeLineNumbers : true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: it's unusual for local variables to start with _
. Instead, I'd name the argument optIncludeLineNumbers
and then name this variable includeLineNumbers
. Or, to simplify things, we could just make the argument required.
src/util.js
Outdated
@@ -146,3 153,9 @@ exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) { | |||
} | |||
return sb.contents(); | |||
}; | |||
|
|||
exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd get rid of the optional ranges
here. This function can be the simple one that includes the line number and only points to a single offset, and any code that needs more than that can call the other version (which I've suggested you rename to describeSourceLocation
.
…eAndColumnMessage
… that relied on old method
@pdubroy Suggestions were applied. Please review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, just a couple of small changes and it's ready to merge.
@@ -90,49 91,49 @@ test('getLineAndColumnMessage', function(t) { | |||
}); | |||
|
|||
test('getLineAndColumnMessage with ranges', function(t) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should rename this test now, since it's not testing getLineAndColumnMessage
anymore :-)
t.fail('Expected an exception to be thrown'); | ||
} catch (e) { | ||
t.equal(e.message, [ | ||
'Line 1, col 17:', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't appear that this test passes.
ac91299
to
fdddc68
Compare
@pdubroy @alexwarth Please review