Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSLint - Preventing error: The key is not sorted alphabetically

Tags:

I'm doing a test app with Ionic2 / Cordova / Typescript / Angular. I'm using tslint 5.6.0.

I'm using the following module: https://www.npmjs.com/package/tslint

Focusing on just one file...

when linting the following file:

import { NgModule, ErrorHandler } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { IonicApp, IonicModule, IonicErrorHandler } from "ionic-angular";
import { MyApp } from "./app.component";

import { AboutPage } from "../pages/about/about";
import { ContactPage } from "../pages/contact/contact";
import { HomePage } from "../pages/home/home";
import { TabsPage } from "../pages/tabs/tabs";

import { StatusBar } from "@ionic-native/status-bar";
import { SplashScreen } from "@ionic-native/splash-screen";

@NgModule( {
    declarations: [
        MyApp,
        AboutPage,
        ContactPage,
        HomePage,
        TabsPage,
    ],
    imports: [
        BrowserModule,
        IonicModule.forRoot( MyApp ),
    ],
    bootstrap: [ IonicApp ],
    entryComponents: [
        MyApp,
        AboutPage,
        ContactPage,
        HomePage,
        TabsPage,
    ],
    providers: [
        StatusBar,
        SplashScreen,
        { provide: ErrorHandler, useClass: IonicErrorHandler },
    ],
})
export class AppModule { }

I get:

The key 'bootstrap' is not sorted alphabetically
RuleFailurePosition { position: 790, lineAndCharacter: { line: 25, character: 4 } }
RuleFailurePosition { position: 799, lineAndCharacter: { line: 25, character: 13 } }

I'm using the following options:

{
    "extends": "tslint:recommended",
    "rules": {
        "no-duplicate-variable": true,
        "max-line-length": {
            "options": [120]
        },
        "ordered-imports": false,
        "new-parens": true,
        "no-arg": true,
        "no-bitwise": true,
        "no-conditional-assignment": true,
        "no-consecutive-blank-lines": false,
        "no-console": {
            "options": [
                "debug",
                "info",
                "log",
                "time",
                "timeEnd",
                "trace"
            ]
        }
    },
    "jsRules": {
        "max-line-length": {
            "options": [120]
        }
    }
}

What option do I need to configure on TSLint to prevent showing up this error?

like image 733
davidesp Avatar asked Aug 21 '17 08:08

davidesp


People also ask

How do I disable TSLint in typescript?

disable TSLint in the IDE settings (Languages and Frameworks -> TypeScript -> TSLint) to avoid the same errors being shown twice. For additional configuration options, please see the typescript-tslint-plugin docs.

What is object-literal-sort-keys?

Rule: object-literal-sort-keys Checks ordering of keys in object literals. When using the default alphabetical ordering, additional blank lines may be used to group object properties together while keeping the elements within each group in alphabetical order. To opt out of this use ignore-blank-lines option.

How do I Opt Out of the default alphabetical ordering?

When using the default alphabetical ordering, additional blank lines may be used to group object properties together while keeping the elements within each group in alphabetical order. To opt out of this use ignore-blank-lines option. By default, this rule checks that keys are in alphabetical order. The following may optionally be passed:

What is the default number of keys in Lint?

Default is true. minKeys - Specifies the minimum number of keys that an object should have in order for the object’s unsorted keys to produce an error. Default is 2, which means by default all objects with unsorted keys will result in lint errors.


1 Answers

The rule failing here seems to be object-literal-sort-keys.

You should be able to disable it in the rules section of your config file by adding:

"object-literal-sort-keys": false

You can find all the tslint rules here.

like image 86
toskv Avatar answered Feb 20 '23 00:02

toskv