Skip to content

Use ad-hoc code for nested destructuring in require #40188

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 3 commits into from
Sep 10, 2020

Conversation

sandersn
Copy link
Member

@sandersn sandersn commented Aug 21, 2020

Nested destructuring doesn't really map to ES imports, and I didn't add any code to support it:

const { utils: { nub, intercalate } } = require('./monopackage')

Previously, isRequireVariableDeclaration walked up binding elements until it reached a variable declaration. This change instead only walks up one binding element and stops. Then it's not bound as an alias and uses the checker-only code to produce types for the nested-imported identifiers.

Fixes #40143

Nested destructuring doesn't really map to ES imports:

```js
const { utils: { nub, intercalate } } = require('./monopackage')
```

Previously, isRequireVariableDeclaration walked up binding elements
until it reached a variable declaration. This change instead only walks
up one binding element and stops. Then it's not bound as an alias and
uses the checker-only code to produce types for the nested-imported
identifiers.

Fixes #40143
@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Aug 21, 2020
@weswigham
Copy link
Member

weswigham commented Aug 24, 2020

While you're right in that there's no existing alias declaration that had a similar shape, updating getTargetOfAliasDeclaration to handle arbitrary nesting for locating the alias target symbol seems doable, no? Most of the checker just sees an alias a "a thing the resolves to another thing" - the shape of the declaration isn't particularly important for much other than resolving/serializing it. (Which, for serializing, you can make it into multiple namespace imports, probably, like you've already done for dotted access)

@sandersn
Copy link
Member Author

This pattern is rarely used; it's really only prettier, plus one or two each from eslint, discord and zipkin. I think our time would be a lot better spent by skipping correct emit, falling back to the fake type and asking prettier to test out the beta to see if that's good enough.

(The old fallback needs to stick around for other uses, so fixing this wouldn't let us get rid of it either.)

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

Alright.

@sandersn sandersn merged commit b7c598e into master Sep 10, 2020
@sandersn sandersn deleted the use-ad-hoc-code-for-require-nested-destructuring branch September 10, 2020 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

TS 4.1 should support require with nested binding patterns
3 participants