assemblyscript/lib/tslint/asVariablesRule.js
dcodeIO 23a7db4dc3 Ensure consistent variable modifiers
'var' is a distinct local or mutable global, 'let' a shared local
2018-03-13 02:32:10 +01:00

63 lines
2.8 KiB
JavaScript

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
var Lint = require("tslint");
var tsutils_1 = require("tsutils");
var Rule = /** @class */ (function (_super) {
__extends(Rule, _super);
function Rule() {
return _super !== null && _super.apply(this, arguments) || this;
}
Rule.prototype.apply = function (sourceFile) {
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
};
Rule.TOP_LEVEL_VAR = "Top-level variable should be 'var' (distinct local or global).";
Rule.BLOCK_LEVEL_LET = "Block-level variable should be 'let' (shared local).";
return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
var DiagnosticsWalker = /** @class */ (function (_super) {
__extends(DiagnosticsWalker, _super);
function DiagnosticsWalker() {
return _super !== null && _super.apply(this, arguments) || this;
}
DiagnosticsWalker.prototype.visitVariableDeclarationList = function (node) {
if (tsutils_1.isVariableStatement(node.parent)) {
if (tsutils_1.isBlock(node.parent.parent)) {
if (tsutils_1.isFunctionScopeBoundary(node.parent.parent.parent) ||
tsutils_1.isNamespaceDeclaration(node.parent.parent.parent)) {
if (tsutils_1.getVariableDeclarationKind(node) == 1 /* Let */) {
this.addFailureAtNode(node, Rule.TOP_LEVEL_VAR);
}
}
else if (tsutils_1.getVariableDeclarationKind(node) == 0 /* Var */) {
this.addFailureAtNode(node, Rule.BLOCK_LEVEL_LET);
}
}
else if (tsutils_1.isSourceFile(node.parent.parent) ||
tsutils_1.isModuleBlock(node.parent.parent)) {
if (tsutils_1.getVariableDeclarationKind(node) == 1 /* Let */) {
this.addFailureAtNode(node, Rule.TOP_LEVEL_VAR);
}
}
else if (tsutils_1.getVariableDeclarationKind(node) == 0 /* Var */) {
this.addFailureAtNode(node, Rule.BLOCK_LEVEL_LET);
}
}
else if (tsutils_1.getVariableDeclarationKind(node) == 0 /* Var */) {
this.addFailureAtNode(node, Rule.BLOCK_LEVEL_LET);
}
_super.prototype.visitVariableDeclarationList.call(this, node);
};
return DiagnosticsWalker;
}(Lint.RuleWalker));