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