File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -444,4 +444,9 @@ if (import.meta.vitest) {
444444 ( 0 , eval ) ( `(${ script } )` ) ( import . meta. vitest )
445445 } )
446446 }
447+
448+ const reassignmentTestSource = `export default () => { const i = 0; i = 1; }`
449+ expect ( async ( ) : Promise < any > => await processScript ( reassignmentTestSource , { scriptName : true , minify : false } ) )
450+ . rejects
451+ . toThrowError ( `Reassignment to const variable i is not allowed!` )
447452}
Original file line number Diff line number Diff line change @@ -881,8 +881,22 @@ export function transform(
881881 parent . superClass = t . identifier ( `Object` )
882882 } ,
883883 VariableDeclaration ( { node : variableDeclaration } ) {
884- if ( variableDeclaration . kind == `const` )
884+ if ( variableDeclaration . kind == `const` ) {
885885 variableDeclaration . kind = `let`
886+ variableDeclaration . extra = {
887+ ...variableDeclaration . extra ,
888+ usedToBeConst : true ,
889+ }
890+ }
891+ } ,
892+ AssignmentExpression ( { node : assignment , scope } ) {
893+ const lhs = assignment . left
894+ if ( lhs . type != `Identifier` ) return
895+
896+ const binding = scope . getBinding ( lhs . name )
897+ if ( binding ?. path ?. parentPath ?. node ?. extra ?. usedToBeConst ) {
898+ throw new Error ( `Reassignment to const variable ${ lhs . name } is not allowed!` ) ;
899+ }
886900 } ,
887901 ThisExpression : path => {
888902 path . replaceWith ( t . identifier ( `undefined` ) )
You can’t perform that action at this time.
0 commit comments