-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// LICENSE : MIT | ||
"use strict"; | ||
import {parse} from "esprima"; | ||
import {traverse} from "estraverse"; | ||
import {EventEmitter} from "events"; | ||
class RuleContext extends EventEmitter { | ||
report(node, message) { | ||
this.emit("report", message); | ||
} | ||
} | ||
export default class MyLint extends EventEmitter { | ||
constructor() { | ||
super(); | ||
this._emitter = new EventEmitter(); | ||
this._ruleContext = new RuleContext(); | ||
this._ruleContext.on("report", (message) => { | ||
this.emit("report", message); | ||
}); | ||
} | ||
|
||
loadPlugin(plugin) { | ||
var rule = plugin(this._ruleContext); | ||
// on(nodeType, nodeTypeCallback); | ||
Object.keys(rule).forEach(nodeType => { | ||
this._emitter.on(nodeType, rule[nodeType]); | ||
}); | ||
} | ||
|
||
|
||
lint(code) { | ||
var ast = parse(code); | ||
traverse(ast, { | ||
enter: (node) => { | ||
this._emitter.emit(node.type, node); | ||
}, | ||
leave: (node) => { | ||
this._emitter.emit(`${node.type}:exit`, node); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"use strict"; | ||
module.exports = function (context) { | ||
return { | ||
"MemberExpression": function (node) { | ||
if (node.object.name === "console") { | ||
context.report(node, "Unexpected console statement."); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// LICENSE : MIT | ||
"use strict"; | ||
import assert from "power-assert"; | ||
import MyLint from "../../src/ESLint/MyLint"; | ||
import noConsole from "../../src/ESLint/no-console"; | ||
describe("MyLint", function () { | ||
it("should load and lint", function () { | ||
let lint = new MyLint(); | ||
lint.loadPlugin(noConsole); | ||
lint.on("report", (message)=> { | ||
assert.equal(message, "Unexpected console statement."); | ||
}); | ||
lint.lint(`console.log("test")`); | ||
}); | ||
}); |