WebStorm: turn off auto deletion for unused imprts - webstorm

I like the optimized imports a lot except for one behavior that I want to get rid of.
So if I make a bracket mistake in my JSX and use an imported library below this mistake it automatically removes this import on save. Then I fix the issue and now my imports are gone and I have to get them manually back (or copy my changes and command + z until they are back again and paste my new changes).
This is really annoying so I'm searching for the option to turn off deletion for unused imports in WebStorm.

Just found out that suppressing JavaScript and TypeScript | Imports and dependencies | Unused ES6 / TypeScript imports inspection for particular import or for the whole file with // noinspection ES6UnusedImports helps:
// noinspection ES6UnusedImports
import {Bar} from "bar";

Related

Multiple Transforms for VSCode Snippet

I've got a few snippets set up and working as I would like to, but I'm having a hard time getting one snippet to work where I believe I will need to have multiple transforms occur?
Essentially I have a TypeScript Interface defined for one of my components.
i.e. IRadioButtonListProps.ts which is inside of an Interfaces folder. The Interfaces folder has a sibling folder named Theme containing an interface named IRadioButtonListTheme.ts
Inside of IRadioButtonListProps I'm trying to stub out the entire interface. The snippet I currently have stubs out the interface like...
import * as React from 'react';
import IRadioButtonListPropsTheme from '../Theme/IRadioButtonListPropsTheme';
export interface IRadioButtonListPropsProps {
...props...
}
export default IRadioButtonListPropsProps;
The import line inside of the snippet is...
"import I${TM_FILENAME_BASE/(.*)\\..+$/$1/}Props from './Interfaces/I${TM_FILENAME_BASE/(.*)\\..+$/$1/}Props';"
What I'm trying to have happen and can't seem to figure out is how to also remove the word "Props". So instead of import IRadioButtonListPropsTheme... I would get import import IRadioButtonListTheme....
At the same time, I want to remove all extensions, including those of the form *.abc.abc ("two" extensions) and *.abc (one simple extension).
Is this possible?
It isn't crystal clear what you what but try:
"import ${TM_FILENAME/((\\w*)Props)*?(\\..*)/$2/}Theme from './Interfaces/${TM_FILENAME/((\\w*)Props)*?(\\..*)/$2/}Theme';"
which results in:
import IRadioButtonListTheme from './Interfaces/IRadioButtonListTheme';
from IRadioButtonListProps.ts
and
import CheckboxListTheme from './Interfaces/CheckboxListTheme';
from CheckboxListProps.test.tsx
[Edit] Here is a simpler version which I think also works:
"import ${TM_FILENAME/(Props)*?(\\..*)//}Theme from './Interfaces/${TM_FILENAME/(Props)*?(\\..*)//}Theme';"
match any "Props", if any, replace with nothing.
match from first \. to end of filename, replace with nothing.

How to prevent WebStorm from removing imports?

WebStorm is removing unused imports by default. How can I turn this off?
e.g. if I comment out an imported element, the import statement is auto-deleted. When I comment that element back in, I need to manually insert the necessary import statement again.
Often auto-insert of import statements does not help since WebStorm does not recognize that the element has to be imported...
Unused imports are always removed when optimizing imports, so, if you like to keep them, don't run Code | Optimize imports, and make sure that Optimize Imports is not enabled in Reformat Code dialog: Code | Show Reformat File dialog, clear the Optimize Imports checkbox

Autocomplete for import keyword is ommiting file extension

I am having a problem with WebStorm auto complete when using import keyword in JavaScript.
You can see an example here: https://i.gyazo.com/95adbf84c964663f715fc069ba1e1e8a.mp4
Basically when I auto complete following code:
import {loadLevel, loadSprite} from './loaders';
It doesn't add a file extension to loaders, which should look like this:
import {loadLevel, loadSprite} from './loaders.js';
I am using latest WebStorm version, I also tried to Invalidate cache and restarted IDE.
I am using JavaScript ES6 version in settings.
There are no other files called loaders with different extension anywhere else in my project.
If you need any more info, let me know.
Thanks
WebStorm supports completing file name with extension (when using completion in the from part). To enable completing file name with extension, set registry key commonjs.complete.required.filename.with.extension to true :
Open Help | Find Action... dialog
Type Registry, find Registry item in dropdown list and press Enter
Find commonjs.complete.required.filename.with.extension there, tick the checkbox
But this hidden option doesn't affect auto-imports, so, when auto-creating import statement from reference, extension won't be added - WEB-28741

Can ember-cli import json and other non-JS files?

Given that ember-cli uses ES6 modules syntax to import other JavaScript files, is it also possible to use this same syntax to import non-JavaScript files, such as JSON files or other text files?
Assume I have a JSON file named "foo.json" in my current directory. How could I import the contents of that file into a variable within my current JavaScript file? I've tried, without success, several variations of:
import foo from 'foo.json';
if (typeof foo === 'object') {
// Success
} else {
// import failed
}
Is it possible to import non-JavaScript files into the current file using the import statement or any other means?
No it's not. The ES6 modules syntax to import stuff also needs the requested object to be properly exported, which your json-files or text-files won't be. Also, since we don't really have ES6 in browsers yet, all those nifty statements are converted to commonjs-modules when building your project.
As I see it you have two options for this.
Putting the files in your public-folder and load them via ajax is the simple solution though not very elegant.
The cool option would be to add a preprocessor of your own that wraps your json-files in a proper export-statement as a build-step. That would be quite a lot of more work though and might require some deep diving into ember-cli and broccoli to attach it at the right time.
Yes, ember-cli can import JSON and non-JS files as long as you have the proper plugin.
This one allows the import of JSON files:
https://github.com/IvyApp/ember-cli-json-module
... and this one works for YAML: https://github.com/joankaradimov/ember-cli-yaml-module.

matplotlib style library not updating when .mplstyle files added/deleted

I'm trying to create my own matplotlib stylesheets but Python doesn't detect them. Even worse: when I moved one of the five default stylesheets (ggplot.mplstyle), Python kept reporting it was availabe.
I tried to reload the entire module but to no avail:
import matplotlib
reload(matplotlib)
from matplotlib import style as style
plt = matplotlib.pyplot
print plt.style.available
just keeps returning
[u'dark_background', u'bmh', u'grayscale', u'ggplot', u'fivethirtyeight']
How can I force a "refresh" of these styles?
P.s. I'm not a Python expert.
Just in case someone else stumbles on this post, this issue was reported and resolved here:
https://github.com/matplotlib/matplotlib/issues/3601
Basically, the style library looks for files in a subdirectory of the matplotlib config directory. On linux/osx system, this would be something like ~/.matplotlib/stylelib/<my-custom-style>.mplstyle.
Also, as #tcaswell suggested in comments, loading is done at import time, so style files added or edited after import will not work without a call to plt.style.reload_library().