|
1 | 1 | import { generate } from 'astring'
|
2 |
| -import type { VariableDeclaration } from 'estree' |
3 |
| -import { Chapter } from '../../../types' |
4 |
| -import { type Rule, RuleError } from '../../types' |
5 |
| -import { getVariableDeclarationName } from '../../../utils/ast/astCreator' |
| 2 | +import * as es from 'estree' |
| 3 | + |
| 4 | +import { UNKNOWN_LOCATION } from '../../../constants' |
| 5 | +import { Chapter, ErrorSeverity, ErrorType, Node, Rule, SourceError } from '../../../types' |
6 | 6 |
|
7 | 7 | const mutableDeclarators = ['let', 'var']
|
8 | 8 |
|
9 |
| -export class NoDeclareMutableError extends RuleError<VariableDeclaration> { |
| 9 | +export class NoDeclareMutableError implements SourceError { |
| 10 | + public type = ErrorType.SYNTAX |
| 11 | + public severity = ErrorSeverity.ERROR |
| 12 | + |
| 13 | + constructor(public node: es.VariableDeclaration) {} |
| 14 | + |
| 15 | + get location() { |
| 16 | + return this.node.loc ?? UNKNOWN_LOCATION |
| 17 | + } |
| 18 | + |
10 | 19 | public explain() {
|
11 | 20 | return (
|
12 | 21 | 'Mutable variable declaration using keyword ' + `'${this.node.kind}'` + ' is not allowed.'
|
13 | 22 | )
|
14 | 23 | }
|
15 | 24 |
|
16 | 25 | public elaborate() {
|
17 |
| - const name = getVariableDeclarationName(this.node) |
| 26 | + const name = (this.node.declarations[0].id as es.Identifier).name |
18 | 27 | const value = generate(this.node.declarations[0].init)
|
19 | 28 |
|
20 | 29 | return `Use keyword "const" instead, to declare a constant:\n\n\tconst ${name} = ${value};`
|
21 | 30 | }
|
22 | 31 | }
|
23 | 32 |
|
24 |
| -const noDeclareMutable: Rule<VariableDeclaration> = { |
| 33 | +const noDeclareMutable: Rule<es.VariableDeclaration> = { |
25 | 34 | name: 'no-declare-mutable',
|
26 | 35 | disableFromChapter: Chapter.SOURCE_3,
|
27 |
| - testSnippets: [ |
28 |
| - ['let i = 0;', "Line 1: Mutable variable declaration using keyword 'let' is not allowed."] |
29 |
| - ], |
| 36 | + |
30 | 37 | checkers: {
|
31 |
| - VariableDeclaration(node: VariableDeclaration) { |
| 38 | + VariableDeclaration(node: es.VariableDeclaration, _ancestors: [Node]) { |
32 | 39 | if (mutableDeclarators.includes(node.kind)) {
|
33 | 40 | return [new NoDeclareMutableError(node)]
|
34 | 41 | } else {
|
|
0 commit comments