Skip to content

[ConstPlugin] use RestElement in ObjectPattern will got error #17189

@chenyulun

Description

@chenyulun

Bug report

What is the current behavior?

Module parse failed: Cannot read properties of undefined (reading 'type')
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
TypeError: Cannot read properties of undefined (reading 'type')
    at collectDeclaration (/Users/chenyulun390/learn/testwebpack/node_modules/webpack/lib/ConstPlugin.js:26:16)
    at getHoistedDeclarations (/Users/chenyulun390/learn/testwebpack/node_modules/webpack/lib/ConstPlugin.js:110:7)
    at /Users/chenyulun390/learn/testwebpack/node_modules/webpack/lib/ConstPlugin.js:196:25
    at Hook.eval [as call] (eval at create (/Users/chenyulun390/learn/testwebpack/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:16)
    at Hook.CALL_DELEGATE [as _call] (/Users/chenyulun390/learn/testwebpack/node_modules/tapable/lib/Hook.js:14:14)
    at JavascriptParser.walkIfStatement (/Users/chenyulun390/learn/testwebpack/node_modules/webpack/lib/javascript/JavascriptParser.js:1805:41)
    at JavascriptParser.walkStatement (/Users/chenyulun390/learn/testwebpack/node_modules/webpack/lib/javascript/JavascriptParser.js:1736:10)
    at JavascriptParser.walkStatements (/Users/chenyulun390/learn/testwebpack/node_modules/webpack/lib/javascript/JavascriptParser.js:1613:9)
    at JavascriptParser.parse (/Users/chenyulun390/learn/testwebpack/node_modules/webpack/lib/javascript/JavascriptParser.js:3593:9)
    at /Users/chenyulun390/learn/testwebpack/node_modules/webpack/lib/NormalModule.js:1092:26

webpack 5.82.0 compiled with 1 error in 98 ms

If the current behavior is a bug, please provide the steps to reproduce.

// index.js
var test = {
  a: 1,
  b: 2,
  c: 3
};
if (process.env.NODE_ENV === 'development') {
  var {
    a,
    ...other
  } = test;
  console.log('development:', other);
} else {
  var {
    b,
    ..._other, // error here
  } = test;
  console.log('production:', _other);
}

What is the expected behavior?
image

Do it need to add a non empty judgment here?

for (const property of node.properties) {
+ if(property.value) {
    stack.push(property.value);
+ }
}

Other relevant information:
webpack version: 5.82.0
Node.js version: v16.17.0
Operating System: macOS
Additional tools:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions