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

Put error spans deep on nested object literals #25140

Merged
merged 22 commits into from
Jul 4, 2018

Conversation

weswigham
Copy link
Member

@weswigham weswigham commented Jun 21, 2018

And include an additional location pointing back at the deeply nested property in the target type it is being assigned to. Additionally, like in classes which are incompatible with a base type, object and array literals will now flag all locations which are incompatible with the target instead of just one.

Fixes #25030 partially (for all instances involving expressions where we're tracking which properties have issues).

@DanielRosenwasser
Copy link
Member

As a follow-up to today's error sync, let's use checkExpressionForMutableLocation

@weswigham
Copy link
Member Author

@DanielRosenwasser Done. Also fixed the enum and elaboration changes like I said I aught to be able to. Wanna review?

  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 }
  ~~~~~~~~~~~~~
Which is from property '"another"' of type '{ another: A; }', declared here.
Copy link
Member

Choose a reason for hiding this comment

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

Why is "another" in quotes here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because i'm stringifying the literal type associated with the name. It might equally be '0' or [typeof foo]. I can try to strip " from the type's string (if it's a valid identifier post-strip) if you think it'll improve the message.

@@ -3710,6 +3710,11 @@
"code": 6371
},

"Which is from property '{0}' of type '{1}', declared here.": {
Copy link
Member

Choose a reason for hiding this comment

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

The expected type comes from property '{0}' which was declared here on type '{1}'.

or

The expected type '{0}' comes from property '{1}' which was declared here on type '{2}'.

return false;
}

function elaborateError(node: Expression | undefined, source: Type, target: Type): boolean {
Copy link
Member

Choose a reason for hiding this comment

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

shouldElaborateErrorOnProperty

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's not a check - it actually performs the elaboration and adds the new diagnostics - its return type merely indicates weather such an elaboration occurred.

return false;
}

function *generateObjectliteralElements(node: ObjectLiteralExpression): ElaborationIterator {
Copy link
Member

Choose a reason for hiding this comment

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

Capital L

@@ -3710,6 +3710,11 @@
"code": 6371
},

"Which is from property '{0}' of type '{1}', declared here.": {
"category": "Message",
Copy link
Member

Choose a reason for hiding this comment

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

I thought it was "category": "Error"

Copy link
Member Author

Choose a reason for hiding this comment

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

As discussed duing our sync earlier, right now the category is unused for related information - we can put off deciding why they aughta be until we have a story for using them in some principled way (for, eg, elaborations vs explanations)

*
* @return non-quoted string
*/
export function stripQuotes(name: string) {
Copy link
Member

Choose a reason for hiding this comment

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

I feel like we should just be able to call symbolToString and get the right thing in certain cases, and just funnel along a string.

@weswigham
Copy link
Member Author

@DanielRosenwasser I've synced with master, hopefully addressed your reviews, and added a few more tests for good measure. Have anything more you'd like to see?

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

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

Can you also add a test case similar to what you already had, but which uses a contextual indexed access type?

if (!length(node.properties)) return;
for (const prop of node.properties) {
if (isJsxSpreadAttribute(prop)) continue;
yield [prop.name, prop.initializer, getLiteralType(idText(prop.name))];
Copy link
Member

Choose a reason for hiding this comment

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

It's hard to tell what the intent is here; I'd make this an object instead of a tuple.

// Assignability failure - check each prop individually, and if that fails, fall back on the bad error span
let reportedError = false;
for (let status = iterator.next(); !status.done; status = iterator.next()) {
const [prop, next, nameType] = status.value;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe make it clear that you're diving into next; it doesn't seem immediately obvious.

if (!isTypeAssignableTo(sourcePropType, targetPropType)) {
const elaborated = next && elaborateError(next, sourcePropType, targetPropType);
if (elaborated) {
reportedError = true;
Copy link
Member

Choose a reason for hiding this comment

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

I think this can just become a return

Copy link
Member Author

Choose a reason for hiding this comment

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

No, if I were to return here, i would only issue an elaboration on the first property with an issue instead of all of them.

Copy link
Member Author

@weswigham weswigham Jul 3, 2018

Choose a reason for hiding this comment

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

I could continue and un-indent and remove the else if you'd prefer that, however.

}

type ElaborationIterator = IterableIterator<[Node, Expression | undefined, Type]>;
function elaborateElementwise(iterator: ElaborationIterator, source: Type, target: Type) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you write a quick bit about what the intuition should be before I dive into this function?

return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage, containingMessageChain, errorOutputObject);
}

function checkTypeAssignableToAndOptionallyElaborate(source: Type, target: Type, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined): boolean {
Copy link
Member

Choose a reason for hiding this comment

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

Add a little blurb that this performs a structural comparison using a given expression that we can use to provide specific errors on things like properties.

@weswigham
Copy link
Member Author

Can you also add a test case similar to what you already had, but which uses a contextual indexed access type?

Ahhh.... to what are you referring?

@DanielRosenwasser
Copy link
Member

Looks like you do have a case - however, as we discussed offline, the computed property message is a little confusing given that it's not clear it you're erroring on the name.

@weswigham
Copy link
Member Author

@DanielRosenwasser Added; what do you think about the new error?

@@ -1452,6 +1452,10 @@
"category": "Error",
"code": 2417
},
"Type of computed property value is '{0}', which is not assignable to type '{1}'.": {
Copy link
Member

Choose a reason for hiding this comment

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

computed property's

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

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

LGTM apart from the error message suggestion

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.

2 participants