mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 06:21:29 +00:00
Give some love to the linter
This commit is contained in:
41
lib/lint/README.md
Normal file
41
lib/lint/README.md
Normal file
@ -0,0 +1,41 @@
|
||||
 lint
|
||||
======================
|
||||
|
||||
Recommended [TSLint](https://github.com/palantir/tslint) rules for use with AssemblyScript. Meant to spot the most common issues as you type.
|
||||
|
||||
Not a sophisticated checker in its current state.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
Add the following `tslint.json` to your project:
|
||||
|
||||
```json
|
||||
{
|
||||
"extends": "@assemblyscript/lint"
|
||||
}
|
||||
```
|
||||
|
||||
Add additional rules if necessary.
|
||||
|
||||
Add a script to your `package.json`:
|
||||
|
||||
```json
|
||||
"scripts": {
|
||||
"lint": "tslint -c tslint.json --project ./path/to[/tsconfig.json] --format as"
|
||||
}
|
||||
```
|
||||
|
||||
Now, to check your sources, run:
|
||||
|
||||
```
|
||||
$> npm run lint
|
||||
```
|
||||
|
||||
If you are using [Visual Studio Code](https://code.visualstudio.com/), there's also a [TSLint extension](https://marketplace.visualstudio.com/items?itemName=eg2.tslint) that highlights issues as you type.
|
||||
|
||||
Custom rules
|
||||
------------
|
||||
|
||||
* **as-types** checks that all types are annotated or have an initializer.
|
||||
* **as-variables** checks the use of `var` and `let` to match their semantic meaning. For reference, `var` becomes a distinct local or mutable global, while `let` becomes a shared local.
|
118
lib/lint/base.json
Normal file
118
lib/lint/base.json
Normal file
@ -0,0 +1,118 @@
|
||||
{
|
||||
"rules": {
|
||||
"adjacent-overload-signatures": {},
|
||||
"ban-types": {
|
||||
"severity": "error",
|
||||
"options": [
|
||||
["object", "Not supported."],
|
||||
["any", "Not supported."],
|
||||
["undefined", "Not supported."],
|
||||
["never", "Not supported."],
|
||||
["number", "Use one of the WebAssembly types instead."],
|
||||
["boolean", "Use `bool` instead."]
|
||||
]
|
||||
},
|
||||
"object-literal-shorthand": {
|
||||
"severity": "error",
|
||||
"options": ["never"]
|
||||
},
|
||||
"restrict-plus-operands": {
|
||||
"severity": "error"
|
||||
},
|
||||
"curly": {
|
||||
"options": ["ignore-same-line"]
|
||||
},
|
||||
"deprecation": {},
|
||||
"encoding": {
|
||||
"severity": "error"
|
||||
},
|
||||
"eofline": {},
|
||||
"label-position": {
|
||||
"severity": "error"
|
||||
},
|
||||
"new-parens": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-any": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-arg": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-consecutive-blank-lines": {},
|
||||
"no-debugger": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-default-export": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-duplicate-imports": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-duplicate-super": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-duplicate-switch-case": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-duplicate-variable": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-eval": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-inferred-empty-object-type": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-internal-module": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-invalid-template-strings": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-invalid-this": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-irregular-whitespace": {},
|
||||
"no-misused-new": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-object-literal-type-assertion": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-parameter-properties": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-require-imports": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-shadowed-variable": {},
|
||||
"no-sparse-arrays": {},
|
||||
"no-string-literal": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-string-throw": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-trailing-whitespace": {},
|
||||
"no-unbound-method": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-unsafe-any": {
|
||||
"severity": "error"
|
||||
},
|
||||
"no-unused-variable": {
|
||||
"options": [{
|
||||
"ignore-pattern": "^_"
|
||||
}]
|
||||
},
|
||||
"no-void-expression": {
|
||||
"severity": "error"
|
||||
},
|
||||
"prefer-method-signature": {},
|
||||
"radix": {},
|
||||
"semicolon": {
|
||||
"options": ["always"]
|
||||
}
|
||||
}
|
||||
}
|
50
lib/lint/formatters/asFormatter.js
Normal file
50
lib/lint/formatters/asFormatter.js
Normal file
@ -0,0 +1,50 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const abstractFormatter_1 = require("tslint/lib/language/formatter/abstractFormatter");
|
||||
const colorBlue = "\u001b[93m";
|
||||
const colorYellow = "\u001b[93m";
|
||||
const colorRed = "\u001b[91m";
|
||||
const colorReset = "\u001b[0m";
|
||||
class Formatter extends abstractFormatter_1.AbstractFormatter {
|
||||
format(failures) {
|
||||
return `${this.mapToMessages(failures).join("\n")}\n`;
|
||||
}
|
||||
mapToMessages(failures) {
|
||||
return failures.map((failure) => {
|
||||
var fileName = failure.getFileName();
|
||||
var failureString = failure.getFailure();
|
||||
var ruleName = failure.getRuleName();
|
||||
var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
|
||||
var positionTuple = `:${lineAndCharacter.line + 1}:${lineAndCharacter.character + 1}`;
|
||||
if (this.lastSeverity == failure.getRuleSeverity() && this.lastFailure == failureString) {
|
||||
return " in " + fileName + positionTuple;
|
||||
}
|
||||
else {
|
||||
let message = this.lastSeverity ? "\n" : "";
|
||||
switch (this.lastSeverity = failure.getRuleSeverity()) {
|
||||
case "warning": {
|
||||
message += colorYellow + "WARNING:" + colorReset;
|
||||
break;
|
||||
}
|
||||
case "error": {
|
||||
message += colorRed + "ERROR:" + colorReset;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
message += failure.getRuleSeverity();
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.lastFailure = failureString;
|
||||
return message + " " + failureString + " [" + ruleName + "]\n in " + fileName + positionTuple;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Formatter.metadata = {
|
||||
formatterName: "as",
|
||||
description: "AssemblyScript's TSLint formatter.",
|
||||
sample: "Similar to ASC's output.",
|
||||
consumer: "human",
|
||||
};
|
||||
exports.Formatter = Formatter;
|
11
lib/lint/index.json
Normal file
11
lib/lint/index.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "./base.json",
|
||||
"rulesDirectory": ["./rules", "./rules/internal"],
|
||||
"formattersDirectory": ["./formatters"],
|
||||
"rules": {
|
||||
"as-types": {
|
||||
"severity": "error"
|
||||
},
|
||||
"as-variables": {}
|
||||
}
|
||||
}
|
21
lib/lint/package.json
Normal file
21
lib/lint/package.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "@assemblyscript/lint",
|
||||
"version": "1.0.0",
|
||||
"main": "index.json",
|
||||
"scripts": {
|
||||
"build": "tsc --project ./src --outDir . --diagnostics"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"tslint": "^5.9.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^2.7.2"
|
||||
},
|
||||
"files": [
|
||||
"index.json",
|
||||
"package.json",
|
||||
"README.md",
|
||||
"rules/",
|
||||
"formatters/"
|
||||
]
|
||||
}
|
58
lib/lint/rules/asTypesRule.js
Normal file
58
lib/lint/rules/asTypesRule.js
Normal file
@ -0,0 +1,58 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const ts = require("typescript");
|
||||
const Lint = require("tslint");
|
||||
class Rule extends Lint.Rules.AbstractRule {
|
||||
apply(sourceFile) {
|
||||
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
|
||||
}
|
||||
}
|
||||
Rule.MISSING_TYPE_OR_INITIALIZER = "Missing type or initializer.";
|
||||
Rule.MISSING_RETURN_TYPE = "Missing return type.";
|
||||
exports.Rule = Rule;
|
||||
class DiagnosticsWalker extends Lint.RuleWalker {
|
||||
visitVariableDeclaration(node) {
|
||||
var list = node.parent;
|
||||
if (list) {
|
||||
let stmt = list.parent;
|
||||
if (stmt && stmt.kind != ts.SyntaxKind.ForOfStatement) {
|
||||
this.checkTypeOrInitializer(node);
|
||||
}
|
||||
}
|
||||
super.visitVariableDeclaration(node);
|
||||
}
|
||||
visitPropertyDeclaration(node) {
|
||||
this.checkTypeOrInitializer(node);
|
||||
super.visitPropertyDeclaration(node);
|
||||
}
|
||||
visitParameterDeclaration(node) {
|
||||
this.checkTypeOrInitializer(node);
|
||||
super.visitParameterDeclaration(node);
|
||||
}
|
||||
checkTypeOrInitializer(node) {
|
||||
if (!node.type && !node.initializer) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
|
||||
}
|
||||
}
|
||||
visitFunctionDeclaration(node) {
|
||||
this.checkFunctionReturnType(node);
|
||||
super.visitFunctionDeclaration(node);
|
||||
}
|
||||
visitArrowFunction(node) {
|
||||
this.checkFunctionReturnType(node);
|
||||
super.visitArrowFunction(node);
|
||||
}
|
||||
visitMethodDeclaration(node) {
|
||||
this.checkFunctionReturnType(node);
|
||||
super.visitMethodDeclaration(node);
|
||||
}
|
||||
visitGetAccessor(node) {
|
||||
this.checkFunctionReturnType(node);
|
||||
super.visitGetAccessor(node);
|
||||
}
|
||||
checkFunctionReturnType(node) {
|
||||
if (!node.type) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_RETURN_TYPE);
|
||||
}
|
||||
}
|
||||
}
|
42
lib/lint/rules/asVariablesRule.js
Normal file
42
lib/lint/rules/asVariablesRule.js
Normal file
@ -0,0 +1,42 @@
|
||||
"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);
|
||||
}
|
||||
}
|
36
lib/lint/rules/internal/asInternalCaseRule.js
Normal file
36
lib/lint/rules/internal/asInternalCaseRule.js
Normal file
@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const ts = require("typescript");
|
||||
const Lint = require("tslint");
|
||||
const tsutils_1 = require("tsutils");
|
||||
class Rule extends Lint.Rules.AbstractRule {
|
||||
apply(sourceFile) {
|
||||
return this.applyWithWalker(new CaseWalker(sourceFile, this.getOptions()));
|
||||
}
|
||||
}
|
||||
Rule.NOT_BRACED = "Multi-line case clauses should be braced.";
|
||||
exports.Rule = Rule;
|
||||
class CaseWalker extends Lint.RuleWalker {
|
||||
visitDefaultClause(node) {
|
||||
this.checkDefaultOrCaseClause(node);
|
||||
super.visitDefaultClause(node);
|
||||
}
|
||||
visitCaseClause(node) {
|
||||
this.checkDefaultOrCaseClause(node);
|
||||
super.visitCaseClause(node);
|
||||
}
|
||||
checkDefaultOrCaseClause(node) {
|
||||
var count = node.statements.length;
|
||||
if (count > 1) {
|
||||
this.addFailureAtNode(node, Rule.NOT_BRACED);
|
||||
}
|
||||
else if (count == 1) {
|
||||
let stmt = node.statements[0];
|
||||
if (stmt.kind != ts.SyntaxKind.Block) {
|
||||
if (!tsutils_1.isSameLine(node.getSourceFile(), node.getStart(), stmt.getStart())) {
|
||||
this.addFailureAtNode(node, Rule.NOT_BRACED);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
lib/lint/rules/internal/asInternalDiagnosticsRule.js
Normal file
23
lib/lint/rules/internal/asInternalDiagnosticsRule.js
Normal file
@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const ts = require("typescript");
|
||||
const Lint = require("tslint");
|
||||
const tsutils_1 = require("tsutils");
|
||||
class Rule extends Lint.Rules.AbstractRule {
|
||||
apply(sourceFile) {
|
||||
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
|
||||
}
|
||||
}
|
||||
Rule.NOT_ON_SEPARATE_LINE = "Diagnostic message not on a separate line.";
|
||||
exports.Rule = Rule;
|
||||
class DiagnosticsWalker extends Lint.RuleWalker {
|
||||
visitPropertyAccessExpression(node) {
|
||||
if (node.expression.kind === ts.SyntaxKind.Identifier) {
|
||||
if (node.expression.text == "DiagnosticCode" &&
|
||||
tsutils_1.isSameLine(node.getSourceFile(), node.parent.getStart(), node.getStart())) {
|
||||
this.addFailureAtNode(node, Rule.NOT_ON_SEPARATE_LINE);
|
||||
}
|
||||
}
|
||||
super.visitPropertyAccessExpression(node);
|
||||
}
|
||||
}
|
68
lib/lint/src/rules/asTypesRule.ts
Normal file
68
lib/lint/src/rules/asTypesRule.ts
Normal file
@ -0,0 +1,68 @@
|
||||
import * as ts from "typescript";
|
||||
import * as Lint from "tslint";
|
||||
|
||||
export class Rule extends Lint.Rules.AbstractRule {
|
||||
|
||||
static MISSING_TYPE_OR_INITIALIZER = "Missing type or initializer.";
|
||||
static MISSING_RETURN_TYPE = "Missing return type.";
|
||||
|
||||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
||||
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
|
||||
}
|
||||
}
|
||||
|
||||
class DiagnosticsWalker extends Lint.RuleWalker {
|
||||
|
||||
visitVariableDeclaration(node: ts.VariableDeclaration) {
|
||||
var list = node.parent;
|
||||
if (list) {
|
||||
let stmt = list.parent;
|
||||
if (stmt && stmt.kind != ts.SyntaxKind.ForOfStatement) {
|
||||
this.checkTypeOrInitializer(node);
|
||||
}
|
||||
}
|
||||
super.visitVariableDeclaration(node);
|
||||
}
|
||||
|
||||
visitPropertyDeclaration(node: ts.PropertyDeclaration) {
|
||||
this.checkTypeOrInitializer(node);
|
||||
super.visitPropertyDeclaration(node);
|
||||
}
|
||||
|
||||
visitParameterDeclaration(node: ts.ParameterDeclaration) {
|
||||
this.checkTypeOrInitializer(node);
|
||||
super.visitParameterDeclaration(node);
|
||||
}
|
||||
|
||||
private checkTypeOrInitializer(node: ts.NamedDeclaration & { type?: ts.TypeNode, initializer?: ts.Expression }) {
|
||||
if (!node.type && !node.initializer) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
|
||||
}
|
||||
}
|
||||
|
||||
visitFunctionDeclaration(node: ts.FunctionDeclaration) {
|
||||
this.checkFunctionReturnType(node);
|
||||
super.visitFunctionDeclaration(node);
|
||||
}
|
||||
|
||||
visitArrowFunction(node: ts.ArrowFunction) {
|
||||
this.checkFunctionReturnType(node);
|
||||
super.visitArrowFunction(node);
|
||||
}
|
||||
|
||||
visitMethodDeclaration(node: ts.MethodDeclaration) {
|
||||
this.checkFunctionReturnType(node);
|
||||
super.visitMethodDeclaration(node);
|
||||
}
|
||||
|
||||
visitGetAccessor(node: ts.GetAccessorDeclaration) {
|
||||
this.checkFunctionReturnType(node);
|
||||
super.visitGetAccessor(node);
|
||||
}
|
||||
|
||||
private checkFunctionReturnType(node: ts.FunctionLikeDeclaration) {
|
||||
if (!node.type) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_RETURN_TYPE);
|
||||
}
|
||||
}
|
||||
}
|
@ -18,11 +18,11 @@ export class Rule extends Lint.Rules.AbstractRule {
|
||||
static BLOCK_LEVEL_LET = "Block-level variable should be 'let' (shared local).";
|
||||
|
||||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
||||
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
|
||||
return this.applyWithWalker(new VariablesWalker(sourceFile, this.getOptions()));
|
||||
}
|
||||
}
|
||||
|
||||
class DiagnosticsWalker extends Lint.RuleWalker {
|
||||
class VariablesWalker extends Lint.RuleWalker {
|
||||
|
||||
visitVariableDeclarationList(node: ts.VariableDeclarationList): void {
|
||||
if (isVariableStatement(node.parent)) {
|
@ -7,11 +7,11 @@ export class Rule extends Lint.Rules.AbstractRule {
|
||||
static NOT_BRACED = "Multi-line case clauses should be braced.";
|
||||
|
||||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
||||
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
|
||||
return this.applyWithWalker(new CaseWalker(sourceFile, this.getOptions()));
|
||||
}
|
||||
}
|
||||
|
||||
class DiagnosticsWalker extends Lint.RuleWalker {
|
||||
class CaseWalker extends Lint.RuleWalker {
|
||||
|
||||
visitDefaultClause(node: ts.DefaultClause) {
|
||||
this.checkDefaultOrCaseClause(node);
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"lib": ["es6", "es2015.collection"]
|
||||
},
|
@ -1,63 +0,0 @@
|
||||
"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 abstractFormatter_1 = require("tslint/lib/language/formatter/abstractFormatter");
|
||||
var colorBlue = "\u001b[93m";
|
||||
var colorYellow = "\u001b[93m";
|
||||
var colorRed = "\u001b[91m";
|
||||
var colorReset = "\u001b[0m";
|
||||
var Formatter = /** @class */ (function (_super) {
|
||||
__extends(Formatter, _super);
|
||||
function Formatter() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
Formatter.prototype.format = function (failures) {
|
||||
return this.mapToMessages(failures).join("\n") + "\n";
|
||||
};
|
||||
Formatter.prototype.mapToMessages = function (failures) {
|
||||
var _this = this;
|
||||
return failures.map(function (failure) {
|
||||
var fileName = failure.getFileName();
|
||||
var failureString = failure.getFailure();
|
||||
var ruleName = failure.getRuleName();
|
||||
var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
|
||||
var positionTuple = ":" + (lineAndCharacter.line + 1) + ":" + (lineAndCharacter.character + 1);
|
||||
if (_this.lastSeverity == failure.getRuleSeverity() && _this.lastFailure == failureString) {
|
||||
return " in " + fileName + positionTuple;
|
||||
}
|
||||
else {
|
||||
var message = _this.lastSeverity ? "\n" : "";
|
||||
switch (_this.lastSeverity = failure.getRuleSeverity()) {
|
||||
case "warning":
|
||||
message += colorYellow + "WARNING:" + colorReset;
|
||||
break;
|
||||
case "error":
|
||||
message += colorRed + "ERROR:" + colorReset;
|
||||
break;
|
||||
default:
|
||||
message += failure.getRuleSeverity();
|
||||
break;
|
||||
}
|
||||
_this.lastFailure = failureString;
|
||||
return message + " " + failureString + " [" + ruleName + "]\n in " + fileName + positionTuple;
|
||||
}
|
||||
});
|
||||
};
|
||||
Formatter.metadata = {
|
||||
formatterName: "as",
|
||||
description: "AssemblyScript's TSLint formatter.",
|
||||
sample: "Similar to ASC's output.",
|
||||
consumer: "human"
|
||||
};
|
||||
return Formatter;
|
||||
}(abstractFormatter_1.AbstractFormatter));
|
||||
exports.Formatter = Formatter;
|
@ -1,55 +0,0 @@
|
||||
"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 ts = require("typescript");
|
||||
var Lint = require("tslint");
|
||||
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.MISSING_TYPE_OR_INITIALIZER = "Missing type or initializer.";
|
||||
Rule.MISSING_RETURN_TYPE = "Missing return type.";
|
||||
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.visitVariableDeclaration = function (node) {
|
||||
if (!node.type && !node.initializer &&
|
||||
node.parent.parent.kind != ts.SyntaxKind.ForOfStatement) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
|
||||
}
|
||||
};
|
||||
DiagnosticsWalker.prototype.visitPropertyDeclaration = function (node) {
|
||||
if (!node.type && !node.initializer) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
|
||||
}
|
||||
};
|
||||
DiagnosticsWalker.prototype.visitParameterDeclaration = function (node) {
|
||||
if (!node.type && !node.initializer) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
|
||||
}
|
||||
};
|
||||
DiagnosticsWalker.prototype.visitFunctionDeclaration = function (node) {
|
||||
if (!node.type) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_RETURN_TYPE);
|
||||
}
|
||||
};
|
||||
return DiagnosticsWalker;
|
||||
}(Lint.RuleWalker));
|
@ -1,42 +0,0 @@
|
||||
import * as ts from "typescript";
|
||||
import * as Lint from "tslint";
|
||||
|
||||
export class Rule extends Lint.Rules.AbstractRule {
|
||||
|
||||
static MISSING_TYPE_OR_INITIALIZER = "Missing type or initializer.";
|
||||
static MISSING_RETURN_TYPE = "Missing return type.";
|
||||
|
||||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
||||
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
|
||||
}
|
||||
}
|
||||
|
||||
class DiagnosticsWalker extends Lint.RuleWalker {
|
||||
|
||||
visitVariableDeclaration(node: ts.VariableDeclaration) {
|
||||
if (
|
||||
!node.type && !node.initializer &&
|
||||
node.parent.parent.kind != ts.SyntaxKind.ForOfStatement
|
||||
) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
|
||||
}
|
||||
}
|
||||
|
||||
visitPropertyDeclaration(node: ts.PropertyDeclaration) {
|
||||
if (!node.type && !node.initializer) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
|
||||
}
|
||||
}
|
||||
|
||||
visitParameterDeclaration(node: ts.ParameterDeclaration) {
|
||||
if (!node.type && !node.initializer) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
|
||||
}
|
||||
}
|
||||
|
||||
visitFunctionDeclaration(node: ts.FunctionDeclaration) {
|
||||
if (!node.type) {
|
||||
this.addFailureAtNode(node, Rule.MISSING_RETURN_TYPE);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
"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));
|
@ -1,56 +0,0 @@
|
||||
"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 ts = require("typescript");
|
||||
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.NOT_BRACED = "Multi-line case clauses should be braced.";
|
||||
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.visitDefaultClause = function (node) {
|
||||
this.checkDefaultOrCaseClause(node);
|
||||
_super.prototype.visitDefaultClause.call(this, node);
|
||||
};
|
||||
DiagnosticsWalker.prototype.visitCaseClause = function (node) {
|
||||
this.checkDefaultOrCaseClause(node);
|
||||
_super.prototype.visitCaseClause.call(this, node);
|
||||
};
|
||||
DiagnosticsWalker.prototype.checkDefaultOrCaseClause = function (node) {
|
||||
var count = node.statements.length;
|
||||
if (count > 1) {
|
||||
this.addFailureAtNode(node, Rule.NOT_BRACED);
|
||||
}
|
||||
else if (count == 1) {
|
||||
var stmt = node.statements[0];
|
||||
if (stmt.kind != ts.SyntaxKind.Block) {
|
||||
if (!tsutils_1.isSameLine(node.getSourceFile(), node.getStart(), stmt.getStart())) {
|
||||
this.addFailureAtNode(node, Rule.NOT_BRACED);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
return DiagnosticsWalker;
|
||||
}(Lint.RuleWalker));
|
@ -1,43 +0,0 @@
|
||||
"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 ts = require("typescript");
|
||||
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.NOT_ON_SEPARATE_LINE = "Diagnostic message not on a separate line.";
|
||||
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.visitPropertyAccessExpression = function (node) {
|
||||
if (node.expression.kind === ts.SyntaxKind.Identifier) {
|
||||
if (node.expression.text == "DiagnosticCode" &&
|
||||
tsutils_1.isSameLine(node.getSourceFile(), node.parent.getStart(), node.getStart())) {
|
||||
this.addFailureAtNode(node, Rule.NOT_ON_SEPARATE_LINE);
|
||||
}
|
||||
}
|
||||
_super.prototype.visitPropertyAccessExpression.call(this, node);
|
||||
};
|
||||
return DiagnosticsWalker;
|
||||
}(Lint.RuleWalker));
|
Reference in New Issue
Block a user