kentcdodds / kentcdodds/babel-plugin-macros

Namespace import crashes the plugin

Open
#111 9 comments 0 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
JavaScript
Stars
2.6k
Forks
138
PR merge metrics
No merged PRs in 30d

Description

* `babel-plugin-macros` version: 2.5.1

Relevant code or config

```javascript
import * as foo from 'foo.macro'
```

What happened:
Compiler fails with the following message
```
./src/App.jsx
TypeError: Cannot read property 'name' of undefined
at Array.map ()
```

Problem description:
`ImportNamespaceSpecifier` node is not handled properly here and the error message is not helpful. Offending code:
https://github.com/kentcdodds/babel-plugin-macros/blob/e0ebca780856be76cbe1c5841d09a922fdde533b/src/index.js#L78-L80

Suggested solution:
`ImportNamespaceSpecifier` should either be handled as `default` case or as a separate `namespace` case. Or maybe both variants are valid and there should be a config option akin to `allowSyntheticDefaultImports` in TypeScript

Quick fix:
```es6
(s.type === 'ImportDefaultSpecifier' || s.type === 'ImportNamespaceSpecifier')
? 'default'
: s.imported.name,
```

Contributor guide

Open the contributing guide

Research direction

Start with the reproduction in the issue and inspect src/index.js at the linked lines 78-80, focusing on how ImportNamespaceSpecifier is handled. Compare the proposed default and namespace behaviors, then verify that the namespace import no longer crashes and produces a useful result or error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.