Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdf717bd

Browse files
committed
feat(ESLint): ESLintのシンプルな実装を追加
1 parent4930977 commitdf717bd

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

‎package.json

Lines changed: 2 additions & 0 deletions
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

Lines changed: 41 additions & 0 deletions
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+
classRuleContextextendsEventEmitter{
7+
report(node,message){
8+
this.emit("report",message);
9+
}
10+
}
11+
exportdefaultclassMyLintextendsEventEmitter{
12+
constructor(){
13+
super();
14+
this._emitter=newEventEmitter();
15+
this._ruleContext=newRuleContext();
16+
this._ruleContext.on("report",(message)=>{
17+
this.emit("report",message);
18+
});
19+
}
20+
21+
loadPlugin(plugin){
22+
varrule=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+
varast=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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
importassertfrom"power-assert";
4+
importMyLintfrom"../../src/ESLint/MyLint";
5+
importnoConsolefrom"../../src/ESLint/no-console";
6+
describe("MyLint",function(){
7+
it("should load and lint",function(){
8+
letlint=newMyLint();
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)

[8]ページ先頭

©2009-2025 Movatter.jp