Skip to content

Commit

Permalink
fix(ts): disable no-underscore-dangle since _ is a required prefix …
Browse files Browse the repository at this point in the history
…for private members
  • Loading branch information
GerkinDev committed Feb 14, 2022
1 parent 8388031 commit b4ed1ce
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 162 deletions.
1 change: 1 addition & 0 deletions src/config-fragments/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export = {
'jsdoc/check-tag-names': [ 'error', {
definedTags: [ 'category', 'typeparam', ...jsdoc.rules['jsdoc/check-tag-names'][1].definedTags ],
} ],
'no-underscore-dangle': 'off',
},
}
/* eslint-enable max-len */
266 changes: 106 additions & 160 deletions test/config/typescript/code.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
import { MockContext } from '../../utils';
import { getSingleTsFileResult } from '../../utils';

describe( 'Typescript', () => {
describe( 'Code', () => {
describe( 'jsdoc', () => {
describe( 'Function', () => {
it( 'should detect missing overload documentation on function', async () => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/test.ts', `
const result = await getSingleTsFileResult( './src/test.ts', `
export function foo( bar: string ): string;
export function foo( bar: number ): number;
export function foo( this: string, bar: string | number ): string | number {
return bar + this;
}
` );
const tsconfig = ctx.addFile( './tsconfig.json', JSON.stringify( { files: [ testedFile ] } ) );
const errors = await ctx.getErrors(
dir => ( {
extends: '@knodes/eslint-config/ts',
parserOptions: { project: tsconfig, tsconfigRootDir: dir },
} ),
testedFile );
expect( errors[0].messages ).toEqual( [
expect( result.messages ).toEqual( [
expect.objectContaining( { messageId: 'missingJsDoc' } ),
] );
} );
it( 'should detect overload documentation on function', async () => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/test.ts', `
const result = await getSingleTsFileResult( './src/test.ts', `
/**
* Do some foo
*
Expand All @@ -38,20 +29,12 @@ export function foo( this: string, bar: string | number ): string | number {
return bar + this;
}
` );
const tsconfig = ctx.addFile( './tsconfig.json', JSON.stringify( { files: [ testedFile ] } ) );
const errors = await ctx.getErrors(
dir => ( {
extends: '@knodes/eslint-config/ts',
parserOptions: { project: tsconfig, tsconfigRootDir: dir },
} ),
testedFile );
expect( errors[0].messages ).toEqual( [] );
expect( result.messages ).toEqual( [] );
} );
} );
describe( 'Class', () => {
it( 'should detect missing overload documentation on class', async () => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/test.ts', `
const result = await getSingleTsFileResult( './src/test.ts', `
export class Qux {
public foo( bar: string ): string;
public foo( bar: number ): number;
Expand All @@ -60,20 +43,12 @@ export class Qux {
}
}
` );
const tsconfig = ctx.addFile( './tsconfig.json', JSON.stringify( { files: [ testedFile ] } ) );
const errors = await ctx.getErrors(
dir => ( {
extends: '@knodes/eslint-config/ts',
parserOptions: { project: tsconfig, tsconfigRootDir: dir },
} ),
testedFile );
expect( errors[0].messages ).toEqual( [
expect( result.messages ).toEqual( [
expect.objectContaining( { messageId: 'missingJsDoc' } ),
] );
} );
it( 'should detect overload documentation on class', async () => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/test.ts', `
const result = await getSingleTsFileResult( './src/test.ts', `
export class Qux {
/**
* Do some foo
Expand All @@ -87,184 +62,155 @@ export class Qux {
}
}
` );
const tsconfig = ctx.addFile( './tsconfig.json', JSON.stringify( { files: [ testedFile ] } ) );
const errors = await ctx.getErrors(
dir => ( {
extends: '@knodes/eslint-config/ts',
parserOptions: { project: tsconfig, tsconfigRootDir: dir },
} ),
testedFile );
expect( errors[0].messages ).toEqual( [] );
expect( result.messages ).toEqual( [] );
} );
} );
describe( 'TypeDoc tags', () => {
it.each( [[ 'template', 'typeparam' ]] )( 'should report to replace TypeDoc tag %s with %s', async ( tag, prefered ) => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/test.ts', `
const result = await getSingleTsFileResult( './src/test.ts', `
/**
* @${tag} some content
*/
export const foo = 1;
` );
const tsconfig = ctx.addFile( './tsconfig.json', JSON.stringify( { files: [ testedFile ] } ) );
const errors = await ctx.getErrors(
dir => ( {
extends: '@knodes/eslint-config/ts',
parserOptions: { project: tsconfig, tsconfigRootDir: dir },
} ),
testedFile );
expect( errors[0].messages ).toEqual( [ expect.objectContaining( {
expect( result.messages ).toEqual( [ expect.objectContaining( {
ruleId: 'jsdoc/check-tag-names',
message: `Invalid JSDoc tag (preference). Replace "${tag}" JSDoc tag with "${prefered}".`,
} ) ] );
} );
it.each( [ 'category', 'typeparam', 'usage' ] )( 'should not report TypeDoc tag %s', async tag => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/test.ts', `
const errors = await getSingleTsFileResult( './src/test.ts', `
/**
* @${tag} some content
*/
export const foo = 1;
` );
const tsconfig = ctx.addFile( './tsconfig.json', JSON.stringify( { files: [ testedFile ] } ) );
const errors = await ctx.getErrors(
dir => ( {
extends: '@knodes/eslint-config/ts',
parserOptions: { project: tsconfig, tsconfigRootDir: dir },
} ),
testedFile );
expect( errors[0].messages ).toEqual( [] );
expect( errors.messages ).toEqual( [] );
} );
} );
} );
describe( 'typescript', () => {
it( 'should not trigger typescript error on JS file', async () => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/foo.js', `
const extraConfig = {
eslint: {
env: { node: true },
rules: {
'jsdoc/require-jsdoc': 'off' as const,
},
},
};
it( 'should not trigger typescript result on JS file', async () => {
const result = await getSingleTsFileResult( './src/foo.js', `
module.exports = {};
` );
const errors = await ctx.getErrors(
() => ( {
env: { node: true },
extends: '@knodes/eslint-config/ts',
} ),
testedFile );
expect( errors[0].messages ).toEqual( [] );
`, extraConfig );
expect( result.messages ).toEqual( [] );
} );
describe( 'Naming convention', () => {
it( 'should warn for naming conventions on class methods', async () => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/foo.ts', `
describe.each( [
[ 'Instance', '' ],
[ 'Static', 'static' ],
] )( '%s', ( _, prefix ) => {
it( 'should warn for naming conventions on class methods', async () => {
const result = await getSingleTsFileResult( './src/foo.ts', `
export class Foo {
public bar(): string {
public ${prefix} bar(): string {
return 'bar';
}
protected baz(): string {
protected ${prefix} baz(): string {
return 'bar';
}
private qux(): string {
private ${prefix} qux(): string {
return 'bar';
}
}
` );
const tsconfig = ctx.addFile( './tsconfig.json', JSON.stringify( { files: [ testedFile ] } ) );
const errors = await ctx.getErrors(
dir => ( {
env: { node: true },
parserOptions: { project: tsconfig, tsconfigRootDir: dir },
extends: '@knodes/eslint-config/ts',
rules: {
'jsdoc/require-jsdoc': 'off',
},
} ),
testedFile );
expect( errors[0].messages ).toEqual( [
expect.objectContaining( {
line: 9,
messageId: 'missingUnderscore',
message: expect.stringMatching( /`qux`/ ),
ruleId: '@typescript-eslint/naming-convention',
} ),
] );
} );
it( 'should warn for naming conventions on class get/set', async () => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/foo.ts', `
`, extraConfig );
expect( result.messages ).toEqual( [
expect.objectContaining( {
line: 9,
messageId: 'missingUnderscore',
message: expect.stringMatching( /`qux`/ ),
ruleId: '@typescript-eslint/naming-convention',
} ),
] );
} );
it( 'should warn for naming conventions on class get/set', async () => {
const result = await getSingleTsFileResult( './src/foo.ts', `
export class Foo {
public get bar(): string {
public ${prefix} get bar(): string {
return 'bar';
}
public set bar( x: string ): void {
public ${prefix} set bar( x: string ): void {
x += 'a';
}
protected get baz(): string {
protected ${prefix} get baz(): string {
return 'bar';
}
protected set baz( x: string ): void {
protected ${prefix} set baz( x: string ): void {
x += 'a';
}
private get qux(): string {
private ${prefix} get qux(): string {
return 'bar';
}
private set qux( x: string ): void {
private ${prefix} set qux( x: string ): void {
x += 'a';
}
}
` );
const tsconfig = ctx.addFile( './tsconfig.json', JSON.stringify( { files: [ testedFile ] } ) );
const errors = await ctx.getErrors(
dir => ( {
env: { node: true },
parserOptions: { project: tsconfig, tsconfigRootDir: dir },
extends: '@knodes/eslint-config/ts',
rules: {
'jsdoc/require-jsdoc': 'off',
},
} ),
testedFile );
expect( errors[0].messages ).toEqual( [
expect.objectContaining( {
line: 15,
messageId: 'missingUnderscore',
message: expect.stringMatching( /`qux`/ ),
ruleId: '@typescript-eslint/naming-convention',
} ),
expect.objectContaining( {
line: 18,
messageId: 'missingUnderscore',
message: expect.stringMatching( /`qux`/ ),
ruleId: '@typescript-eslint/naming-convention',
} ),
] );
`, extraConfig );
expect( result.messages ).toEqual( [
expect.objectContaining( {
line: 15,
messageId: 'missingUnderscore',
message: expect.stringMatching( /`qux`/ ),
ruleId: '@typescript-eslint/naming-convention',
} ),
expect.objectContaining( {
line: 18,
messageId: 'missingUnderscore',
message: expect.stringMatching( /`qux`/ ),
ruleId: '@typescript-eslint/naming-convention',
} ),
] );
} );
it( 'should warn for naming conventions on class properties', async () => {
const result = await getSingleTsFileResult( './src/foo.ts', `
export class Foo {
public ${prefix} readonly bar = 'bar';
protected ${prefix} readonly baz = 'bar';
private ${prefix} readonly qux = 'bar';
}
`, extraConfig );
expect( result.messages ).toEqual( [
expect.objectContaining( {
line: 5,
messageId: 'missingUnderscore',
message: expect.stringMatching( /`qux`/ ),
ruleId: '@typescript-eslint/naming-convention',
} ),
] );
} );
} );
it( 'should warn for naming conventions on class properties', async () => {
const ctx = new MockContext();
const testedFile = ctx.addFile( './src/foo.ts', `
it( 'should not warn for naming conventions on same class static', async () => {
const result = await getSingleTsFileResult( './src/foo.ts', `
export class Foo {
public readonly bar = 'bar';
protected readonly baz = 'bar';
private readonly qux = 'bar';
private static _prop: string;
private static get _getset(): string {
return this._prop;
}
private static set _getset( val: string ): void {
this._prop = val;
}
private static _method(): string {
return 'bar';
}
public method(){
let ret = '';
ret += Foo._prop;
ret += Foo._getset;
ret += Foo._method();
return ret;
}
}
` );
const tsconfig = ctx.addFile( './tsconfig.json', JSON.stringify( { files: [ testedFile ] } ) );
const errors = await ctx.getErrors(
dir => ( {
env: { node: true },
parserOptions: { project: tsconfig, tsconfigRootDir: dir },
extends: '@knodes/eslint-config/ts',
rules: {
'jsdoc/require-jsdoc': 'off',
},
} ),
testedFile );
expect( errors[0].messages ).toEqual( [
expect.objectContaining( {
line: 5,
messageId: 'missingUnderscore',
message: expect.stringMatching( /`qux`/ ),
ruleId: '@typescript-eslint/naming-convention',
} ),
] );
`, extraConfig );
expect( result.messages ).toEqual( [] );
} );
} );
} );
Expand Down
2 changes: 2 additions & 0 deletions test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './mock-context';
export * from './ts';
4 changes: 2 additions & 2 deletions test/utils.ts → test/utils/mock-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { ensureDir, mkdirp, pathExists, readFile, rm, writeFile } from 'fs-extra
import { globbySync } from 'globby';
import { nanoid } from 'nanoid';

const allSrc = globbySync( '**/*.ts', { cwd: resolve( __dirname, '../src' ) } );
const allSrc = globbySync( '**/*.ts', { cwd: resolve( __dirname, '../../src' ) } );
allSrc.forEach( src => {
jest.mock(
`@knodes/eslint-config/${src.replace( '.ts', '.js' )}`,
() => jest.requireActual( resolve( __dirname, '../src', src ) ),
() => jest.requireActual( resolve( __dirname, '../../src', src ) ),
{ virtual: true } );
} );

Expand Down
Loading

0 comments on commit b4ed1ce

Please sign in to comment.