mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-14 15:31:31 +00:00
Cleanup; Initial tslint integration
This commit is contained in:
63
lib/tslint/asFormatter.js
Normal file
63
lib/tslint/asFormatter.js
Normal file
@ -0,0 +1,63 @@
|
||||
"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;
|
53
lib/tslint/asFormatter.ts
Normal file
53
lib/tslint/asFormatter.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import { AbstractFormatter } from "tslint/lib/language/formatter/abstractFormatter";
|
||||
import { IFormatterMetadata } from "tslint/lib/language/formatter/formatter";
|
||||
import { RuleFailure } from "tslint/lib/language/rule/rule";
|
||||
|
||||
const colorBlue: string = "\u001b[93m";
|
||||
const colorYellow: string = "\u001b[93m";
|
||||
const colorRed: string = "\u001b[91m";
|
||||
const colorReset: string = "\u001b[0m";
|
||||
|
||||
export class Formatter extends AbstractFormatter {
|
||||
|
||||
static metadata: IFormatterMetadata = {
|
||||
formatterName: "as",
|
||||
description: "AssemblyScript's TSLint formatter.",
|
||||
sample: "Similar to ASC's output.",
|
||||
consumer: "human",
|
||||
};
|
||||
|
||||
lastSeverity: string;
|
||||
lastFailure: string;
|
||||
|
||||
format(failures: RuleFailure[]): string {
|
||||
return `${this.mapToMessages(failures).join("\n")}\n`;
|
||||
}
|
||||
|
||||
mapToMessages(failures: RuleFailure[]): string[] {
|
||||
return failures.map((failure: RuleFailure) => {
|
||||
const fileName = failure.getFileName();
|
||||
const failureString = failure.getFailure();
|
||||
const ruleName = failure.getRuleName();
|
||||
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
|
||||
const 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
55
lib/tslint/asTypesRule.js
Normal file
55
lib/tslint/asTypesRule.js
Normal file
@ -0,0 +1,55 @@
|
||||
"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));
|
38
lib/tslint/asTypesRule.ts
Normal file
38
lib/tslint/asTypesRule.ts
Normal file
@ -0,0 +1,38 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
43
lib/tslint/internal/asDiagnosticsRule.js
Normal file
43
lib/tslint/internal/asDiagnosticsRule.js
Normal file
@ -0,0 +1,43 @@
|
||||
"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));
|
26
lib/tslint/internal/asDiagnosticsRule.ts
Normal file
26
lib/tslint/internal/asDiagnosticsRule.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import * as ts from "typescript";
|
||||
import * as Lint from "tslint";
|
||||
import { isSameLine } from "tsutils";
|
||||
|
||||
export class Rule extends Lint.Rules.AbstractRule {
|
||||
|
||||
static NOT_ON_SEPARATE_LINE = "Diagnostic message not on a separate line.";
|
||||
|
||||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
||||
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
|
||||
}
|
||||
}
|
||||
|
||||
class DiagnosticsWalker extends Lint.RuleWalker {
|
||||
visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
|
||||
if (node.expression.kind === ts.SyntaxKind.Identifier) {
|
||||
if (
|
||||
(node.expression as ts.Identifier).text == "DiagnosticCode" &&
|
||||
isSameLine(node.getSourceFile(), node.parent.getStart(), node.getStart())
|
||||
) {
|
||||
this.addFailureAtNode(node, Rule.NOT_ON_SEPARATE_LINE);
|
||||
}
|
||||
}
|
||||
super.visitPropertyAccessExpression(node);
|
||||
}
|
||||
}
|
10
lib/tslint/tsconfig.json
Normal file
10
lib/tslint/tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"module": "commonjs",
|
||||
"lib": ["es6", "es2015.collection"]
|
||||
},
|
||||
"include": [
|
||||
"./**/**.ts"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user