ast-types icon indicating copy to clipboard operation
ast-types copied to clipboard

exportSpecifier builder and probably others are broken in ^0.14.0

Open jedwards1211 opened this issue 3 years ago • 5 comments

I don't fully understand what's supposed to happen when a node type is defined in two places (esprima and es6) but it seems to be causing problems for exportSpecifier:

> var t = require('ast-types').builders
undefined
> t.exportSpecifier()
Uncaught:
Error: no value or default function given for field "exported" of ExportSpecifier("id": Identifier | null, "name": Identifier | null)
    at addParam (/Users/andy/temp/node_modules/ast-types/lib/types.js:443:27)
    at /Users/andy/temp/node_modules/ast-types/lib/types.js:477:21
    at Array.forEach (<anonymous>)
    at Object.builder [as exportSpecifier] (/Users/andy/temp/node_modules/ast-types/lib/types.js:475:46)
> t.exportSpecifier(t.identifier('foo'), t.identifier('bar'))
Uncaught:
Error: no value or default function given for field "exported" of ExportSpecifier("id": Identifier | null, "name": Identifier | null)
    at addParam (/Users/andy/temp/node_modules/ast-types/lib/types.js:443:27)
    at /Users/andy/temp/node_modules/ast-types/lib/types.js:477:21
    at Array.forEach (<anonymous>)
    at Object.builder [as exportSpecifier] (/Users/andy/temp/node_modules/ast-types/lib/types.js:475:46)

Can't reproduce this issue in 0.13.4.

I noticed that field names id and name come from the esprima defs, whereas the field name exported comes from the es6 defs.

jedwards1211 avatar Oct 06 '20 20:10 jedwards1211

Oh I see now what the problem is: in 0.13.4, ExportSpecifier was declared in the babel defs which come after esprima.

But ExportSpecifier was moved to the es6 defs in 0.14.0, which come before esprima.

There's probably a problem with some of the other defs that got moved. Seems like the ordering is tricky. Should all the es6+ defs be moved after esprima?

jedwards1211 avatar Oct 06 '20 20:10 jedwards1211

I've got this problem as well

what is the best way to fix it? downgrade for now?

is there a changelog for this?

sibelius avatar Nov 04 '20 19:11 sibelius

I downgraded for the time being.

If you look at https://github.com/benjamn/ast-types/blob/master/main.ts, you can see how you could copy this code into your own bootstrapper and tweak the available defs/ordering -- assuming you're not using a tool like jscodeshift which imports the main module.

jedwards1211 avatar Nov 04 '20 23:11 jedwards1211

where is a good place to add these regressions tests?

sibelius avatar Nov 05 '20 16:11 sibelius

You can use the .from function as a workaround for it

j.exportSpecifier.from({
  exported: j.identifier('something'),
  local: j.identifier('something'), // Optional
})

fersilva16 avatar Jan 08 '22 00:01 fersilva16