@@ -41,46 +41,6 @@ let prettier;
4141// Helpers
4242// ------------------------------------------------------------------------------
4343
44- /**
45- * Gets the location of a given index in the source code for a given context.
46- * @param {RuleContext } context - The ESLint rule context.
47- * @param {number } index - An index in the source code.
48- * @returns {Object } An object containing numeric `line` and `column` keys.
49- */
50- function getLocFromIndex ( context , index ) {
51- // If `sourceCode.getLocFromIndex` is available from ESLint, use it - added
52- // in ESLint 3.16.0.
53- const sourceCode = context . getSourceCode ( ) ;
54- if ( typeof sourceCode . getLocFromIndex === 'function' ) {
55- return sourceCode . getLocFromIndex ( index ) ;
56- }
57- const text = sourceCode . getText ( ) ;
58- if ( typeof index !== 'number' ) {
59- throw new TypeError ( 'Expected `index` to be a number.' ) ;
60- }
61- if ( index < 0 || index > text . length ) {
62- throw new RangeError ( 'Index out of range.' ) ;
63- }
64- // Loosely based on
65- // https://github.com/eslint/eslint/blob/18a519fa/lib/ast-utils.js#L408-L438
66- const lineEndingPattern = / \r \n | [ \r \n \u2028 \u2029 ] / g;
67- let offset = 0 ;
68- let line = 0 ;
69- let match ;
70- while ( ( match = lineEndingPattern . exec ( text ) ) ) {
71- const next = match . index + match [ 0 ] . length ;
72- if ( index < next ) {
73- break ;
74- }
75- line ++ ;
76- offset = next ;
77- }
78- return {
79- line : line + 1 ,
80- column : index - offset
81- } ;
82- }
83-
8444/**
8545 * Converts invisible characters to a commonly recognizable visible form.
8646 * @param {string } str - The string with invisibles to convert.
@@ -225,7 +185,7 @@ function generateDifferences(source, prettierSource) {
225185 * @returns {void }
226186 */
227187function reportInsert ( context , offset , text ) {
228- const pos = getLocFromIndex ( context , offset ) ;
188+ const pos = context . getSourceCode ( ) . getLocFromIndex ( offset ) ;
229189 const range = [ offset , offset ] ;
230190 context . report ( {
231191 message : 'Insert `{{ code }}`' ,
@@ -245,8 +205,8 @@ function reportInsert(context, offset, text) {
245205 * @returns {void }
246206 */
247207function reportDelete ( context , offset , text ) {
248- const start = getLocFromIndex ( context , offset ) ;
249- const end = getLocFromIndex ( context , offset + text . length ) ;
208+ const start = context . getSourceCode ( ) . getLocFromIndex ( offset ) ;
209+ const end = context . getSourceCode ( ) . getLocFromIndex ( offset + text . length ) ;
250210 const range = [ offset , offset + text . length ] ;
251211 context . report ( {
252212 message : 'Delete `{{ code }}`' ,
@@ -268,8 +228,10 @@ function reportDelete(context, offset, text) {
268228 * @returns {void }
269229 */
270230function reportReplace ( context , offset , deleteText , insertText ) {
271- const start = getLocFromIndex ( context , offset ) ;
272- const end = getLocFromIndex ( context , offset + deleteText . length ) ;
231+ const start = context . getSourceCode ( ) . getLocFromIndex ( offset ) ;
232+ const end = context
233+ . getSourceCode ( )
234+ . getLocFromIndex ( offset + deleteText . length ) ;
273235 const range = [ offset , offset + deleteText . length ] ;
274236 context . report ( {
275237 message : 'Replace `{{ deleteCode }}` with `{{ insertCode }}`' ,
0 commit comments