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

editor/code: Enable --noUncheckedIndexedAccess & --noPropertyAccessFromIndexSignature ts option #15160

Merged

Conversation

tetsuharuohzeki
Copy link
Contributor

This enables typescript's these option:


Additionally, to enable --noUncheckedIndexedAccess easily, this pull request introduces option-t as a dependency instead of defining a function in this repository like unwrapUndefinable() .

I'll remove it and define them byself if our dependency management policy is that to avoid to add a new package as possible.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 28, 2023
@@ -3,6 +3,7 @@
"compilerOptions": {
"esModuleInterop": false,
"module": "commonjs",
"moduleResolution": "Node16",
Copy link
Member

Choose a reason for hiding this comment

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

Should this be node16 as in the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, there is some reasons:

  1. Future enhancement. This mode (node16) is more natural behavior with the current Node.js' package resolution than node (the current default value). And package.json's exports would be a wide spreading. I think it's better to enable as early as possible.
  2. Compatibility with option-t that depends on exports field too.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry -- I meant node16 instead of Node16, with a lower-case n.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Mysteriously), it works in either. TypeScript compiler use lower-case but vscode's intellisense uses CamelCase.
I'll address it to lower-case.

Copy link
Member

Choose a reason for hiding this comment

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

I guess either is fine then as long as nothing complains.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I pushed the addressed commit: f780810

@lnicola
Copy link
Member

lnicola commented Jun 28, 2023

LGTM except for the noUncheckedIndexedAccess commit, which feels really heavy-handed. But I guess I'm not opposed to that either.

r? @Veykril

@tetsuharuohzeki tetsuharuohzeki force-pushed the enable-noUncheckedIndexedAccess branch from bc9df53 to d1faf5d Compare June 28, 2023 09:39
@lowr
Copy link
Contributor

lowr commented Jun 28, 2023

I'm in favor of noUncheckedIndexedAccess, but I feel we can do better by considering the context instead of unconditionally "unwrapping" nullables. Unwrapping after checking array length/property existence/etc. seems redundant. For example, imo

// simplified for brevity
if (folders.length === 1) {
    const folder = unwrapUndefinable(folders[0]);
    return folder.uri.fsPath;
} else if (folders.length > 1) {
    const folder = unwrapUndefinable(folders[0]);
    // comments for justification
    return folder.uri.fsPath;
} else {
    return "";
}

should be

// comments for justification
const folder = folders[0];
return folder != null ? folder.uri.fsPath : "";

(I'm aware you mostly preserved the existing code, but we can always make them better ✨ )

Also, I'd rather not pull in another dependency for a utility function or two, but I'll leave the decision to others.

@tetsuharuohzeki tetsuharuohzeki force-pushed the enable-noUncheckedIndexedAccess branch 2 times, most recently from 597c7e5 to bf2a15a Compare June 28, 2023 16:19
@tetsuharuohzeki
Copy link
Contributor Author

I addressed @lowr 's comment.

@Veykril How do you think about this?

@Veykril
Copy link
Member

Veykril commented Jul 3, 2023

Also, I'd rather not pull in another dependency for a utility function or two, but I'll leave the decision to others.

I agree here, we only use a single function from that library if I see this right, so if we can't get rid of the call locations for that, can we just hoist that function out into the local source here and drop the dependency?

@tetsuharuohzeki tetsuharuohzeki force-pushed the enable-noUncheckedIndexedAccess branch from bf2a15a to edf14df Compare July 4, 2023 16:00
@tetsuharuohzeki
Copy link
Contributor Author

@Veykril

I agree here, we only use a single function from that library if I see this right, so if we can't get rid of the call locations for that, can we just hoist that function out into the local source here and drop the dependency?

Sure. I addressed them and rebased my changeset again.

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

One small question, but otherwise this should be good for now. Ideally we should get rid of the unwraps where we can, but most of them seem to be due to ts not being smart enough (like array length checks into indexing still resulting in undefined types)

@@ -129,7 +130,8 @@ export function matchingBrace(ctx: CtxInit): Cmd {
),
});
editor.selections = editor.selections.map((sel, idx) => {
const active = client.protocol2CodeConverter.asPosition(response[idx]);
const mayBeActive = client.protocol2CodeConverter.asPosition(response[idx]);
Copy link
Member

Choose a reason for hiding this comment

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

This confuses me, asPosition has the following overload asPosition(value: code.Position): proto.Position; which is what applies here, so why do we unwrap it. It shouldn't be undefined on the type level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Veykril

At here, response[idx] should be a ls.Position | undefined. Then TypeScript selects asPosition(value: ls.Position | undefined | null): code.Position | undefined; overload than asPosition(value: ls.Position): code.Position;.
https://github.com/microsoft/vscode-languageserver-node/blob/2041784436fed53f4e77267a49396bca22a7aacf/client/src/common/protocolConverter.ts#L34-L36

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll update here to:

         editor.selections = editor.selections.map((sel, idx) => {
-            const mayBeActive = client.protocol2CodeConverter.asPosition(response[idx]);
-            const active = unwrapUndefinable(mayBeActive);
+            const position = unwrapUndefinable(response[idx]);
+            const active = client.protocol2CodeConverter.asPosition(position);
             const anchor = sel.isEmpty ? active : sel.anchor;
             return new vscode.Selection(anchor, active);
         });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Oh i missed the indexing into the array, that explains it sorry. Typescript inference is weird, given it tells me that indexing results in a non-undefined type ...
Nevertheless this change is better, thanks

@Veykril
Copy link
Member

Veykril commented Jul 6, 2023

Thanks
@bors r+

@bors
Copy link
Collaborator

bors commented Jul 6, 2023

📌 Commit f708453 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jul 6, 2023

⌛ Testing commit f708453 with merge 537f9b3...

@bors
Copy link
Collaborator

bors commented Jul 6, 2023

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 537f9b3 to master...

@bors bors merged commit 537f9b3 into rust-lang:master Jul 6, 2023
10 checks passed
@tetsuharuohzeki tetsuharuohzeki deleted the enable-noUncheckedIndexedAccess branch July 6, 2023 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants