1
1
// LICENSE : MIT
2
2
"use strict" ;
3
- const path = require ( "path" ) ;
4
- const Source = require ( "structured-source" ) ;
5
- const CLIEngine = require ( "eslint" ) . CLIEngine ;
3
+ var __awaiter = ( this && this . __awaiter ) || function ( thisArg , _arguments , P , generator ) {
4
+ function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
5
+ return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
6
+ function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
7
+ function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
8
+ function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
9
+ step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
10
+ } ) ;
11
+ } ;
12
+ var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
13
+ return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
14
+ } ;
15
+ Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
16
+ const path_1 = __importDefault ( require ( "path" ) ) ;
17
+ const structured_source_1 = __importDefault ( require ( "structured-source" ) ) ;
18
+ const eslint_1 = require ( "eslint" ) ;
6
19
const defaultOptions = {
7
20
// path to .eslintrc file
8
21
"configFile" : null ,
@@ -17,80 +30,84 @@ const getConfigBaseDir = (context) => {
17
30
// https://github.com/textlint/textlint/issues/294
18
31
const textlintRcFilePath = context . config ? context . config . configFile : null ;
19
32
// .textlinrc directory
20
- return textlintRcFilePath ? path . dirname ( textlintRcFilePath ) : process . cwd ( ) ;
33
+ return textlintRcFilePath ? path_1 . default . dirname ( textlintRcFilePath ) : process . cwd ( ) ;
21
34
} ;
22
-
23
35
const reporter = ( context , options ) => {
24
- const { Syntax, RuleError, report, fixer, getSource } = context ;
36
+ const { Syntax, RuleError, report, fixer, getSource, getFilePath } = context ;
37
+ if ( ! options ) {
38
+ throw new Error ( `Require options: { "configFile": "path/to/.eslintrc" }` ) ;
39
+ }
25
40
if ( ! options . configFile ) {
26
41
throw new Error ( `Require options: { "configFile": "path/to/.eslintrc" }` ) ;
27
42
}
28
43
const availableLang = options . langs || defaultOptions . langs ;
29
44
const textlintRCDir = getConfigBaseDir ( context ) ;
30
- const ESLintOptions = {
31
- configFile : path . resolve ( textlintRCDir , options . configFile )
32
- } ;
33
- const engine = new CLIEngine ( ESLintOptions ) ;
45
+ const esLintConfigFilePath = textlintRCDir ? path_1 . default . resolve ( textlintRCDir , options . configFile ) : options . configFile ;
46
+ const engine = new eslint_1 . ESLint ( {
47
+ useEslintrc : false ,
48
+ overrideConfigFile : esLintConfigFilePath
49
+ } ) ;
34
50
return {
35
51
[ Syntax . CodeBlock ] ( node ) {
36
- if ( availableLang . indexOf ( node . lang ) === - 1 ) {
37
- return ;
38
- }
39
- const raw = getSource ( node ) ;
40
- const code = getUntrimmedCode ( node , raw ) ;
41
- const source = new Source ( code ) ;
42
- const resultLinting = engine . executeOnText ( code , node . lang ) ;
43
- if ( resultLinting . errorCount === 0 ) {
44
- return ;
45
- }
46
- const results = resultLinting . results ;
47
- results . forEach ( result => {
48
- result . messages . forEach ( message => {
49
- /*
50
-
51
- 1. ```js
52
- 2. CODE
53
- 3. ```
54
-
55
- ESLint message line and column start with 1
56
- */
57
- if ( options . ignoreParsingErrors && message . message . includes ( "Parsing error" ) ) {
58
- return ;
59
- }
60
-
61
- const prefix = message . ruleId ? `${ message . ruleId } : ` : "" ;
62
- if ( message . fix ) {
63
- const fixedRange = message . fix . range ;
64
- const fixedText = message . fix . text ;
65
- const sourceBlockDiffIndex = ( raw !== node . value ) ? raw . indexOf ( code ) : 0 ;
66
- const fixedWithPadding = [ fixedRange [ 0 ] + sourceBlockDiffIndex , fixedRange [ 1 ] + sourceBlockDiffIndex ] ;
67
- const index = source . positionToIndex ( {
68
- line : message . line ,
69
- column : message . column
70
- } ) ;
71
- const adjustedIndex = index + sourceBlockDiffIndex - 1 ;
72
- report ( node , new RuleError ( `${ prefix } ${ message . message } ` , {
73
- index : adjustedIndex ,
74
- fix : fixer . replaceTextRange ( fixedWithPadding , fixedText )
75
- } ) ) ;
76
- } else {
77
- const sourceBlockDiffIndex = ( raw !== node . value ) ? raw . indexOf ( code ) : 0 ;
78
- const index = source . positionToIndex ( {
79
- line : message . line ,
80
- column : message . column
81
- } ) ;
82
- const adjustedIndex = index + sourceBlockDiffIndex - 1 ;
83
- report ( node , new RuleError ( `${ prefix } ${ message . message } ` , {
84
- index : adjustedIndex
85
- } ) ) ;
86
- }
87
-
52
+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
53
+ if ( availableLang . indexOf ( node . lang ) === - 1 ) {
54
+ return ;
55
+ }
56
+ const raw = getSource ( node ) ;
57
+ const code = getUntrimmedCode ( node , raw ) ;
58
+ const source = new structured_source_1 . default ( code ) ;
59
+ const resultLinting = yield engine . lintText ( code , {
60
+ filePath : `test.${ node . lang } `
61
+ } ) ;
62
+ if ( resultLinting . length === 0 ) {
63
+ return ;
64
+ }
65
+ resultLinting . forEach ( result => {
66
+ result . messages . forEach ( message => {
67
+ /*
68
+
69
+ 1. ```js
70
+ 2. CODE
71
+ 3. ```
72
+
73
+ ESLint message line and column start with 1
74
+ */
75
+ if ( options . ignoreParsingErrors && message . message . includes ( "Parsing error" ) ) {
76
+ return ;
77
+ }
78
+ const prefix = message . ruleId ? `${ message . ruleId } : ` : "" ;
79
+ if ( message . fix ) {
80
+ const fixedRange = message . fix . range ;
81
+ const fixedText = message . fix . text ;
82
+ const sourceBlockDiffIndex = ( raw !== node . value ) ? raw . indexOf ( code ) : 0 ;
83
+ const fixedWithPadding = [ fixedRange [ 0 ] + sourceBlockDiffIndex , fixedRange [ 1 ] + sourceBlockDiffIndex ] ;
84
+ const index = source . positionToIndex ( {
85
+ line : message . line ,
86
+ column : message . column
87
+ } ) ;
88
+ const adjustedIndex = index + sourceBlockDiffIndex - 1 ;
89
+ report ( node , new RuleError ( `${ prefix } ${ message . message } ` , {
90
+ index : adjustedIndex ,
91
+ fix : fixer . replaceTextRange ( fixedWithPadding , fixedText )
92
+ } ) ) ;
93
+ }
94
+ else {
95
+ const sourceBlockDiffIndex = ( raw !== node . value ) ? raw . indexOf ( code ) : 0 ;
96
+ const index = source . positionToIndex ( {
97
+ line : message . line ,
98
+ column : message . column
99
+ } ) ;
100
+ const adjustedIndex = index + sourceBlockDiffIndex - 1 ;
101
+ report ( node , new RuleError ( `${ prefix } ${ message . message } ` , {
102
+ index : adjustedIndex
103
+ } ) ) ;
104
+ }
105
+ } ) ;
88
106
} ) ;
89
107
} ) ;
90
108
}
91
- }
109
+ } ;
92
110
} ;
93
-
94
111
/**
95
112
* [Markdown] get actual code value from CodeBlock node
96
113
* @param {Object } node
@@ -99,17 +116,16 @@ const reporter = (context, options) => {
99
116
*/
100
117
function getUntrimmedCode ( node , raw ) {
101
118
if ( node . type !== "CodeBlock" ) {
102
- return node . value
119
+ return node . value ;
103
120
}
104
121
// Space indented CodeBlock that has not lang
105
122
if ( ! node . lang ) {
106
123
return node . value ;
107
124
}
108
-
109
125
// If it is not markdown codeBlock, just use node.value
110
126
if ( ! ( raw . startsWith ( "```" ) && raw . endsWith ( "```" ) ) ) {
111
127
if ( node . value . endsWith ( "\n" ) ) {
112
- return node . value
128
+ return node . value ;
113
129
}
114
130
return node . value + "\n" ;
115
131
}
@@ -122,8 +138,7 @@ function getUntrimmedCode(node, raw) {
122
138
// \n```
123
139
return codeLines . join ( "\n" ) + "\n" ;
124
140
}
125
-
126
- module . exports = {
141
+ exports . default = {
127
142
linter : reporter ,
128
143
fixer : reporter
129
144
} ;
0 commit comments