File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -444,4 +444,9 @@ if (import.meta.vitest) {
444
444
( 0 , eval ) ( `(${ script } )` ) ( import . meta. vitest )
445
445
} )
446
446
}
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!` )
447
452
}
Original file line number Diff line number Diff line change @@ -881,8 +881,22 @@ export function transform(
881
881
parent . superClass = t . identifier ( `Object` )
882
882
} ,
883
883
VariableDeclaration ( { node : variableDeclaration } ) {
884
- if ( variableDeclaration . kind == `const` )
884
+ if ( variableDeclaration . kind == `const` ) {
885
885
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
+ }
886
900
} ,
887
901
ThisExpression : path => {
888
902
path . replaceWith ( t . identifier ( `undefined` ) )
You can’t perform that action at this time.
0 commit comments