Optimize Imports

Remove unused and repeated importPackage, importClass and importScript calls from Digital Script with Optimize Imports, on save, on commit or on the fly

Import calls pile up as scripts change. The plugin marks the ones a .ds or server .js file doesn't need, and Optimize Imports removes them, by hand, on save, on commit or as you type

Import calls the file does not need are gray: a call that repeats an earlier one, a class nothing uses, a package none of whose classes are used, a script none of whose functions are called. Code › Optimize Imports removes them all

What it removes

Code › Optimize Imports (Ctrl+Alt+O) removes the import calls the file doesn't need, then runs the IDE's JavaScript optimizer. A hint says how many went, such as "Removed 4 import calls"

CallRemoved when
Any import callAn earlier call imports the same class, package or path
importClassNothing uses its class
importPackageNothing uses any of its classes. A package outside dw stays
importScriptNothing calls any of its functions
A call with a wrong argumentNever

A name counts as used anywhere in the file, type annotations included. These importScript calls stay:

  • The script has no functions, or the path doesn't resolve
  • The file is itself imported by other scripts, which see the functions it imports

The editor shows the same calls in gray, from the Unused or repeated import call inspection, with a Remove the import call fix:

  • importPackage(dw.util) repeats an earlier import call
  • importPackage(dw.catalog) is not used
Before
/* Calculates the basket */
importPackage(dw.util);
importPackage(dw.util);
importClass(dw.order.BasketMgr);
importPackage(dw.catalog);
importScript('app_core:util/Log.ds');

var m = new HashMap();
After
/* Calculates the basket */
importPackage(dw.util);

var m = new HashMap();

How lines are handled

  • A call alone on its line takes the line with it
  • A blank line left next to another one, or at the top of the file, goes too
  • On a line shared with other code, only the call and the spaces beside it go
  • A call inside a function goes like any other
  • A call used as a value, as in var x = importPackage(dw.campaign), stays, since removing it would break the statement
Before
run(); importClass(dw.order.BasketMgr);
importClass(dw.catalog.ProductMgr); run();
function f() {
    importPackage(dw.catalog);
    return 1;
}
var x = importPackage(dw.campaign);
After
run();
run();
function f() {
    return 1;
}
var x = importPackage(dw.campaign);

Where it runs

TriggerWhere to turn it on
Code › Optimize ImportsAlways there
Reformat CodeOptimize imports in the Reformat File dialog
Saving a fileOptimize imports in Settings › Tools › Actions on Save
CommittingOptimize imports in the commit options
TypingOptimize imports on the fly, see On the fly

It changes .ds and server .js files and needs a license. Templates and client .js files keep their calls

On the fly

Open the settings

Settings › Languages & Frameworks › Digital Script

Check Optimize imports on the fly

It's in the Imports group, off by default, and saved for you only, not with the project

Edit as usual

A call that turns unused goes once the file settles. The removal is one undo step, Optimize Imports on the Fly

It waits until it's safe to change the file:

  • The caret isn't on an import call or the blank lines next to them
  • The file has no errors outside the import calls
  • Highlighting has finished, and no undo or redo is running
  • You changed the file. Files you only opened keep their calls

It needs the Unused or repeated import call inspection on. The IDE's own Optimize imports on the fly in Settings › Editor › General › Auto Import covers other languages and doesn't turn this one on

On this page