[Bug]: @babel/plugin-transform-react-constant-elements transforms non constant element in for loop
See original GitHub issueš»
- Would you like to work on a fix?
How are you using Babel?
babel-loader (webpack)
Input code
function Example(){
const nodes = []
for (let i = 0; i < 10; i++) {
const n = i
nodes.push(<div children={n}></div>)
}
return nodes;
}
Configuration file name
babel.config.js
Configuration
module.exports = {
env: {
production: {
plugins: [
'@babel/plugin-transform-react-inline-elements',
'@babel/plugin-transform-react-constant-elements',
['transform-react-remove-prop-types', { removeImport: true }],
],
},
test: {
plugins: [['babel-plugin-webpack-alias', { config: './webpack/path-aliases.config.js' }]],
},
},
plugins: [
[
'@babel/plugin-transform-typescript',
{
allowDeclareFields: true,
isTSX: true,
},
],
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-optional-chaining',
['@babel/plugin-proposal-private-methods', { loose: true }],
['@babel/plugin-proposal-private-property-in-object', { loose: true }],
'@babel/plugin-transform-runtime',
'babel-plugin-generate-i18n-id',
'react-hot-loader/babel',
],
presets: [['@babel/preset-env', { loose: true }], '@babel/preset-react'],
};
Current and expected behavior
When the code is transformed the element is treated as constant when both transform-block-scoping
and transform-react-constant-elements
are used. incorrect vs correct
Environment
System:
OS: macOS 11.4
Binaries:
Node: 14.15.0 - ~/.nvm/versions/node/v14.15.0/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.8 - ~/.nvm/versions/node/v14.15.0/bin/npm
npmPackages:
@babel/core: 7.15.0 => 7.15.0
@babel/plugin-proposal-class-properties: 7.14.5 => 7.14.5
@babel/plugin-proposal-export-default-from: 7.14.5 => 7.14.5
@babel/plugin-proposal-export-namespace-from: 7.14.5 => 7.14.5
@babel/plugin-proposal-nullish-coalescing-operator: 7.14.5 => 7.14.5
@babel/plugin-proposal-object-rest-spread: 7.14.7 => 7.14.7
@babel/plugin-proposal-optional-chaining: 7.14.5 => 7.14.5
@babel/plugin-transform-react-constant-elements: 7.14.5 => 7.14.5
@babel/plugin-transform-react-inline-elements: 7.14.5 => 7.14.5
@babel/plugin-transform-runtime: 7.15.0 => 7.15.0
@babel/preset-env: 7.15.0 => 7.15.0
@babel/preset-react: 7.14.5 => 7.14.5
@babel/preset-typescript: 7.15.0 => 7.15.0
@babel/register: 7.15.3 => 7.15.3
@babel/runtime: 7.15.4 => 7.15.4
babel-eslint: 10.1.0 => 10.1.0
babel-jest: 27.0.6 => 27.0.6
babel-loader: 8.2.2 => 8.2.2
babel-plugin-generate-i18n-id: 1.4.12 => 1.4.12
babel-plugin-istanbul: 6.0.0 => 6.0.0
babel-plugin-transform-react-remove-prop-types: 0.4.24 => 0.4.24
babel-plugin-webpack-alias: 2.1.2 => 2.1.2
eslint: 7.32.0 => 7.32.0
eslint-plugin-babel: 5.3.1 => 5.3.1
jest: 27.0.6 => 27.0.6
webpack: 4.44.2 => 4.44.2
Possible solution
Add an exception for this case here: https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-react-constant-elements/src/index.js#L75
Additional context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Python for loop weird bug [duplicate] - Stack Overflow
I had selected error but to my surprise, it actually works and is able to print all the elements inside the list. But...
Read more >The infamous bug of range-based for loops
In this above example, we played with getting an element of a vector of string, but we would run into the same issue...
Read more >Washing your code: avoid loops - Artem Sapegin
JavaScript has many useful methods to transform and iterate over arrays, ... Or let's find an element in an array with a for...
Read more >Change value of array in for-loop | Apple Developer Forums
When I change the value of an array in a for-loop the array is not saved with this new value. Example: Code Block...
Read more >21 Iteration | R for Data Science - Hadley Wickham
What if only some of the elements are named? What if the names are not unique? Write a function that prints the mean...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Iāve started digging into the code.
I think a better solution would be in
@babel/traverse
, where we setbinding.constant
: if avar
declaration is defined in a loop, itās not a constant (itās reassigned as many times as the loop runs).Iām assigning this to you, since you ticked the āWould you like to work on a fix?ā bug. If you need any help, or you donāt want to work on it anymore, let us know!
If it is the first time that you contribute to Babel, follow these steps: (you need to have
make
andyarn
available on your machine)git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
yarn && make bootstrap
make watch
(ormake build
whenever you change a file)constant-elements
package (onlyinput.js
;output.js
will be automatically generated)@babel/traverse
yarn jest constant-elements
and ``yarn jest traverse` to run the testsoutput.js
files and run the tests againOVERWRITE=true yarn jest constant-elements
and they will be automatically updated.make test
to run all the testsgit push
and open a PR!