1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var ts = require("typescript");
|
4 | var Lint = require("tslint");
|
5 | var utils = require("tsutils/typeguard/2.8");
|
6 | var Ignore = require("./shared/ignore");
|
7 | var check_node_1 = require("./shared/check-node");
|
8 |
|
9 | exports.Rule = check_node_1.createCheckNodeRule(Ignore.checkNodeWithIgnore(checkNode), "Unexpected let, use const instead.");
|
10 | function checkNode(node, ctx) {
|
11 | var variableStatementFailures = checkVariableStatement(node, ctx);
|
12 | var forStatementsFailures = checkForStatements(node, ctx);
|
13 | return {
|
14 | invalidNodes: variableStatementFailures.concat(forStatementsFailures)
|
15 | };
|
16 | }
|
17 | function checkVariableStatement(node, ctx) {
|
18 | if (utils.isVariableStatement(node)) {
|
19 | return checkDeclarationList(node.declarationList, ctx);
|
20 | }
|
21 | return [];
|
22 | }
|
23 | function checkForStatements(node, ctx) {
|
24 | if ((utils.isForStatement(node) ||
|
25 | utils.isForInStatement(node) ||
|
26 | utils.isForOfStatement(node)) &&
|
27 | node.initializer &&
|
28 | utils.isVariableDeclarationList(node.initializer) &&
|
29 | Lint.isNodeFlagSet(node.initializer, ts.NodeFlags.Let)) {
|
30 | return checkDeclarationList(node.initializer, ctx);
|
31 | }
|
32 | return [];
|
33 | }
|
34 | function checkDeclarationList(declarationList, ctx) {
|
35 | if (Lint.isNodeFlagSet(declarationList, ts.NodeFlags.Let)) {
|
36 |
|
37 | var invalidVariableDeclarationNodes = [];
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | var addFix = true;
|
45 | for (var _i = 0, _a = declarationList.declarations; _i < _a.length; _i++) {
|
46 | var variableDeclarationNode = _a[_i];
|
47 | if (!Ignore.shouldIgnorePrefix(variableDeclarationNode, ctx.options, ctx.sourceFile)) {
|
48 | invalidVariableDeclarationNodes.push(check_node_1.createInvalidNode(variableDeclarationNode, addFix
|
49 | ? [
|
50 | new Lint.Replacement(declarationList.getStart(ctx.sourceFile), "let".length, "const")
|
51 | ]
|
52 | : []));
|
53 | addFix = false;
|
54 | }
|
55 | }
|
56 | return invalidVariableDeclarationNodes;
|
57 | }
|
58 | return [];
|
59 | }
|
60 |
|
\ | No newline at end of file |