Giter VIP home page Giter VIP logo

tslint-lines-between-class-members's Introduction

tslint-lines-between-class-members

npm CircleCI

Custom rule for TSLint to enforce blank lines between class methods - achieves a similar thing to lines-between-class-members in ESLint

Install

# yarn
yarn add --dev tslint-lines-between-class-members

# npm
npm install --save-dev tslint-lines-between-class-members

Configuration

Update your tslint.json config file, adding the new rules directory and the new rule
You can choose to specify the exact number of lines you want between methods, or leave it to default to just checking there is at least 1

{
  "rulesDirectory": [
    "node_modules/tslint-lines-between-class-members"
  ],
  "rules": {
    "lines-between-class-members": true,
  }
}

Config Examples

At least one line:

"lines-between-class-members": true

Exactly one line:

"lines-between-class-members": [true, 1]

Exactly twenty two lines:

"lines-between-class-members": [true, 22]

tslint-lines-between-class-members's People

Contributors

caroso1222 avatar chinchiheather avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tslint-lines-between-class-members's Issues

Support Tab Indentation

If you are using tab indentation for your project your indentations are changed to spaces while fixing.

Example:

class NoLineAndTabIndent {
	constructor() { }
	method() { }
}

becomes...

class NoLineAndTabIndent {
	constructor() { }
+
-	method() { }
+ method() { }
}

Since I already fixed it for myself I can offer you a pr.

fix seem not respect tab size

After running tslint --fix, this is the diff:

     }
-    public pause (): void {
+
+  public pause (): void {

As you can see, the rule insert two space, and not four
(my tslint.conf have "indent": [ true, "spaces", 4 ])

Linting fails for constructor of generic type after prettier multi-line format

Prettier formats some generic type definitions by placing the generic constraints on multiple lines.

This causes tslint-lines-between-class-members to fail linting.

The error is must have 1 new line(s) between class methods, see docs for how to configure which appears on the constructor() { line.

I believe this is due to how you are checking the previous line.

Passes

export class PermissionsStore extends EntityStore<PermissionsState, Permission> {
  constructor() {
    super();
  }
}

Fails

export class PermissionsStore extends EntityStore<
  PermissionsState,
  Permission
> {
  constructor() {
    super();
  }
}

I also ran into this situation where Prettier puts the implements keyword on its own line which causes this lint rule to fail at the line constructor() { }.

interface SomeInterface {
    name: string;
}

export class SomeClass
  implements SomeInterface {
  constructor() { }

  name = 'test';
}

Release v1.3.5 does not install without yarn

Hi,

The 1.3.5 release on npm won't install on a standard node installation. The postinstall script tries to do

cd ./integration && yarn

which results in:

The system cannot find the path specified.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postinstall: `cd ./integration && yarn`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Surely doing anything with yarn should not be required on postinstall?

Bug: Expects blank line at overloading

Used versions

Code

class X {
  foo(x: number, y: string): string;
  foo( y: string): number;
  foo(...args: any): string | number {
    return 42;
  }
}

Expected behavior
No error

Actual behavior
It wants to have a blank line between the function declarations.

fix changes it to

class X {
  foo(x: number, y: string): string;
  
  foo( y: string): number;
  
  foo(...args: any): string | number {
    return 42;
  }
}

Failing when linebreak is present

ngOnInit does not have a line break between class members according to the rule.

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  ngOnInit() {
  }

}

must have blank line between class methods (lines-between-class-members)'

image

export class Debug {
  
  public static Log(...args: any[]) {
    console.log(args);
  }

  public static LogError(...args: any[]) {
    console.error(args);
  }

  public static LogWarning(...args: any[]) {
    console.warn(args);
  }
}

this following is my tslint.json

{
  "extends": ["tslint:latest", "tslint-config-prettier"],

  "rulesDirectory": ["node_modules/tslint-lines-between-class-members"],

  "rules": {
    "quotemark": [true, "single"],
    "indent": [true],
    "interface-name": [false],
    "arrow-parens": false,
    "object-literal-sort-keys": false,
    "no-unused-expression": false,
    "max-classes-per-file": false,
    "no-construct": false,
    "no-empty": false,
    "no-console": false,
    "prefer-for-of": false,
    "variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore"],
    "no-implicit-dependencies": false,
    "lines-between-class-members": true
  }
}

Bug: expects blank linke when default exporting an object

Using the rule "lines-between-class-members": [true, 1]:

export default {
  foo() {},
}

must have 1 new line(s) between class methods, see docs for how to configure (lines-between-class-members)

Adding a blank line above foo() {} "fixes" it, but conflicts with no-empty-line-after-opening-brace and it's not really the case that it is "between" class members. This is not even a class, so the rule shouldn't apply anyway.

Disable rule

TSLint allows me to disable rules for "whole file", "next line", or "this line". I assumed this rule could also be disabled.

I tried:

// tslint:disable-next-line:lines-between-class-members

and

// tslint:disable-line:lines-between-class-members

Which don't work. Please advise?

question

Youy dont happen to know about anyone who made a rule for "lines-between-class-methods" ?

Wants initial blank line when class declaration is broken over lines

export class ExtremelyOverlongClassNameCausingLineBreak
  implements BaseClass {
  constructor() {}
}

When prettier cleans up a class declaration to span multiple lines (or if you do so manually), the rule will decide here that constructor is missing a leading blank line (which, unfortunately, prettier will then remove)

Error if there is a comment above the class

I am getting an error if there is a comment above the class definition. If I log the previousLine in this case, I see that it calculates the previousLine wrongly. Please look:

bug

fix should not add space into the empty line

After running tslint --fix the tools seems add new line when missing, but the newly created line is not empty, it have two blank space at the beginning, causing tslint to fail again.

Only applies to methods, not members

export class FooBar {
  foo;
  bar;
}

This doesn't report anything even though there is no new line between the members foo and bar.

If this rule is only for class methods, it should maybe not be named lines-between-class-members?

Would be nice to make it work for any type of member and then you could make it configurable in case someone only wants it to apply to methods.

Not supporting arrow functions in a class?

I use arrow functions within my class, but "lines-between-class-members" doesn't work.

code:

class user {
    public func1 = () => {
        // some codes
    }
    public func2 = () => {
        // some codes
    }
    public func3(){
        // some codes
    }
}

what I expect:

class user {
    public func1 = () => {
        // some codes
    }

    public func2 = () => {
        // some codes
    }

    public func3(){
        // some codes
    }
}

However, the output now is:

class user {
    public func1 = () => {
        // some codes
    }
    public func2 = () => {
        // some codes
    }

    public func3(){
        // some codes
    }
}

We see that func1 and func2 are not formatted correctly while func3 can be formatted.
It seems that "lines-between-class-members" doesn't support arrow functions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.