Skip to content

improve property access completion in markup #1381

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

Merged
merged 19 commits into from
Feb 16, 2022

Conversation

jasonlyu123
Copy link
Member

Related to #538, #786 and #1302

This PR added a utility to check if there's trailing property access behind an expression. The Reason we needed this is that we blank the operators before parsing. So the operator won't exist in the AST. We need to check it so that the expression transformation also include it.

This should only be called with an expression that is a direct child of a template node. And since we only go backwards from the end of the moustache (}), it only makes sense to call this with the expression that might end directly before the end of the moustache.

I also change the v2 transformation for {@debug}, {@const} and {@await} to have a better sourcemap. I can't find a way to make {@debug} and {@const} work with the transform function. So I change it to directly overwrite.

I have also experimented, but not committed yet, with skipping ) while blanking operators before parsing here:

} else if (!/\s/.test(char)) {

- } else if (!/\s/.test(char)) {
+ } else if (char !== ')' && !/\s/.test(char)) {

This would let the property access also work in a function call and the key of each block. Haven't thought of a downside for this. Tested and it works well and the existing test still passed. Wondering if I should do this in this PR or a separate one.

@@ -5,7 +5,7 @@ if(hello){
hello;
}}
{ try { const $$_value = await (aPromise); { const foo = $$_value;
const hello = foo;
const hello = foo;
Copy link
Member Author

Choose a reason for hiding this comment

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

The existing test mostly is just whitespaces difference.

* Usually it's there because of the preprocess we have before svelte parses
* the template
*/
export function getNodeEndIncludingTrailingPropertyAccess(
Copy link
Member Author

Choose a reason for hiding this comment

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

Should I import from the v1 utils? I found there's some utility copied from v1. Maybe the plan is to remove v1 eventually this way it's easier to remove?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah the plan is to remove v1 at some point, so it's okay to duplicate it here

* Usually it's there because of the preprocess we have before svelte parses
* the template
*/
export function getNodeEndIncludingTrailingPropertyAccess(
Copy link
Member Author

Choose a reason for hiding this comment

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

Wondering if the name of this function is too long.

Copy link
Member

@dummdidumm dummdidumm Feb 11, 2022

Choose a reason for hiding this comment

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

What about getOriginalNodeEnd? Edit: After having written my other comment I think this would not be correct, since it only handles property access, not other cases. Maybe withTrailingPropertyAccess?

@@ -115,6 +115,51 @@ function test(useNewTransformation: boolean) {
});
});

const editingTestPositionsNewOnly: Array<[number, number, string]> = [
Copy link
Member Author

Choose a reason for hiding this comment

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

These four doesn't work in v1. Wondering if it still makes sense to make it work.

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't worry too much about the old transformation at this point, so I'm fine with it only working for the new transformation

): number {
let index = position;

while (index < originalText.length) {
Copy link
Member

Choose a reason for hiding this comment

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

Could we simplify this to originalText.indexOf('}', position) - 1 ? This is always used in a situation where there's a closing bracket of a mustache tag at the end. Or is this wrong because we only can do this for property assignments and not things like +, & etc which we also blank?
(having written this out, I think so, so I think it's fine as it is now)

Copy link
Member Author

@jasonlyu123 jasonlyu123 Feb 12, 2022

Choose a reason for hiding this comment

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

Yeah, it's currently only used for . and ?.. Not other operators we blanked. This condition for the while loop is mostly for preventing an infinite loop. Just so it's safer than while(true). In most cases, it would hit other characters and break the loop.

@dummdidumm
Copy link
Member

Looks good to me, left a couple of small comments. About the ): Can't think of a drawback either, but maybe we should do it in a separate PR.

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

I shortened the name adjusted the transform util which makes the catch overwrite obsolete and as a nice side effect ensures document symbols are correct in more cases now. Good to merge now 👍

@dummdidumm dummdidumm merged commit ec51f91 into sveltejs:master Feb 16, 2022
@jasonlyu123 jasonlyu123 deleted the editing-member-access branch February 16, 2022 10:01
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