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

Issue #43 - Customizable operation parse error #174

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

kyuweftea
Copy link

@pdubroy @alexwarth Please review

Copy link
Contributor

@pdubroy pdubroy left a 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?

@@ -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') {
Copy link
Contributor

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.

@@ -171,6 +171,31 @@ MatchFailure.prototype.getRightmostFailures = function() {
return this._failures;
};

MatchFailure.prototype.getMessage = function(config) {
Copy link
Contributor

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.

var detail = 'Expected ' + this.getExpectedText();

// use default values if not defined
var includeSource = typeof config !== 'undefined' &&
Copy link
Contributor

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}));
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 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 */) {
Copy link
Contributor

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;
Copy link
Contributor

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 */) {
Copy link
Contributor

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.

@kyuweftea
Copy link
Author

@pdubroy Suggestions were applied. Please review.

Copy link
Contributor

@pdubroy pdubroy left a 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) {
Copy link
Contributor

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:',
Copy link
Contributor

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.

@pdubroy pdubroy force-pushed the main branch 4 times, most recently from ac91299 to fdddc68 Compare March 4, 2023 21:55
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