11// LICENSE : MIT
22"use strict" ;
3- const CLIEngine = require ( "eslint" ) . CLIEngine ;
43const path = require ( "path" ) ;
4+ const Source = require ( "structured-source" ) ;
5+ const CLIEngine = require ( "eslint" ) . CLIEngine ;
56const defaultOptions = {
67 // path to .eslintrc file
78 "configFile" : null ,
@@ -37,7 +38,7 @@ const reporter = (context, options) => {
3738 }
3839 const raw = getSource ( node ) ;
3940 const code = getUntrimmedCode ( node , raw ) ;
40-
41+ const source = new Source ( code ) ;
4142 const resultLinting = engine . executeOnText ( code , node . lang ) ;
4243 if ( resultLinting . errorCount === 0 ) {
4344 return ;
@@ -54,24 +55,33 @@ const reporter = (context, options) => {
5455 ESLint message line and column start with 1
5556 */
5657 if ( options . ignoreParsingErrors && message . message . includes ( "Parsing error" ) ) {
57- return ;
58+ return ;
5859 }
5960
6061 const prefix = message . ruleId ? `${ message . ruleId } : ` : "" ;
6162 if ( message . fix ) {
62- const paddingIndex = raw . indexOf ( code ) ;
6363 const fixedRange = message . fix . range ;
6464 const fixedText = message . fix . text ;
65- const fixedWithPadding = [ fixedRange [ 0 ] + paddingIndex , fixedRange [ 1 ] + paddingIndex ] ;
66- report ( node , new RuleError ( `${ prefix } ${ message . message } ` , {
65+ const sourceBlockDiffIndex = ( raw !== node . value ) ? raw . indexOf ( code ) : 0 ;
66+ const fixedWithPadding = [ fixedRange [ 0 ] + sourceBlockDiffIndex , fixedRange [ 1 ] + sourceBlockDiffIndex ] ;
67+ const index = source . positionToIndex ( {
6768 line : message . line ,
68- column : message . column - 1 ,
69+ column : message . column
70+ } ) ;
71+ const adjustedIndex = index + sourceBlockDiffIndex - 1 ;
72+ report ( node , new RuleError ( `${ prefix } ${ message . message } ` , {
73+ index : adjustedIndex ,
6974 fix : fixer . replaceTextRange ( fixedWithPadding , fixedText )
7075 } ) ) ;
7176 } else {
72- report ( node , new RuleError ( `${ prefix } ${ message . message } ` , {
77+ const sourceBlockDiffIndex = ( raw !== node . value ) ? raw . indexOf ( code ) : 0 ;
78+ const index = source . positionToIndex ( {
7379 line : message . line ,
74- column : message . column - 1
80+ column : message . column
81+ } ) ;
82+ const adjustedIndex = index + sourceBlockDiffIndex - 1 ;
83+ report ( node , new RuleError ( `${ prefix } ${ message . message } ` , {
84+ index : adjustedIndex
7585 } ) ) ;
7686 }
7787
@@ -82,7 +92,7 @@ const reporter = (context, options) => {
8292} ;
8393
8494/**
85- * get actual code value from CodeBlock node
95+ * [Markdown] get actual code value from CodeBlock node
8696 * @param {Object } node
8797 * @param {string } raw raw value include CodeBlock syntax
8898 * @returns {string }
@@ -91,18 +101,23 @@ function getUntrimmedCode(node, raw) {
91101 if ( node . type !== "CodeBlock" ) {
92102 return node . value
93103 }
94-
95104 // Space indented CodeBlock that has not lang
96105 if ( ! node . lang ) {
97106 return node . value ;
98107 }
99108
109+ // If it is not markdown codeBlock, just use node.value
110+ if ( ! ( raw . startsWith ( "```" ) && raw . endsWith ( "```" ) ) ) {
111+ if ( node . value . endsWith ( "\n" ) ) {
112+ return node . value
113+ }
114+ return node . value + "\n" ;
115+ }
116+ // Markdown(remark) specific hack
100117 // https://github.com/wooorm/remark/issues/207#issuecomment-244620590
101118 const lines = raw . split ( "\n" ) ;
102-
103119 // code lines without the first line and the last line
104120 const codeLines = lines . slice ( 1 , lines . length - 1 ) ;
105-
106121 // add last new line
107122 // \n```
108123 return codeLines . join ( "\n" ) + "\n" ;
@@ -111,4 +126,4 @@ function getUntrimmedCode(node, raw) {
111126module . exports = {
112127 linter : reporter ,
113128 fixer : reporter
114- } ;
129+ } ;
0 commit comments