1

I have polluted one of my classes with too many functions. Now, I want to clean it up by introducing several namespaces.

Consider following example:

const Tool = {};

tool.fct1 = function() { /* ... */ };
tool.fct2 = function() { /* ... */ };
// ...
tool.fctN = function() { /* ... */ };

where many external modules make use of these functions

import {Tool} from '/path/to/tool.js'

const result = Tool.fctN();

Now, introducing namespaces would cause Tool to be more structured like this:

const Tool = {};

Tool.ns1 = {};
Tool.ns2 = {};

Tool.ns1.fct1 = function() { /* ... */ };
Tool.ns1.fct2 = function() { /* ... */ };
// ...
Tool.ns2.fctN = function() { /* ... */ };

So, the depending code has to change to the following structure:

import {Tool} from '/path/to/tool.js'

const result = Tool.ns2.fctN();


The problem is

that I tried several refactoring functionality in WebStorm but none allowed me to automatically co-refactor depending code.


Refactor.Rename

Obviously the first thing to try. Refactor -> rename fct1 to ns1.fct1 Result: WS prevented me from doing that and showed a message ns1.fct1 is not a valid identifier. (Works neither when ns1 being property of Tool nor when being not.)


Refactor.Change-siganture

Obviously changes only signature and not properties / variables.


Refactor.Move, Refactor.Copy, Refactor.Safe-Delete

No ways of changing code in this file as described above.


Refactor.extract

Extracting either does not change anything related to the current variable or extracts an inner variable, which does not help to introduce a namespace at all.


Refactor.Inline

Trying inline on Tool.fct1 shows message cannot inline complex expression evaluation.

Anybody found a solution to this so that I don't need to rewrite all my code by hand?

2
  • I think you should just try a simple find and replace. Use regex for more control, too. Commented Mar 23, 2018 at 11:38
  • Sounds reasonable. Will give hat a try but I am still curious if this is going to work using refactoring. Commented Mar 23, 2018 at 11:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.