swc: [@swc/core@1.3.7 + ] index out of bounds: the len is 1 but the index is 1

Describe the bug

thread '<unnamed>' panicked at 'index out of bounds: the len is 1 but the index is 1', /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/swc_common-0.29.6/src/syntax_pos/hygiene.rs

env: typescript v4.8.4

in @swc/core v1.3.6 ok

Input code

// 
class HuluCssModules extends Visitor {
  visitImportDeclaration(expression) {
     return expression;
  }      
}

exports.default = HuluCssModules;


### Config

```json
{
            test: /\.(js|mjs|jsx|ts|tsx)$/,
            exclude: /node_modules/,
            use: {
                loader: require.resolve('swc-loader'),
                options: {
                    parseMap: true,
                    sync: true,
                    jsc: {
                        parser: {
                            syntax: 'typescript',
                            tsx: true
                        },
                        transform: {
                            react: {
                                runtime: 'automatic',
                                throwIfNamespace: true,
                                development: devMode,
                                useBuiltins: false,
                                refresh: true
                            }
                        }
                    },
                    plugin: (m) => new HuluCssModules().visitProgram(m)
                }
            }
        }


### Playground link

_No response_

### Expected behavior

compile no error

### Actual behavior

_No response_

### Version

v1.3.10

### Additional context

_No response_

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 33 (14 by maintainers)

Commits related to this issue

Most upvoted comments

So what is actually the issue here? I’m seeing the “index out of bounds” in my build and came here to find a fix or at least a workaround. I read “this encourages wrong usage”, so I would like to know what I’m doing wrong. I also read “Fix would be trivial” so I assume a root cause is found, but I don’t see it, is is the default exports? How would a PR to fix this look like?

For anyone still encountering the issue: it may not be the issue from @swc/core itself; it may be due to the plugins.

I tried the 1.3.58 version of @swc/core, and there is no export {xxx}; in my project, but I still got the “index out of bounds” message. But when I pasted my code to the SWC playground, it worked well. There’s also no issue when I use 1.3.7 version in the playground.

Then I removed all plugins in my project and found it worked… After several tries, I found that both options.plugin and options.jsc.experimental.plugins can cause this issue.

I’m not familiar with Rust so I didn’t find the root cause, but removing all SWC plugins is an acceptable solution for me.

JS plugins will be removed in v2, and it’s deprecated.

I get this with a blank plugin in JS just extending Visitor (class MyPlugin extends Visitor { }). How does this “encourage wrong usage”? The only way to not cause this error (on my machine) is to not use any JS plugins.

I didn’t close it because I would accept PR

Just to add also

this cause the error

import {  display  } from './display'


const randomId = () => { 
  return display()
}

export { display }
export { randomId }

Solution

import {  display  } from './display'

export const randomId = () => { 
  return display()
}

export { display } from './display' 

@rene-leanix transform(Module) will also be removed when we can make a breaking change.

Thanks no worries for now i will use version <= 1.3.6 👍

@aelbore Not sure. Fix would be trivial but I don’t want to use my time for this