Skip to content

Commit 081ac76

Browse files
committed
Give some love to the linter
1 parent 23a7db4 commit 081ac76

34 files changed

+526
-401
lines changed

bin/asc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ exports.main = function main(argv, options, callback) {
534534

535535
function readFileNode(filename) {
536536
try {
537-
var text;
537+
let text;
538538
stats.readCount++;
539539
stats.readTime += measure(() => {
540540
text = fs.readFileSync(filename, { encoding: "utf8" });

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/asc.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.

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/lint/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
![AS](https://avatars1.githubusercontent.com/u/28916798?s=48) lint
2+
======================
3+
4+
Recommended [TSLint](https://github.com/palantir/tslint) rules for use with AssemblyScript. Meant to spot the most common issues as you type.
5+
6+
Not a sophisticated checker in its current state.
7+
8+
Usage
9+
-----
10+
11+
Add the following `tslint.json` to your project:
12+
13+
```json
14+
{
15+
"extends": "@assemblyscript/lint"
16+
}
17+
```
18+
19+
Add additional rules if necessary.
20+
21+
Add a script to your `package.json`:
22+
23+
```json
24+
"scripts": {
25+
"lint": "tslint -c tslint.json --project ./path/to[/tsconfig.json] --format as"
26+
}
27+
```
28+
29+
Now, to check your sources, run:
30+
31+
```
32+
$> npm run lint
33+
```
34+
35+
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.
36+
37+
Custom rules
38+
------------
39+
40+
* **as-types** checks that all types are annotated or have an initializer.
41+
* **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.

lib/lint/base.json

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"rules": {
3+
"adjacent-overload-signatures": {},
4+
"ban-types": {
5+
"severity": "error",
6+
"options": [
7+
["object", "Not supported."],
8+
["any", "Not supported."],
9+
["undefined", "Not supported."],
10+
["never", "Not supported."],
11+
["number", "Use one of the WebAssembly types instead."],
12+
["boolean", "Use `bool` instead."]
13+
]
14+
},
15+
"object-literal-shorthand": {
16+
"severity": "error",
17+
"options": ["never"]
18+
},
19+
"restrict-plus-operands": {
20+
"severity": "error"
21+
},
22+
"curly": {
23+
"options": ["ignore-same-line"]
24+
},
25+
"deprecation": {},
26+
"encoding": {
27+
"severity": "error"
28+
},
29+
"eofline": {},
30+
"label-position": {
31+
"severity": "error"
32+
},
33+
"new-parens": {
34+
"severity": "error"
35+
},
36+
"no-any": {
37+
"severity": "error"
38+
},
39+
"no-arg": {
40+
"severity": "error"
41+
},
42+
"no-consecutive-blank-lines": {},
43+
"no-debugger": {
44+
"severity": "error"
45+
},
46+
"no-default-export": {
47+
"severity": "error"
48+
},
49+
"no-duplicate-imports": {
50+
"severity": "error"
51+
},
52+
"no-duplicate-super": {
53+
"severity": "error"
54+
},
55+
"no-duplicate-switch-case": {
56+
"severity": "error"
57+
},
58+
"no-duplicate-variable": {
59+
"severity": "error"
60+
},
61+
"no-eval": {
62+
"severity": "error"
63+
},
64+
"no-inferred-empty-object-type": {
65+
"severity": "error"
66+
},
67+
"no-internal-module": {
68+
"severity": "error"
69+
},
70+
"no-invalid-template-strings": {
71+
"severity": "error"
72+
},
73+
"no-invalid-this": {
74+
"severity": "error"
75+
},
76+
"no-irregular-whitespace": {},
77+
"no-misused-new": {
78+
"severity": "error"
79+
},
80+
"no-object-literal-type-assertion": {
81+
"severity": "error"
82+
},
83+
"no-parameter-properties": {
84+
"severity": "error"
85+
},
86+
"no-require-imports": {
87+
"severity": "error"
88+
},
89+
"no-shadowed-variable": {},
90+
"no-sparse-arrays": {},
91+
"no-string-literal": {
92+
"severity": "error"
93+
},
94+
"no-string-throw": {
95+
"severity": "error"
96+
},
97+
"no-trailing-whitespace": {},
98+
"no-unbound-method": {
99+
"severity": "error"
100+
},
101+
"no-unsafe-any": {
102+
"severity": "error"
103+
},
104+
"no-unused-variable": {
105+
"options": [{
106+
"ignore-pattern": "^_"
107+
}]
108+
},
109+
"no-void-expression": {
110+
"severity": "error"
111+
},
112+
"prefer-method-signature": {},
113+
"radix": {},
114+
"semicolon": {
115+
"options": ["always"]
116+
}
117+
}
118+
}

lib/lint/formatters/asFormatter.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const abstractFormatter_1 = require("tslint/lib/language/formatter/abstractFormatter");
4+
const colorBlue = "\u001b[93m";
5+
const colorYellow = "\u001b[93m";
6+
const colorRed = "\u001b[91m";
7+
const colorReset = "\u001b[0m";
8+
class Formatter extends abstractFormatter_1.AbstractFormatter {
9+
format(failures) {
10+
return `${this.mapToMessages(failures).join("\n")}\n`;
11+
}
12+
mapToMessages(failures) {
13+
return failures.map((failure) => {
14+
var fileName = failure.getFileName();
15+
var failureString = failure.getFailure();
16+
var ruleName = failure.getRuleName();
17+
var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
18+
var positionTuple = `:${lineAndCharacter.line + 1}:${lineAndCharacter.character + 1}`;
19+
if (this.lastSeverity == failure.getRuleSeverity() && this.lastFailure == failureString) {
20+
return " in " + fileName + positionTuple;
21+
}
22+
else {
23+
let message = this.lastSeverity ? "\n" : "";
24+
switch (this.lastSeverity = failure.getRuleSeverity()) {
25+
case "warning": {
26+
message += colorYellow + "WARNING:" + colorReset;
27+
break;
28+
}
29+
case "error": {
30+
message += colorRed + "ERROR:" + colorReset;
31+
break;
32+
}
33+
default: {
34+
message += failure.getRuleSeverity();
35+
break;
36+
}
37+
}
38+
this.lastFailure = failureString;
39+
return message + " " + failureString + " [" + ruleName + "]\n in " + fileName + positionTuple;
40+
}
41+
});
42+
}
43+
}
44+
Formatter.metadata = {
45+
formatterName: "as",
46+
description: "AssemblyScript's TSLint formatter.",
47+
sample: "Similar to ASC's output.",
48+
consumer: "human",
49+
};
50+
exports.Formatter = Formatter;

lib/lint/index.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./base.json",
3+
"rulesDirectory": ["./rules", "./rules/internal"],
4+
"formattersDirectory": ["./formatters"],
5+
"rules": {
6+
"as-types": {
7+
"severity": "error"
8+
},
9+
"as-variables": {}
10+
}
11+
}

lib/lint/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@assemblyscript/lint",
3+
"version": "1.0.0",
4+
"main": "index.json",
5+
"scripts": {
6+
"build": "tsc --project ./src --outDir . --diagnostics"
7+
},
8+
"peerDependencies": {
9+
"tslint": "^5.9.1"
10+
},
11+
"devDependencies": {
12+
"typescript": "^2.7.2"
13+
},
14+
"files": [
15+
"index.json",
16+
"package.json",
17+
"README.md",
18+
"rules/",
19+
"formatters/"
20+
]
21+
}

lib/lint/rules/asTypesRule.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const ts = require("typescript");
4+
const Lint = require("tslint");
5+
class Rule extends Lint.Rules.AbstractRule {
6+
apply(sourceFile) {
7+
return this.applyWithWalker(new DiagnosticsWalker(sourceFile, this.getOptions()));
8+
}
9+
}
10+
Rule.MISSING_TYPE_OR_INITIALIZER = "Missing type or initializer.";
11+
Rule.MISSING_RETURN_TYPE = "Missing return type.";
12+
exports.Rule = Rule;
13+
class DiagnosticsWalker extends Lint.RuleWalker {
14+
visitVariableDeclaration(node) {
15+
var list = node.parent;
16+
if (list) {
17+
let stmt = list.parent;
18+
if (stmt && stmt.kind != ts.SyntaxKind.ForOfStatement) {
19+
this.checkTypeOrInitializer(node);
20+
}
21+
}
22+
super.visitVariableDeclaration(node);
23+
}
24+
visitPropertyDeclaration(node) {
25+
this.checkTypeOrInitializer(node);
26+
super.visitPropertyDeclaration(node);
27+
}
28+
visitParameterDeclaration(node) {
29+
this.checkTypeOrInitializer(node);
30+
super.visitParameterDeclaration(node);
31+
}
32+
checkTypeOrInitializer(node) {
33+
if (!node.type && !node.initializer) {
34+
this.addFailureAtNode(node, Rule.MISSING_TYPE_OR_INITIALIZER);
35+
}
36+
}
37+
visitFunctionDeclaration(node) {
38+
this.checkFunctionReturnType(node);
39+
super.visitFunctionDeclaration(node);
40+
}
41+
visitArrowFunction(node) {
42+
this.checkFunctionReturnType(node);
43+
super.visitArrowFunction(node);
44+
}
45+
visitMethodDeclaration(node) {
46+
this.checkFunctionReturnType(node);
47+
super.visitMethodDeclaration(node);
48+
}
49+
visitGetAccessor(node) {
50+
this.checkFunctionReturnType(node);
51+
super.visitGetAccessor(node);
52+
}
53+
checkFunctionReturnType(node) {
54+
if (!node.type) {
55+
this.addFailureAtNode(node, Rule.MISSING_RETURN_TYPE);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)