1
0
mirror of https://github.com/fluencelabs/assemblyscript synced 2025-06-17 17:01:37 +00:00
Files
.github
bin
cli
dist
examples
lib
lint
formatters
rules
internal
asTypesRule.js
asVariablesRule.js
src
README.md
base.json
index.json
package.json
loader
parse
webpack
media
scripts
snap
src
std
tests
.gitattributes
.gitignore
.travis.yml
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
NOTICE
README.md
index.d.ts
index.js
package-lock.json
package.json
tsconfig-base.json
tsconfig-docs.json
tslint.json
webpack.config.js
assemblyscript/lib/lint/rules/asVariablesRule.js

43 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-03-13 14:03:57 +01:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Lint = require("tslint");
const tsutils_1 = require("tsutils");
class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile) {
return this.applyWithWalker(new VariablesWalker(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).";
exports.Rule = Rule;
class VariablesWalker extends Lint.RuleWalker {
visitVariableDeclarationList(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.visitVariableDeclarationList(node);
}
}