1- import { checkVueTemplate } from './utils/rule.js' ;
2- import { getParenthesizedRange } from './utils/parentheses.js' ;
1+ import {
2+ checkVueTemplate ,
3+ getParenthesizedRange ,
4+ getTokenStore ,
5+ } from './utils/index.js' ;
36import { replaceNodeOrTokenAndSpacesBefore , fixSpaceAroundKeyword } from './fix/index.js' ;
47import builtinErrors from './shared/builtin-errors.js' ;
58import typedArray from './shared/typed-array.js' ;
@@ -53,9 +56,11 @@ const strictStrategyConstructors = [
5356 'FinalizationRegistry' ,
5457] ;
5558
56- const replaceWithFunctionCall = ( node , sourceCode , functionName ) => function * ( fixer ) {
57- const { tokenStore, instanceofToken} = getInstanceOfToken ( sourceCode , node ) ;
59+ const replaceWithFunctionCall = ( node , context , functionName ) => function * ( fixer ) {
5860 const { left, right} = node ;
61+ const tokenStore = getTokenStore ( context , node ) ;
62+ const instanceofToken = tokenStore . getTokenAfter ( left , isInstanceofToken ) ;
63+ const { sourceCode} = context ;
5964
6065 yield * fixSpaceAroundKeyword ( fixer , node , sourceCode ) ;
6166
@@ -67,9 +72,11 @@ const replaceWithFunctionCall = (node, sourceCode, functionName) => function * (
6772 yield * replaceNodeOrTokenAndSpacesBefore ( right , '' , fixer , sourceCode , tokenStore ) ;
6873} ;
6974
70- const replaceWithTypeOfExpression = ( node , sourceCode ) => function * ( fixer ) {
71- const { tokenStore, instanceofToken} = getInstanceOfToken ( sourceCode , node ) ;
75+ const replaceWithTypeOfExpression = ( node , context ) => function * ( fixer ) {
7276 const { left, right} = node ;
77+ const tokenStore = getTokenStore ( context , node ) ;
78+ const instanceofToken = tokenStore . getTokenAfter ( left , isInstanceofToken ) ;
79+ const { sourceCode} = context ;
7380
7481 // Check if the node is in a Vue template expression
7582 const vueExpressionContainer = sourceCode . getAncestors ( node ) . findLast ( ancestor => ancestor . type === 'VExpressionContainer' ) ;
@@ -89,19 +96,6 @@ const replaceWithTypeOfExpression = (node, sourceCode) => function * (fixer) {
8996 yield fixer . replaceTextRange ( rightRange , safeQuote + sourceCode . getText ( right ) . toLowerCase ( ) + safeQuote ) ;
9097} ;
9198
92- const getInstanceOfToken = ( sourceCode , node ) => {
93- const { left} = node ;
94-
95- let tokenStore = sourceCode ;
96- let instanceofToken = tokenStore . getTokenAfter ( left , isInstanceofToken ) ;
97- if ( ! instanceofToken && sourceCode . parserServices . getTemplateBodyTokenStore ) {
98- tokenStore = sourceCode . parserServices . getTemplateBodyTokenStore ( ) ;
99- instanceofToken = tokenStore . getTokenAfter ( left , isInstanceofToken ) ;
100- }
101-
102- return { tokenStore, instanceofToken} ;
103- } ;
104-
10599/** @param {import('eslint').Rule.RuleContext } context */
106100const create = context => {
107101 const {
@@ -117,8 +111,6 @@ const create = context => {
117111 : include ,
118112 ) ;
119113
120- const { sourceCode} = context ;
121-
122114 return {
123115 /** @param {import('estree').BinaryExpression } node */
124116 BinaryExpression ( node ) {
@@ -141,12 +133,12 @@ const create = context => {
141133 || ( constructorName === 'Error' && useErrorIsError )
142134 ) {
143135 const functionName = constructorName === 'Array' ? 'Array.isArray' : 'Error.isError' ;
144- problem . fix = replaceWithFunctionCall ( node , sourceCode , functionName ) ;
136+ problem . fix = replaceWithFunctionCall ( node , context , functionName ) ;
145137 return problem ;
146138 }
147139
148140 if ( constructorName === 'Function' ) {
149- problem . fix = replaceWithTypeOfExpression ( node , sourceCode ) ;
141+ problem . fix = replaceWithTypeOfExpression ( node , context ) ;
150142 return problem ;
151143 }
152144
@@ -155,7 +147,7 @@ const create = context => {
155147 {
156148 messageId : MESSAGE_ID_SWITCH_TO_TYPE_OF ,
157149 data : { type : constructorName . toLowerCase ( ) } ,
158- fix : replaceWithTypeOfExpression ( node , sourceCode ) ,
150+ fix : replaceWithTypeOfExpression ( node , context ) ,
159151 } ,
160152 ] ;
161153 return problem ;
0 commit comments