1- /* eslint-disable */
2- import { includes } from 'lodash' ;
31import p5CodeAstAnalyzer from './p5CodeAstAnalyzer' ;
42import {
53 getContext ,
@@ -8,20 +6,10 @@ import {
86 getClassContext ,
97 isThisReference
108} from './renameVariableHelper' ;
11- const parser = require ( '@babel/parser' ) ;
12- const traverse = require ( '@babel/traverse' ) . default ;
139
14- export function handleRename ( fromPos , oldName , newName , cm ) {
15- if ( ! cm ) {
16- return ;
17- }
18- const ast = getAST ( cm ) ;
19- startRenaming ( cm , ast , fromPos , newName , oldName ) ;
20- }
10+ const traverse = require ( '@babel/traverse' ) . default ;
2111
2212function startRenaming ( cm , ast , fromPos , newName , oldName ) {
23- const code = cm . getValue ( ) ;
24- const fromIndex = cm . indexFromPos ( fromPos ) ;
2513 const {
2614 scopeToDeclaredVarsMap = { } ,
2715 userDefinedClassMetadata = { } ,
@@ -69,8 +57,7 @@ function startRenaming(cm, ast, fromPos, newName, oldName) {
6957 parent . type === 'ArrowFunctionExpression' ) &&
7058 parent . params . some ( ( p ) => p . type === 'Identifier' && p . name === oldName )
7159 ) {
72- if ( parent . params . includes ( node ) ) {
73- } else {
60+ if ( ! parent . params . includes ( node ) ) {
7461 return ;
7562 }
7663 }
@@ -125,29 +112,30 @@ function startRenaming(cm, ast, fromPos, newName, oldName) {
125112 isThis === isBaseThis &&
126113 baseContext === thisContext
127114 ) {
128- for ( const [ methodName , vars ] of Object . entries (
129- classMeta . methodVars || { }
130- ) ) {
131- if ( ! vars . includes ( oldName ) && thisContext === methodName ) {
132- shouldRenameMethodVar = true ;
115+ Object . entries ( classMeta . methodVars || { } ) . forEach (
116+ ( [ methodName , vars ] ) => {
117+ if ( ! vars . includes ( oldName ) && thisContext === methodName ) {
118+ const shouldRenameMethodVar = true ;
119+ }
133120 }
134- }
121+ ) ;
122+
135123 shouldRename =
136124 thisContext === baseContext &&
137- ( currentMethodName == 'constructor' || shouldRenameGlobalVar ) ;
125+ ( currentMethodName === 'constructor' || shouldRenameGlobalVar ) ;
138126 } else {
139- for ( const [ methodName , vars ] of Object . entries (
140- classMeta . methodVars || { }
141- ) ) {
142- if (
143- ! vars . includes ( oldName ) &&
144- baseMethodName === currentMethodName &&
145- isThis === isBaseThis &&
146- baseContext === thisContext
147- ) {
148- shouldRename = true ;
127+ Object . entries ( classMeta . methodVars || { } ) . forEach (
128+ ( [ methodName , vars ] ) => {
129+ if (
130+ ! vars . includes ( oldName ) &&
131+ baseMethodName === currentMethodName &&
132+ isThis === isBaseThis &&
133+ baseContext === thisContext
134+ ) {
135+ shouldRename = true ;
136+ }
149137 }
150- }
138+ ) ;
151139 }
152140
153141 // Rename constructor parameters, class fields, and method variables carefully
@@ -161,21 +149,31 @@ function startRenaming(cm, ast, fromPos, newName, oldName) {
161149
162150 if ( ! ( thisContext in userDefinedClassMetadata ) ) {
163151 const thisScopeVars = scopeToDeclaredVarsMap [ thisContext ] || { } ;
164- shouldRename = isGlobal && ! thisScopeVars . hasOwnProperty ( oldName ) ;
152+ shouldRename =
153+ isGlobal &&
154+ ! Object . prototype . hasOwnProperty . call ( thisScopeVars , oldName ) ;
165155 }
166156 shouldRenameGlobalVar = isGlobal && thisContext === 'global' ;
167157 }
168158 // Handle renaming outside classes
169159 else {
170160 const thisScopeVars = scopeToDeclaredVarsMap [ thisContext ] || { } ;
171161 const baseScopeVars = scopeToDeclaredVarsMap [ baseContext ] || { } ;
172- const globalScopeVars = scopeToDeclaredVarsMap [ ' global' ] || { } ;
162+ const globalScopeVars = scopeToDeclaredVarsMap . global || { } ;
173163
174164 const isInBaseScope = thisContext === baseContext ;
175165 const isShadowedLocally =
176- ! isInBaseScope && thisScopeVars . hasOwnProperty ( oldName ) ;
177- const isDeclaredInBaseScope = baseScopeVars . hasOwnProperty ( oldName ) ;
178- const isDeclaredGlobally = globalScopeVars . hasOwnProperty ( oldName ) ;
166+ ! isInBaseScope &&
167+ Object . prototype . hasOwnProperty . call ( thisScopeVars , oldName ) ;
168+ const isDeclaredInBaseScope = Object . prototype . hasOwnProperty . call (
169+ baseScopeVars ,
170+ oldName
171+ ) ;
172+ const isDeclaredGlobally = Object . prototype . hasOwnProperty . call (
173+ globalScopeVars ,
174+ oldName
175+ ) ;
176+
179177 const isThisGlobal = isGlobalReference (
180178 oldName ,
181179 thisContext ,
@@ -223,23 +221,24 @@ function startRenaming(cm, ast, fromPos, newName, oldName) {
223221 ! ( currentMethodName === 'constructor' )
224222 ) {
225223 const classMeta = userDefinedClassMetadata [ thisContext ] ;
226- for ( const [ methodName , vars ] of Object . entries (
227- classMeta . methodVars || { }
228- ) ) {
229- if ( ! vars . includes ( oldName ) && methodName === currentMethodName ) {
230- shouldRename = true ;
224+ Object . entries ( classMeta . methodVars || { } ) . forEach (
225+ ( [ methodName , vars ] ) => {
226+ if ( ! vars . includes ( oldName ) && methodName === currentMethodName ) {
227+ shouldRename = true ;
228+ }
231229 }
232- }
230+ ) ;
233231 } else {
234232 shouldRename =
235233 isInBaseScope ||
236234 ( ! isShadowedLocally &&
237- thisScopeVars . hasOwnProperty ( oldName ) === { } &&
235+ Object . prototype . hasOwnProperty . call ( thisScopeVars , oldName ) ===
236+ { } &&
238237 baseContext === 'global' ) ||
239238 ( baseContext === 'global' &&
240- ! thisScopeVars . hasOwnProperty ( oldName ) ) ||
239+ ! Object . prototype . hasOwnProperty . call ( thisScopeVars , oldName ) ) ||
241240 ( isDeclaredGlobally &&
242- ! thisScopeVars . hasOwnProperty ( oldName ) &&
241+ ! Object . prototype . hasOwnProperty . call ( thisScopeVars , oldName ) &&
243242 ! isDeclaredInBaseScope ) ;
244243
245244 shouldRenameGlobalVar =
@@ -261,8 +260,16 @@ function startRenaming(cm, ast, fromPos, newName, oldName) {
261260 ) ;
262261
263262 cm . operation ( ( ) => {
264- for ( const { from, to } of replacements ) {
263+ replacements . forEach ( ( { from, to } ) => {
265264 cm . replaceRange ( newName , from , to ) ;
266- }
265+ } ) ;
267266 } ) ;
268267}
268+
269+ export default function handleRename ( fromPos , oldName , newName , cm ) {
270+ if ( ! cm ) {
271+ return ;
272+ }
273+ const ast = getAST ( cm ) ;
274+ startRenaming ( cm , ast , fromPos , newName , oldName ) ;
275+ }
0 commit comments