Skip to content

Commit

Permalink
feat(eslint-config-angular): support line statements (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Dec 7, 2021
1 parent cb7307f commit 6a5bdfe
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/eslint-config-angular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
'./internal/base-typescript',
'./internal/import',
'./internal/member-ordering',
'./internal/line-statements',
'./internal/extraneous-class',
],
};
1 change: 1 addition & 0 deletions packages/eslint-config-angular/internal/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ module.exports = {
* @note: [*.js, *.ts]
*/
'no-param-reassign': 'off',
'no-case-declarations': 'error',
},
};
2 changes: 2 additions & 0 deletions packages/eslint-config-angular/internal/extraneous-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
overrides: [
{
files: ['*.ts'],
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/no-extraneous-class': [
'error',
Expand All @@ -16,6 +17,7 @@ module.exports = {
},
{
files: ['*.spec.ts', '*.fixture.ts'],
plugins: ['@typescript-eslint'],
rules: {
/**
* @note: because in tests, there may be special fixtures
Expand Down
56 changes: 56 additions & 0 deletions packages/eslint-config-angular/internal/line-statements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module.exports = {
overrides: [
{
files: ['*.ts'],
plugins: ['@typescript-eslint'],
rules: {
'lines-around-comment': [
'error',
{
beforeBlockComment: false,
afterBlockComment: false,
beforeLineComment: false,
afterLineComment: false,
allowBlockStart: true,
allowBlockEnd: true,
allowObjectStart: true,
allowObjectEnd: true,
allowArrayStart: true,
allowArrayEnd: true,
allowClassStart: true,
allowClassEnd: true,
applyDefaultIgnorePatterns: true,
},
],
'lines-between-class-members': 'off',
'@typescript-eslint/lines-between-class-members': [
'error',
'always',
{ exceptAfterSingleLine: true, exceptAfterOverload: true },
],
'padding-line-between-statements': 'off',
'@typescript-eslint/padding-line-between-statements': [
'error',
{ blankLine: 'always', prev: '*', next: 'block' },
{ blankLine: 'always', prev: 'block', next: '*' },
{ blankLine: 'always', prev: '*', next: 'block-like' },
{ blankLine: 'always', prev: 'block-like', next: '*' },
{ blankLine: 'always', prev: '*', next: 'return' },
{ blankLine: 'always', prev: 'directive', next: '*' },
{ blankLine: 'always', prev: '*', next: ['interface', 'type'] },
{ blankLine: 'always', prev: ['const', 'let', 'var'], next: '*' },
{
blankLine: 'any',
prev: ['const', 'let', 'var', 'export'],
next: ['const', 'let', 'var', 'export'],
},
{ blankLine: 'any', prev: '*', next: ['case', 'default'] },
{ blankLine: 'any', prev: ['case', 'default'], next: '*' },
{ blankLine: 'any', prev: '*', next: 'class' },
{ blankLine: 'any', prev: 'class', next: '*' },
{ blankLine: 'any', prev: 'directive', next: 'directive' },
],
},
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class A1 {}
class B1 {}

function a1() {}

function b1() {}

function c1() {
const value = '123';

return value + 'etc';
}

const d1: string | null = null;
let e1: string | null = null;

switch (d1) {
case '1': {
e1 = '1';
break;
}
case '2': {
e1 = '2';
break;
}
default: {
e1 = d1;
break;
}
}

let sum = 0;

for (let i = 0; i < 5; i++) {
sum += i;
}

sum = 0;

class LinesBetween {
x;
y;

foo(x?: string);
foo(x?: string, y?: string);
foo() {
//...
}

bar() {
//...
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class A2 {}
class B2 {}

function a2() {}
function b2() {}
function c2() {
const value = '123';
return value + 'etc';
}

const d2: string | null = null;
let e2: string | null = null;
switch (d2) {
case '1': {
e1 = '1';
break;
}

case '2': {
e1 = '2';
break;
}

default: {
e1 = d1;
break;
}
}

let sum2: number = 0;
for (let i = 0; i < 5; i++) {
sum2 += i;
}
sum2 = 0;

class LinesBetween2 {
x;
y;
foo(x?: string);
foo(x?: string, y?: string);
foo() {
//...
}
bar() {
//...
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`imports / happy path happy 1`] = `""`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`line statements / unhappy path unhappy 1`] = `
"error: Expected blank line before this statement (@typescript-eslint/padding-line-between-statements) at packages/eslint-config-angular/test/line-statements/__fixtures__/line-statements-unhappy.fixture.ts:5:1:
3 |
4 | function a2() {}
> 5 | function b2() {}
| ^
6 | function c2() {
7 | const value = '123';
8 | return value + 'etc';
error: Expected blank line before this statement (@typescript-eslint/padding-line-between-statements) at packages/eslint-config-angular/test/line-statements/__fixtures__/line-statements-unhappy.fixture.ts:6:1:
4 | function a2() {}
5 | function b2() {}
> 6 | function c2() {
| ^
7 | const value = '123';
8 | return value + 'etc';
9 | }
error: Expected blank line before this statement (@typescript-eslint/padding-line-between-statements) at packages/eslint-config-angular/test/line-statements/__fixtures__/line-statements-unhappy.fixture.ts:8:5:
6 | function c2() {
7 | const value = '123';
> 8 | return value + 'etc';
| ^
9 | }
10 |
11 | const d2: string | null = null;
error: Expected blank line before this statement (@typescript-eslint/padding-line-between-statements) at packages/eslint-config-angular/test/line-statements/__fixtures__/line-statements-unhappy.fixture.ts:13:1:
11 | const d2: string | null = null;
12 | let e2: string | null = null;
> 13 | switch (d2) {
| ^
14 | case '1': {
15 | e1 = '1';
16 | break;
error: Expected blank line before this statement (@typescript-eslint/padding-line-between-statements) at packages/eslint-config-angular/test/line-statements/__fixtures__/line-statements-unhappy.fixture.ts:31:1:
29 |
30 | let sum2: number = 0;
> 31 | for (let i = 0; i < 5; i++) {
| ^
32 | sum2 += i;
33 | }
34 | sum2 = 0;
error: Expected blank line before this statement (@typescript-eslint/padding-line-between-statements) at packages/eslint-config-angular/test/line-statements/__fixtures__/line-statements-unhappy.fixture.ts:34:1:
32 | sum2 += i;
33 | }
> 34 | sum2 = 0;
| ^
35 |
36 | class LinesBetween2 {
37 | x;
error: Expected blank line between class members (@typescript-eslint/lines-between-class-members) at packages/eslint-config-angular/test/line-statements/__fixtures__/line-statements-unhappy.fixture.ts:44:5:
42 | //...
43 | }
> 44 | bar() {
| ^
45 | //...
46 | }
47 | }
7 errors found.
7 errors potentially fixable with the \`--fix\` option."
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ESlint from 'eslint';
import path from 'path';

describe('imports / happy path', () => {
const cli = new ESlint.CLIEngine({
cwd: path.join(__dirname, '..'),
useEslintrc: false,
baseConfig: {
extends: ['../internal/base', '../internal/line-statements'],
},
});

it('happy', () => {
const codeframe = cli.getFormatter('codeframe');
const report = cli.executeOnFiles([
path.join(__dirname, './__fixtures__/line-statements-happy.fixture.ts'),
]);

expect(codeframe(report.results)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ESlint from 'eslint';
import path from 'path';

describe('line statements / unhappy path', () => {
const cli = new ESlint.CLIEngine({
cwd: path.join(__dirname, '..'),
useEslintrc: false,
baseConfig: {
extends: ['../internal/base', '../internal/line-statements'],
},
});

it('unhappy', () => {
const codeframe = cli.getFormatter('codeframe');
const report = cli.executeOnFiles([
path.join(__dirname, './__fixtures__/line-statements-unhappy.fixture.ts'),
]);

expect(codeframe(report.results)).toMatchSnapshot();
});
});

0 comments on commit 6a5bdfe

Please sign in to comment.