Skip to content

Commit

Permalink
Add const reassign rule (#5388)
Browse files Browse the repository at this point in the history
* Add const reassign rule

* Update src/utils/logs.ts

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>

* Update tests

---------

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
TrickyPi and lukastaegert committed Feb 15, 2024
1 parent f74d0a9 commit d6fd383
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ast/nodes/AssignmentExpression.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import { logConstVariableReassignError } from '../../utils/logs';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
Expand Down Expand Up @@ -79,6 +80,12 @@ export default class AssignmentExpression extends NodeBase {
}

initialise(): void {
if (this.left instanceof Identifier) {
const variable = this.scope.variables.get(this.left.name);
if (variable?.kind === 'const') {
this.scope.context.error(logConstVariableReassignError(), this.left.start);
}
}
this.left.setAssignedValue(this.right);
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const ADDON_ERROR = 'ADDON_ERROR',
CHUNK_INVALID = 'CHUNK_INVALID',
CIRCULAR_DEPENDENCY = 'CIRCULAR_DEPENDENCY',
CIRCULAR_REEXPORT = 'CIRCULAR_REEXPORT',
CONST_REASSIGN = 'CONST_REASSIGN',
CYCLIC_CROSS_CHUNK_REEXPORT = 'CYCLIC_CROSS_CHUNK_REEXPORT',
DEPRECATED_FEATURE = 'DEPRECATED_FEATURE',
DUPLICATE_ARGUMENT_NAME = 'DUPLICATE_ARGUMENT_NAME',
Expand Down Expand Up @@ -312,6 +313,13 @@ export function logDeprecation(
};
}

export function logConstVariableReassignError() {
return {
code: CONST_REASSIGN,
message: 'Cannot reassign a variable declared with `const`'
};
}

export function logDuplicateArgumentNameError(name: string): RollupLog {
return { code: DUPLICATE_ARGUMENT_NAME, message: `Duplicate argument name "${name}"` };
}
Expand Down
21 changes: 21 additions & 0 deletions test/function/samples/error-const-reassign/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require('node:path');
const MAIN_ID = path.resolve(__dirname, 'main.js');

module.exports = defineTest({
description: 'Cannot reassign a variable declared with `const`',
error: {
code: 'CONST_REASSIGN',
frame: `
1: const foo = 1;\n2: foo = 2;\n ^
`,
id: MAIN_ID,
loc: {
column: 0,
file: MAIN_ID,
line: 2
},
message: 'Cannot reassign a variable declared with `const`',
pos: 15,
watchFiles: [MAIN_ID]
}
});
2 changes: 2 additions & 0 deletions test/function/samples/error-const-reassign/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 1;
foo = 2;

0 comments on commit d6fd383

Please sign in to comment.