ske*_*ske 94 visual-studio-code angular
我被指派从最近离开公司的其他开发人员处获取一些混乱的代码.
我很好奇地问是否有一些Visual Studio Code插件或其他方法可以帮助我们快速有效地整理和组织导入和引用?
例如,可能有数百个这样的导入
import { AutoCompleteModule,InputSwitchModule } from 'primeng/primeng';
import { ListboxModule } from 'primeng/primeng';
Run Code Online (Sandbox Code Playgroud)
可以转换成类似的
import { AutoCompleteModule,
InputSwitchModule,
ListboxModule } from 'primeng/primeng';
Run Code Online (Sandbox Code Playgroud)
或者其他功能,比如从app.module或整个项目中的所有组件自动删除那些未使用的导入和声明?
感谢您的任何反馈!
Muh*_*dri 88
我希望这个视觉工作室代码扩展能满足您的需求:https://marketplace.visualstudio.com/items? itemName = rbbit.typescript-hero
它提供以下功能:
ken*_*nny 41
从VSCode v.1.24和TypeScript v.2.9开始:
对于Mac:option+ Shift+O
对于Win:Alt+ Shift+O
小智 34
为了能够检测未使用的导入,代码或变量,请确保在tsconfig.json文件中有此选项
"compilerOptions": {
"noUnusedLocals": true,
"noUnusedParameters": true
}
Run Code Online (Sandbox Code Playgroud)
安装了typescript编译器,如果没有安装它:
npm install -g typescript
Run Code Online (Sandbox Code Playgroud)
并且在Vcode中安装了tslint扩展,这对我有用,但在启用后我注意到CPU使用量的增加,特别是在大项目上.
我还建议使用typescript hero扩展来组织你的导入.
Jay*_*Kan 30
如果您是Visual Studio的重度用户,则只需打开您的首选项设置,然后将以下内容添加到settings.json中:
...
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
....
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助!
There are already so many good answers on this thread! I am going to post this to help anybody trying to do this automatically! To automatically remove unused imports for the whole project this article was really helpful to me.
In the article the author explains it like this:
Make a stand alone tslint file that has the following in it:
{
"extends": ["tslint-etc"],
"rules": {
"no-unused-declaration": true
}
}
Run Code Online (Sandbox Code Playgroud)
Then run the following command to fix the imports:
tslint --config tslint-imports.json --fix --project .
Run Code Online (Sandbox Code Playgroud)
Consider fixing any other errors it throws. (I did)
Then check the project works by building it:
ng build
Run Code Online (Sandbox Code Playgroud)
or
ng build name_of_project --configuration=production
Run Code Online (Sandbox Code Playgroud)
End: If it builds correctly, you have successfully removed imports automatically!
NOTE: This only removes unnecessary imports. It does not provide the other features that VS Code does when using one of the commands previously mentioned.