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

Destructuring Objects At Top Level #48

Open
lsh opened this issue Mar 16, 2021 · 3 comments
Open

Destructuring Objects At Top Level #48

lsh opened this issue Mar 16, 2021 · 3 comments

Comments

@lsh
Copy link

lsh commented Mar 16, 2021

Problem statement: Destructuring is incredibly useful when writing legible code, especially when the notebook user only wants several methods from a parent library. Currently, if one wants methods { A, B } from library lib, they would write:

// cell 1
lib = require('lib');

// cell 2
A = lib.A;

// cell 3
B = lib.B;

Current workaround:
One way Observable users have achieved similar functionality when dealing with libraries is by creating a separate notebook that serves as a representation of the restructured cells. So assuming the above snippet was stored in a notebook called @lsh/lib, it could be imported as:

import { A, B } from '@lsh/lib';

Alternatively, some users push these management cells to an appendix section.

Prior art:
There is a pull request for this functionality at observablehq/parser#88, however it seems to have been abandoned due to possible lack of backwards compatibility with mutable cells.

Ideal functionality:

The ideal solution would be a single cell that allows:

{ A, B } = require('lib');

however, as is referenced in the parser PR, wrapping the braces in parenthesis might be needed.

({ A, B }) = require('lib');

Originally laid out at talk/4738.

@jrus
Copy link

jrus commented Mar 17, 2021

See also talk/203, talk/1127

@CobusT
Copy link
Collaborator

CobusT commented Aug 16, 2021

related: observablehq/parser#88

@tomlarkworthy
Copy link

I have started working around this with code generation around module objects, because importing notebooks does support destructuring https://observablehq.com/@tomlarkworthy/paste-codegen, using it to generate wrappers like https://observablehq.com/collection/@tomlarkworthy/firebase-modular-sdk
Obviously code-gen is a terrible crutch and a proper support would be greatly prefered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants