简体   繁体   中英

TSLint unhappy with non-alphabetized import sources

I have a couple of TypeScript import sources that TSLint is unhappy about, because they are apparently not alphabetized.

import { DialogNoConfigurationFile } from './view/dialogs/dialog-no-configuration-file';
import { DisplayMain } from './view/display/display-main';
import { Global } from './business/global';
import { remote } from 'electron';
import { RequestResponse } from './data/model/request-response';
import { UserRequestResponse } from './data/model/user-request-response';

The error message from TSLint:

ERROR: /home/myuser/Documents/myproject/administration2/src/app.tsx:11:1 - Import sources within a group must be alphabetized.
ERROR: /home/myuser/Documents/myproject/administration2/src/app.tsx:12:1 - Import sources within a group must be alphabetized.

My package.json script command:

"lint": "tslint --project tsconfig.json --force"

Even though TSLint is unhappy about Global and remote , everything is ok with alphabetization. I believe TSLint must be unhappy about the capitalization or something similar. Is there anyway to check how TSLint is alphabetizing?

You could run tslint with --fix to sort the imports:

npx tslint --fix --project tsconfig.json src/app.tsx

And if you use VS Code, there's a tslint extension that allows you to apply individual fixes.

Assuming you use the default settings for the ordered-imports rule, your imports should be okay when sorted like this: (absolute before relative)

import { remote } from 'electron';
import { Global } from './business/global';
import { RequestResponse } from './data/model/request-response';
import { UserRequestResponse } from './data/model/user-request-response';
import { DialogNoConfigurationFile } from './view/dialogs/dialog-no-configuration-file';
import { DisplayMain } from './view/display/display-main';

For more importation you can check the documentation of the rule at https://palantir.github.io/tslint/rules/ordered-imports/ :

If no "groups" options is set, a default grouping is used of third-party, parent directories and the current directory. ("bar", "../baz", "./foo".)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM