chinchiheather / tslint-lines-between-class-members Goto Github PK
View Code? Open in Web Editor NEWCustom rule for TSLint to enforce blank lines between class methods - achieves a similar thing to lines-between-class-members in ESLint
Custom rule for TSLint to enforce blank lines between class methods - achieves a similar thing to lines-between-class-members in ESLint
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?
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 ]
)
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?
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.
Youy dont happen to know about anyone who made a rule for "lines-between-class-methods" ?
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';
}
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.
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;
}
}
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.
Ref: #19
Re-opening as 1.3.2 did not completely fix the issue; please reference #19 (comment) for examples of scenarios still exhibiting the issue.
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() {
}
}
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.
The 1.3.3 package on npm does not work due to missing file linesBetweenClassMembersRule.js
Many thanks for maintaining this :)
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.
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)
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
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.