@@ -7,8 +7,10 @@ const { indexToColumn } = require("./file_coordinates.js");
77
88// how annoying is that kind of import in JS ?!
99var OffsetMap = require ( "./offset_map.js" ) . OffsetMap ;
10+ var DummyReplacementsMap = require ( "./dummy_replacements_map.js" ) . DummyReplacementsMap ;
1011
1112const ERB_REGEX = / < % [ \s \S ] * ?% > / g;
13+ const HASH = "566513c5d83ac26e15414f2754" ; // to avoid collisions with user code
1214
1315/**
1416 * Transforms the given text into lintable text. We do this by stripping out
@@ -17,8 +19,10 @@ const ERB_REGEX = /<%[\s\S]*?%>/g;
1719 * location of messages in the postprocess step later.
1820 * @param {string } text text of the file
1921 * @param {string } filename filename of the file
20- * @param {string } dummyString dummy string to replace ERB tags with
21- * (this is language-specific)
22+ * @param {(id) => string } dummyString dummy string to replace ERB tags with
23+ * (this is language-specific). Should be
24+ * a function that takes an id and returns
25+ * a UNIQUE dummy string.
2226 * @returns {Array<{ filename: string, text: string }> } source code blocks to lint
2327 */
2428function preprocess ( text , filename , dummyString ) {
@@ -28,7 +32,9 @@ function preprocess(text, filename, dummyString) {
2832 let numAddLines = 0 ;
2933 let numDiffChars = 0 ;
3034 const offsetMap = new OffsetMap ( ) ;
35+ const dummyReplacementsMap = new DummyReplacementsMap ( ) ;
3136
37+ let matchedId = 0 ;
3238 while ( ( match = ERB_REGEX . exec ( text ) ) !== null ) {
3339 // Match information
3440 const startIndex = match . index ;
@@ -41,23 +47,28 @@ function preprocess(text, filename, dummyString) {
4147 numAddLines += matchLines . length - 1 ;
4248
4349 // Columns
50+ const dummy = dummyString ( matchedId ) ;
4451 const coordStartIndex = indexToColumn ( text , startIndex ) ;
45- const endColumnAfter = coordStartIndex . column + dummyString . length ;
52+ const endColumnAfter = coordStartIndex . column + dummy . length ;
4653 const coordEndIndex = indexToColumn ( text , endIndex ) ;
4754 const endColumnBefore = coordEndIndex . column ;
4855 const numAddColumns = endColumnBefore - endColumnAfter ;
56+ replaceTextWithDummy ( lintableTextArr , startIndex , matchLength - 1 , dummy ) ;
4957
50- replaceTextWithDummy ( lintableTextArr , startIndex , matchLength - 1 , dummyString ) ;
58+ const textWithErbSyntax = text . slice ( startIndex , endIndex ) ;
59+ dummyReplacementsMap . addMapping ( textWithErbSyntax , dummy ) ;
5160
5261 // Store in map
5362 const lineAfter = coordEndIndex . line - numAddLines ;
54- numDiffChars += dummyString . length - matchLength ;
63+ numDiffChars += dummy . length - matchLength ;
5564 const endIndexAfter = endIndex + numDiffChars ;
5665 offsetMap . addMapping ( endIndexAfter , lineAfter , numAddLines , numAddColumns ) ;
66+
67+ matchedId += 1 ;
5768 }
5869
5970 const lintableText = lintableTextArr . join ( "" ) ;
60- cache . add ( filename , text , lintableText , offsetMap ) ;
71+ cache . add ( filename , text , lintableText , offsetMap , dummyReplacementsMap ) ;
6172 return [ lintableText ] ;
6273}
6374
@@ -69,20 +80,24 @@ function preprocess(text, filename, dummyString) {
6980 * For the length of the match, subsequent characters are replaced with empty
7081 * strings in the array.
7182 */
72- function replaceTextWithDummy ( lintableTextArr , startIndex , length , dummyString ) {
73- lintableTextArr [ startIndex ] = dummyString ;
83+ function replaceTextWithDummy ( lintableTextArr , startIndex , length , dummy ) {
84+ lintableTextArr [ startIndex ] = dummy ;
7485 const replaceArgs = Array ( length ) . join ( "." ) . split ( "." ) ;
7586 // -> results in ['', '', '', '', ...]
7687 lintableTextArr . splice ( startIndex + 1 , length , ...replaceArgs ) ;
7788}
7889
7990function preprocessJs ( text , filename ) {
80- const dummyString = "/* eslint-disable */{}/* eslint-enable */" ;
91+ function dummyString ( id ) {
92+ return `/* eslint-disable */{}/* ${ HASH } ${ id } *//* eslint-enable */` ;
93+ }
8194 return preprocess ( text , filename , dummyString ) ;
8295}
8396
8497function preprocessHtml ( text , filename ) {
85- const dummyString = "<!-- -->" ;
98+ function dummyString ( id ) {
99+ return `<!-- ${ HASH } ${ id } -->` ;
100+ }
86101 return preprocess ( text , filename , dummyString ) ;
87102}
88103
0 commit comments