feedback icon indicating copy to clipboard operation
feedback copied to clipboard

Destructuring Objects At Top Level

Open lsh opened this issue 3 years ago • 3 comments

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.

lsh avatar Mar 16 '21 01:03 lsh

See also talk/203, talk/1127

jrus avatar Mar 17 '21 01:03 jrus

related: https://github.com/observablehq/parser/pull/88

CobusT avatar Aug 16 '21 17:08 CobusT

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.

tomlarkworthy avatar Aug 19 '22 09:08 tomlarkworthy