Skip to content

Commit df717bd

Browse files
committed
feat(ESLint): ESLintのシンプルな実装を追加
1 parent 4930977 commit df717bd

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"eslint": "^1.3.0",
3535
"eslint-plugin-markdown": "git://github.com/eslint/eslint-plugin-markdown.git",
3636
"espower-babel": "^3.3.0",
37+
"esprima": "^2.5.0",
38+
"estraverse": "^4.1.0",
3739
"gitbook-cli": "^0.3.4",
3840
"gitbook-plugin-richquotes": "0.0.5",
3941
"gitbook-summary-to-path": "^1.0.1",

src/ESLint/MyLint.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import {parse} from "esprima";
4+
import {traverse} from "estraverse";
5+
import {EventEmitter} from "events";
6+
class RuleContext extends EventEmitter {
7+
report(node, message) {
8+
this.emit("report", message);
9+
}
10+
}
11+
export default class MyLint extends EventEmitter {
12+
constructor() {
13+
super();
14+
this._emitter = new EventEmitter();
15+
this._ruleContext = new RuleContext();
16+
this._ruleContext.on("report", (message) => {
17+
this.emit("report", message);
18+
});
19+
}
20+
21+
loadPlugin(plugin) {
22+
var rule = plugin(this._ruleContext);
23+
// on(nodeType, nodeTypeCallback);
24+
Object.keys(rule).forEach(nodeType => {
25+
this._emitter.on(nodeType, rule[nodeType]);
26+
});
27+
}
28+
29+
30+
lint(code) {
31+
var ast = parse(code);
32+
traverse(ast, {
33+
enter: (node) => {
34+
this._emitter.emit(node.type, node);
35+
},
36+
leave: (node) => {
37+
this._emitter.emit(`${node.type}:exit`, node);
38+
}
39+
});
40+
}
41+
}

src/ESLint/no-console.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
module.exports = function (context) {
3+
return {
4+
"MemberExpression": function (node) {
5+
if (node.object.name === "console") {
6+
context.report(node, "Unexpected console statement.");
7+
}
8+
}
9+
};
10+
};

test/ESLint/MyLint-test.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import assert from "power-assert";
4+
import MyLint from "../../src/ESLint/MyLint";
5+
import noConsole from "../../src/ESLint/no-console";
6+
describe("MyLint", function () {
7+
it("should load and lint", function () {
8+
let lint = new MyLint();
9+
lint.loadPlugin(noConsole);
10+
lint.on("report", (message)=> {
11+
assert.equal(message, "Unexpected console statement.");
12+
});
13+
lint.lint(`console.log("test")`);
14+
});
15+
});

0 commit comments

Comments
 (0)