File tree 4 files changed +68
-0
lines changed
4 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 34
34
"eslint" : " ^1.3.0" ,
35
35
"eslint-plugin-markdown" : " git://github.com/eslint/eslint-plugin-markdown.git" ,
36
36
"espower-babel" : " ^3.3.0" ,
37
+ "esprima" : " ^2.5.0" ,
38
+ "estraverse" : " ^4.1.0" ,
37
39
"gitbook-cli" : " ^0.3.4" ,
38
40
"gitbook-plugin-richquotes" : " 0.0.5" ,
39
41
"gitbook-summary-to-path" : " ^1.0.1" ,
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments