Skip to content

Commit

Permalink
Added tests, fixed naming-convention config
Browse files Browse the repository at this point in the history
  • Loading branch information
burtek committed Nov 21, 2021
1 parent 98b0082 commit 50fe7f9
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/.gitignore
/.markdownlint.json
/.eslintrc
/eslint.test.js
/tests
38 changes: 6 additions & 32 deletions eslint-config-base.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const makeNamingRules = require('./helpers/make-naming-config');

module.exports = {
extends: [
'plugin:@typescript-eslint/eslint-recommended',
Expand Down Expand Up @@ -60,37 +62,7 @@ module.exports = {
}],
'@typescript-eslint/method-signature-style': 'error',
'camelcase': 'off',
'@typescript-eslint/naming-convention': ['error',
{
'selector': 'default',
'format': ['camelCase'],
'leadingUnderscore': 'forbid',
'trailingUnderscore': 'forbid'
},
{
'selector': 'variable',
'modifiers': ['const', 'global'],
'format': ['camelCase', 'UPPER_CASE']
},
{
'selector': 'function',
'modifiers': ['global'],
'format': ['camelCase', 'PascalCase']
},
{
'selector': 'parameter',
'modifiers': ['unused'],
'leadingUnderscore': 'allow',
'format': null
},
{
'selector': 'enumMember',
'format': ['UPPER_CASE']
},
{
'selector': 'typeLike',
'format': ['PascalCase']
}],
'@typescript-eslint/naming-convention': makeNamingRules(),
'@typescript-eslint/no-base-to-string': 'error',
'@typescript-eslint/no-confusing-non-null-assertion': 'error',
'@typescript-eslint/no-confusing-void-expression': 'error',
Expand Down Expand Up @@ -200,7 +172,9 @@ module.exports = {
'@typescript-eslint/no-extra-parens': ['error', 'all', {
'enforceForArrowConditionals': false,
'enforceForSequenceExpressions': false,
'enforceForFunctionPrototypeMethods': false
'enforceForFunctionPrototypeMethods': false,
'ignoreJSX': 'multi-line',
'nestedBinaryExpressions': false
}],
'@typescript-eslint/no-extra-semi': 'error',
'@typescript-eslint/no-implied-eval': 'error',
Expand Down
53 changes: 3 additions & 50 deletions eslint-config-react.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const makeNamingRules = require('./helpers/make-naming-config');

const linkComponents = [
{
'name': 'Link', 'linkAttribute': 'to'
Expand Down Expand Up @@ -63,55 +65,7 @@ module.exports = {
{
files: ['*.tsx'],
rules: {
'@typescript-eslint/naming-convention': ['error',
{
'selector': 'default',
'format': ['camelCase'],
'leadingUnderscore': 'forbid',
'trailingUnderscore': 'forbid'
},
{
'selector': 'variable',
'modifiers': ['const', 'global'],
'format': ['camelCase', 'UPPER_CASE']
},
{
'selector': 'variable',
'modifiers': ['const', 'global', 'exported'],
'types': ['function'],
'format': ['camelCase', 'UPPER_CASE', 'PascalCase']
},
{
'selector': 'variable',
'modifiers': ['const', 'global', 'exported'],
'types': ['function'],
'filter': '^use',
'format': ['camelCase']
},
{
'selector': 'function',
'modifiers': ['global'],
'format': ['camelCase', 'PascalCase']
},
{
'selector': 'function',
'filter': '^use',
'format': ['camelCase']
},
{
'selector': 'parameter',
'modifiers': ['unused'],
'leadingUnderscore': 'allow',
'format': null
},
{
'selector': 'enumMember',
'format': ['UPPER_CASE']
},
{
'selector': 'typeLike',
'format': ['PascalCase']
}]
'@typescript-eslint/naming-convention': makeNamingRules({ isReact: true })
}
},
{
Expand Down Expand Up @@ -163,7 +117,6 @@ module.exports = {
'allow': 'literal'
}],
'react/jsx-pascal-case': 'error',
'react/jsx-space-before-closing': 'error',
'react/jsx-tag-spacing': 'error',
'react/jsx-wrap-multilines': ['error', {
'declaration': 'parens-new-line',
Expand Down
23 changes: 0 additions & 23 deletions eslint.test.js

This file was deleted.

65 changes: 65 additions & 0 deletions helpers/make-naming-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const configs = [
{
// default
'selector': 'default',
'format': ['camelCase'],
'leadingUnderscore': 'forbid',
'trailingUnderscore': 'forbid'
},
{
// const global variables can be UPPER_CASE
'selector': 'variable',
'modifiers': ['const', 'global'],
'format': ['camelCase', 'UPPER_CASE']
},
{
// unused parameters start with _
'selector': 'parameter',
'modifiers': ['unused'],
'leadingUnderscore': 'allow',
'format': null
},
{
// enums
'selector': 'enumMember',
'format': ['UPPER_CASE']
},
{
// types
'selector': 'typeLike',
'format': ['PascalCase']
}
];
const reactConfigs = [
{
// const global variables of function type can be PascalCase for React Components
'selector': 'variable',
'modifiers': ['const', 'global'],
'types': ['function'],
'format': ['camelCase', 'UPPER_CASE', 'PascalCase']
},
{
// Components passed as props
'selector': 'property',
'filter': 'Comp(onent)?$',
'format': ['camelCase', 'PascalCase']
},
{
// react hooks defined as const variables
'selector': 'variable',
'types': ['function'],
'filter': '^use',
'format': ['camelCase']
},
{
// react hooks defined as function
'selector': 'function',
'filter': '^use',
'format': ['camelCase']
}
];

module.exports = ({ isReact = false } = {}, level = 'error') => [level].concat(
configs,
isReact ? reactConfigs : []
);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"private": false,
"scripts": {
"test": "node eslint.test.js",
"test": "node tests/run-tests.js",
"preversion": "yarn test",
"version": "auto-changelog -p && git add CHANGELOG.md"
},
Expand Down
59 changes: 59 additions & 0 deletions tests/run-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-env node */
/* eslint-disable no-console */

const { ESLint } = require('eslint');
const fs = require('fs');
const path = require('path');

/**
* @returns {promise is PromiseRejectedResult}
*/
function filterRejected(promise) {
return promise.status === 'rejected'
}

async function getErrors(configFile) {
const cli = new ESLint({
overrideConfig: {
extends: [path.resolve(configFile)],
parserOptions: {
project: './tests/tsconfig.json'
}
}
});

const results = await Promise.allSettled([
cli.lintText('', { filePath: 'file.js' }),
cli.lintText('', { filePath: 'file.jsx' }),
cli.lintText('', { filePath: 'file.ts' }),
cli.lintText('', { filePath: 'file.tsx' })
]);
const rejectReasons = results.filter(filterRejected).map(promise => promise.reason)

if (rejectReasons.length > 0) {
throw rejectReasons
}
}

async function run() {
const files = await fs.promises.readdir('.', { withFileTypes: true });
const configs = files.filter(file => file.isFile() && file.name.startsWith('eslint-config') && file.name.endsWith('.js'));

let errored = false;

configs.forEach(async f => {
try {
await getErrors(f.name);
} catch (errors) {
console.error('Error in config file', f.name);
console.error(errors);

errored = true;
}
});

if (errored) {
process.exit(1);
}
}
run();
3 changes: 3 additions & 0 deletions tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compilerOptions": {}
}

0 comments on commit 50fe7f9

Please sign in to comment.