Skip to content

Commit 23a7db4

Browse files
committed
Ensure consistent variable modifiers
'var' is a distinct local or mutable global, 'let' a shared local
1 parent 7ee6e1c commit 23a7db4

33 files changed

+736
-570
lines changed

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tslint/asFormatter.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ export class Formatter extends AbstractFormatter {
2525

2626
mapToMessages(failures: RuleFailure[]): string[] {
2727
return failures.map((failure: RuleFailure) => {
28-
const fileName = failure.getFileName();
29-
const failureString = failure.getFailure();
30-
const ruleName = failure.getRuleName();
31-
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
32-
const positionTuple = `:${lineAndCharacter.line + 1}:${lineAndCharacter.character + 1}`;
28+
var fileName = failure.getFileName();
29+
var failureString = failure.getFailure();
30+
var ruleName = failure.getRuleName();
31+
var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
32+
var positionTuple = `:${lineAndCharacter.line + 1}:${lineAndCharacter.character + 1}`;
3333
if (this.lastSeverity == failure.getRuleSeverity() && this.lastFailure == failureString) {
3434
return " in " + fileName + positionTuple;
3535
} else {
36-
var message = this.lastSeverity ? "\n" : "";
36+
let message = this.lastSeverity ? "\n" : "";
3737
switch (this.lastSeverity = failure.getRuleSeverity()) {
3838
case "warning": {
3939
message += colorYellow + "WARNING:" + colorReset;

lib/tslint/asVariablesRule.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = Object.setPrototypeOf ||
4+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6+
return function (d, b) {
7+
extendStatics(d, b);
8+
function __() { this.constructor = d; }
9+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10+
};
11+
})();
12+
exports.__esModule = true;
13+
var Lint = require("tslint");
14+
var tsutils_1 = require("tsutils");
15+
var Rule = /** @class */ (function (_super) {
16+
__extends(Rule, _super);
17+
function Rule() {
18+
return _super !== null && _super.apply(this, arguments) || this;
19+
}
20+
Rule.prototype.apply = function (sourceFile) {
21+
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
22+
};
23+
Rule.TOP_LEVEL_VAR = "Top-level variable should be 'var' (distinct local or global).";
24+
Rule.BLOCK_LEVEL_LET = "Block-level variable should be 'let' (shared local).";
25+
return Rule;
26+
}(Lint.Rules.AbstractRule));
27+
exports.Rule = Rule;
28+
var DiagnosticsWalker = /** @class */ (function (_super) {
29+
__extends(DiagnosticsWalker, _super);
30+
function DiagnosticsWalker() {
31+
return _super !== null && _super.apply(this, arguments) || this;
32+
}
33+
DiagnosticsWalker.prototype.visitVariableDeclarationList = function (node) {
34+
if (tsutils_1.isVariableStatement(node.parent)) {
35+
if (tsutils_1.isBlock(node.parent.parent)) {
36+
if (tsutils_1.isFunctionScopeBoundary(node.parent.parent.parent) ||
37+
tsutils_1.isNamespaceDeclaration(node.parent.parent.parent)) {
38+
if (tsutils_1.getVariableDeclarationKind(node) == 1 /* Let */) {
39+
this.addFailureAtNode(node, Rule.TOP_LEVEL_VAR);
40+
}
41+
}
42+
else if (tsutils_1.getVariableDeclarationKind(node) == 0 /* Var */) {
43+
this.addFailureAtNode(node, Rule.BLOCK_LEVEL_LET);
44+
}
45+
}
46+
else if (tsutils_1.isSourceFile(node.parent.parent) ||
47+
tsutils_1.isModuleBlock(node.parent.parent)) {
48+
if (tsutils_1.getVariableDeclarationKind(node) == 1 /* Let */) {
49+
this.addFailureAtNode(node, Rule.TOP_LEVEL_VAR);
50+
}
51+
}
52+
else if (tsutils_1.getVariableDeclarationKind(node) == 0 /* Var */) {
53+
this.addFailureAtNode(node, Rule.BLOCK_LEVEL_LET);
54+
}
55+
}
56+
else if (tsutils_1.getVariableDeclarationKind(node) == 0 /* Var */) {
57+
this.addFailureAtNode(node, Rule.BLOCK_LEVEL_LET);
58+
}
59+
_super.prototype.visitVariableDeclarationList.call(this, node);
60+
};
61+
return DiagnosticsWalker;
62+
}(Lint.RuleWalker));

lib/tslint/asVariablesRule.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as ts from "typescript";
2+
import * as Lint from "tslint";
3+
import {
4+
getVariableDeclarationKind,
5+
VariableDeclarationKind,
6+
isVariableStatement,
7+
isBlock,
8+
isFunctionScopeBoundary,
9+
isSourceFile,
10+
isNamespaceDeclaration,
11+
isExportSpecifier,
12+
isModuleBlock
13+
} from "tsutils";
14+
15+
export class Rule extends Lint.Rules.AbstractRule {
16+
17+
static TOP_LEVEL_VAR = "Top-level variable should be 'var' (distinct local or global).";
18+
static BLOCK_LEVEL_LET = "Block-level variable should be 'let' (shared local).";
19+
20+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
21+
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
22+
}
23+
}
24+
25+
class DiagnosticsWalker extends Lint.RuleWalker {
26+
27+
visitVariableDeclarationList(node: ts.VariableDeclarationList): void {
28+
if (isVariableStatement(node.parent)) {
29+
if (isBlock(node.parent.parent)) {
30+
if (
31+
isFunctionScopeBoundary(node.parent.parent.parent) ||
32+
isNamespaceDeclaration(node.parent.parent.parent)
33+
) {
34+
if (getVariableDeclarationKind(node) == VariableDeclarationKind.Let) {
35+
this.addFailureAtNode(node, Rule.TOP_LEVEL_VAR);
36+
}
37+
} else if (getVariableDeclarationKind(node) == VariableDeclarationKind.Var) {
38+
this.addFailureAtNode(node, Rule.BLOCK_LEVEL_LET);
39+
}
40+
} else if (
41+
isSourceFile(node.parent.parent) ||
42+
isModuleBlock(node.parent.parent)
43+
) {
44+
if (getVariableDeclarationKind(node) == VariableDeclarationKind.Let) {
45+
this.addFailureAtNode(node, Rule.TOP_LEVEL_VAR);
46+
}
47+
} else if (getVariableDeclarationKind(node) == VariableDeclarationKind.Var) {
48+
this.addFailureAtNode(node, Rule.BLOCK_LEVEL_LET);
49+
}
50+
} else if (getVariableDeclarationKind(node) == VariableDeclarationKind.Var) {
51+
this.addFailureAtNode(node, Rule.BLOCK_LEVEL_LET);
52+
}
53+
super.visitVariableDeclarationList(node);
54+
}
55+
}

src/ast.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ export abstract class Node {
603603
stmt.members = members; setParent(members, stmt);
604604
stmt.path = path;
605605
if (path) {
606-
var normalizedPath = normalizePath(path.value);
606+
let normalizedPath = normalizePath(path.value);
607607
if (path.value.startsWith(".")) { // relative
608608
stmt.normalizedPath = resolvePath(
609609
normalizedPath,
@@ -1806,7 +1806,7 @@ export function addModifier(modifier: ModifierNode, modifiers: ModifierNode[] |
18061806
/** Gets a specific modifier from the specified set of modifiers. */
18071807
export function getModifier(kind: ModifierKind, modifiers: ModifierNode[] | null): ModifierNode | null {
18081808
if (modifiers) {
1809-
for (var i = 0, k = modifiers.length; i < k; ++i) {
1809+
for (let i = 0, k = modifiers.length; i < k; ++i) {
18101810
if (modifiers[i].modifierKind == kind) {
18111811
return modifiers[i];
18121812
}
@@ -1823,9 +1823,9 @@ export function hasModifier(kind: ModifierKind, modifiers: ModifierNode[] | null
18231823
/** Gets the first decorator by name within at set of decorators, if present. */
18241824
export function getFirstDecorator(name: string, decorators: DecoratorNode[] | null): DecoratorNode | null {
18251825
if (decorators) {
1826-
for (var i = 0, k = decorators.length; i < k; ++i) {
1827-
var decorator = decorators[i];
1828-
var expression = decorator.name;
1826+
for (let i = 0, k = decorators.length; i < k; ++i) {
1827+
let decorator = decorators[i];
1828+
let expression = decorator.name;
18291829
if (expression.kind == NodeKind.IDENTIFIER && (<IdentifierExpression>expression).text == name) {
18301830
return decorator;
18311831
}
@@ -1879,15 +1879,15 @@ export function mangleInternalPath(path: string): string {
18791879

18801880
/** Sets the parent node on an array of nodes. */
18811881
function setParent(nodes: Node[], parent: Node): void {
1882-
for (var i = 0, k = nodes.length; i < k; ++i) {
1882+
for (let i = 0, k = nodes.length; i < k; ++i) {
18831883
nodes[i].parent = parent;
18841884
}
18851885
}
18861886

18871887
/** Sets the parent node on an array of nullable nodes. */
18881888
function setParentIfNotNull(nodes: (Node | null)[], parent: Node): void {
1889-
for (var i = 0, k = nodes.length; i < k; ++i) {
1890-
var node = nodes[i];
1889+
for (let i = 0, k = nodes.length; i < k; ++i) {
1890+
let node = nodes[i];
18911891
if (node) node.parent = parent;
18921892
}
18931893
}

src/builtins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,7 @@ export function compileCall(
19041904
return arg0;
19051905
}
19061906

1907-
var abort = compileAbort(compiler, operands.length == 2 ? operands[1] : null, reportNode);
1907+
let abort = compileAbort(compiler, operands.length == 2 ? operands[1] : null, reportNode);
19081908

19091909
compiler.currentType = type.nonNullableType;
19101910

0 commit comments

Comments
 (0)