Skip to content

Commit 8dc517e

Browse files
committed
Cleanup; Initial tslint integration
1 parent 16ffddc commit 8dc517e

40 files changed

+6706
-4826
lines changed

.travis.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ language: node_js
22
node_js:
33
- lts/*
44
- node
5-
before_script: npm run clean && node bin/asc -v && npm test
6-
script: npm run build && node bin/asc -v && npm test
5+
before_script:
6+
- npm run lint
7+
- npm run clean && node bin/asc -v && npm test
8+
script:
9+
- npm run build && node bin/asc -v && npm test

bin/asc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ try {
1313
} catch (e) {
1414
try {
1515
require("ts-node").register({
16-
project: path.join(__dirname, "..", "src")
16+
project: path.join(__dirname, "..", "src", "tsconfig.json")
1717
});
1818
require("../src/glue/js");
1919
assemblyscript = require("../src");
2020
isDev = true;
2121
} catch (e) {
22-
// last resort: browser bundle under node
22+
// last resort: browser bundle under node (relative to 'dist/')
2323
assemblyscript = require("./assemblyscript");
2424
isDev = false;
2525
}
@@ -480,7 +480,7 @@ exports.main = function main(argv, options, callback) {
480480
}
481481

482482
// Write asm.js
483-
if (args.asmjsFile != null && args.asmjsFile.length) {
483+
if (args.asmjsFile != null) {
484484
let asm;
485485
if (args.asmjsFile.length) {
486486
stats.emitCount++;

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.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 abstractFormatter_1 = require("tslint/lib/language/formatter/abstractFormatter");
14+
var colorBlue = "\u001b[93m";
15+
var colorYellow = "\u001b[93m";
16+
var colorRed = "\u001b[91m";
17+
var colorReset = "\u001b[0m";
18+
var Formatter = /** @class */ (function (_super) {
19+
__extends(Formatter, _super);
20+
function Formatter() {
21+
return _super !== null && _super.apply(this, arguments) || this;
22+
}
23+
Formatter.prototype.format = function (failures) {
24+
return this.mapToMessages(failures).join("\n") + "\n";
25+
};
26+
Formatter.prototype.mapToMessages = function (failures) {
27+
var _this = this;
28+
return failures.map(function (failure) {
29+
var fileName = failure.getFileName();
30+
var failureString = failure.getFailure();
31+
var ruleName = failure.getRuleName();
32+
var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
33+
var positionTuple = ":" + (lineAndCharacter.line + 1) + ":" + (lineAndCharacter.character + 1);
34+
if (_this.lastSeverity == failure.getRuleSeverity() && _this.lastFailure == failureString) {
35+
return " in " + fileName + positionTuple;
36+
}
37+
else {
38+
var message = _this.lastSeverity ? "\n" : "";
39+
switch (_this.lastSeverity = failure.getRuleSeverity()) {
40+
case "warning":
41+
message += colorYellow + "WARNING:" + colorReset;
42+
break;
43+
case "error":
44+
message += colorRed + "ERROR:" + colorReset;
45+
break;
46+
default:
47+
message += failure.getRuleSeverity();
48+
break;
49+
}
50+
_this.lastFailure = failureString;
51+
return message + " " + failureString + " [" + ruleName + "]\n in " + fileName + positionTuple;
52+
}
53+
});
54+
};
55+
Formatter.metadata = {
56+
formatterName: "as",
57+
description: "AssemblyScript's TSLint formatter.",
58+
sample: "Similar to ASC's output.",
59+
consumer: "human"
60+
};
61+
return Formatter;
62+
}(abstractFormatter_1.AbstractFormatter));
63+
exports.Formatter = Formatter;

lib/tslint/asFormatter.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { AbstractFormatter } from "tslint/lib/language/formatter/abstractFormatter";
2+
import { IFormatterMetadata } from "tslint/lib/language/formatter/formatter";
3+
import { RuleFailure } from "tslint/lib/language/rule/rule";
4+
5+
const colorBlue: string = "\u001b[93m";
6+
const colorYellow: string = "\u001b[93m";
7+
const colorRed: string = "\u001b[91m";
8+
const colorReset: string = "\u001b[0m";
9+
10+
export class Formatter extends AbstractFormatter {
11+
12+
static metadata: IFormatterMetadata = {
13+
formatterName: "as",
14+
description: "AssemblyScript's TSLint formatter.",
15+
sample: "Similar to ASC's output.",
16+
consumer: "human",
17+
};
18+
19+
lastSeverity: string;
20+
lastFailure: string;
21+
22+
format(failures: RuleFailure[]): string {
23+
return `${this.mapToMessages(failures).join("\n")}\n`;
24+
}
25+
26+
mapToMessages(failures: RuleFailure[]): string[] {
27+
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}`;
33+
if (this.lastSeverity == failure.getRuleSeverity() && this.lastFailure == failureString) {
34+
return " in " + fileName + positionTuple;
35+
} else {
36+
var message = this.lastSeverity ? "\n" : "";
37+
switch (this.lastSeverity = failure.getRuleSeverity()) {
38+
case "warning":
39+
message += colorYellow + "WARNING:" + colorReset;
40+
break;
41+
case "error":
42+
message += colorRed + "ERROR:" + colorReset;
43+
break;
44+
default:
45+
message += failure.getRuleSeverity();
46+
break;
47+
}
48+
this.lastFailure = failureString;
49+
return message + " " + failureString + " [" + ruleName + "]\n in " + fileName + positionTuple;
50+
}
51+
});
52+
}
53+
}

lib/tslint/asTypesRule.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 ts = require("typescript");
14+
var Lint = require("tslint");
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.MISSING_TYPE_OR_INITIALIZER = "Missing type or initializer.";
24+
Rule.MISSING_RETURN_TYPE = "Missing return type.";
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.visitVariableDeclaration = function (node) {
34+
if (!node.type && !node.initializer &&
35+
node.parent.parent.kind != ts.SyntaxKind.ForOfStatement) {
36+
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
37+
}
38+
};
39+
DiagnosticsWalker.prototype.visitPropertyDeclaration = function (node) {
40+
if (!node.type && !node.initializer) {
41+
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
42+
}
43+
};
44+
DiagnosticsWalker.prototype.visitParameterDeclaration = function (node) {
45+
if (!node.type && !node.initializer) {
46+
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
47+
}
48+
};
49+
DiagnosticsWalker.prototype.visitFunctionDeclaration = function (node) {
50+
if (!node.type) {
51+
this.addFailureAtNode(node, Rule.MISSING_RETURN_TYPE);
52+
}
53+
};
54+
return DiagnosticsWalker;
55+
}(Lint.RuleWalker));

lib/tslint/asTypesRule.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as ts from "typescript";
2+
import * as Lint from "tslint";
3+
4+
export class Rule extends Lint.Rules.AbstractRule {
5+
6+
static MISSING_TYPE_OR_INITIALIZER = "Missing type or initializer.";
7+
static MISSING_RETURN_TYPE = "Missing return type.";
8+
9+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
10+
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
11+
}
12+
}
13+
14+
class DiagnosticsWalker extends Lint.RuleWalker {
15+
visitVariableDeclaration(node: ts.VariableDeclaration) {
16+
if (
17+
!node.type && !node.initializer &&
18+
node.parent.parent.kind != ts.SyntaxKind.ForOfStatement
19+
) {
20+
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
21+
}
22+
}
23+
visitPropertyDeclaration(node: ts.PropertyDeclaration) {
24+
if (!node.type && !node.initializer) {
25+
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
26+
}
27+
}
28+
visitParameterDeclaration(node: ts.ParameterDeclaration) {
29+
if (!node.type && !node.initializer) {
30+
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
31+
}
32+
}
33+
visitFunctionDeclaration(node: ts.FunctionDeclaration) {
34+
if (!node.type) {
35+
this.addFailureAtNode(node, Rule.MISSING_RETURN_TYPE);
36+
}
37+
}
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 ts = require("typescript");
14+
var Lint = require("tslint");
15+
var tsutils_1 = require("tsutils");
16+
var Rule = /** @class */ (function (_super) {
17+
__extends(Rule, _super);
18+
function Rule() {
19+
return _super !== null && _super.apply(this, arguments) || this;
20+
}
21+
Rule.prototype.apply = function (sourceFile) {
22+
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
23+
};
24+
Rule.NOT_ON_SEPARATE_LINE = "Diagnostic message not on a separate line.";
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.visitPropertyAccessExpression = function (node) {
34+
if (node.expression.kind === ts.SyntaxKind.Identifier) {
35+
if (node.expression.text == "DiagnosticCode" &&
36+
tsutils_1.isSameLine(node.getSourceFile(), node.parent.getStart(), node.getStart())) {
37+
this.addFailureAtNode(node, Rule.NOT_ON_SEPARATE_LINE);
38+
}
39+
}
40+
_super.prototype.visitPropertyAccessExpression.call(this, node);
41+
};
42+
return DiagnosticsWalker;
43+
}(Lint.RuleWalker));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as ts from "typescript";
2+
import * as Lint from "tslint";
3+
import { isSameLine } from "tsutils";
4+
5+
export class Rule extends Lint.Rules.AbstractRule {
6+
7+
static NOT_ON_SEPARATE_LINE = "Diagnostic message not on a separate line.";
8+
9+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
10+
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
11+
}
12+
}
13+
14+
class DiagnosticsWalker extends Lint.RuleWalker {
15+
visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
16+
if (node.expression.kind === ts.SyntaxKind.Identifier) {
17+
if (
18+
(node.expression as ts.Identifier).text == "DiagnosticCode" &&
19+
isSameLine(node.getSourceFile(), node.parent.getStart(), node.getStart())
20+
) {
21+
this.addFailureAtNode(node, Rule.NOT_ON_SEPARATE_LINE);
22+
}
23+
}
24+
super.visitPropertyAccessExpression(node);
25+
}
26+
}

lib/tslint/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"lib": ["es6", "es2015.collection"]
6+
},
7+
"include": [
8+
"./**/**.ts"
9+
]
10+
}

0 commit comments

Comments
 (0)