Skip to content

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

Merged
Nathan Shively-Sanders (sandersn) merged 3 commits into
masterfrom
use-ad-hoc-code-for-require-nested-destructuring
Sep 10, 2020
Merged

Nathan Shively-Sanders (sandersn) merged 3 commits into
masterfrom
use-ad-hoc-code-for-require-nested-destructuring

Conversation

@sandersn

@sandersn Nathan Shively-Sanders (sandersn) commented Aug 21, 2020 •

Copy link
Copy Markdown
Member

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
@weswigham

Wesley Wigham (weswigham) commented Aug 24, 2020 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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.)

@weswigham Wesley Wigham (weswigham) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Alright.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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