Skip to content

Commit

Permalink
feat(eslint)!: flat config
Browse files Browse the repository at this point in the history
  • Loading branch information
Badisi committed Jul 10, 2023
1 parent 2c26a18 commit e595f54
Show file tree
Hide file tree
Showing 25 changed files with 334 additions and 130 deletions.
File renamed without changes.
10 changes: 10 additions & 0 deletions configs/angular/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import moderate from './moderate.js';
import recommended from './recommended.js';

/** @type { import('../../index.js').HugConfig } */
export default {
configs: {
moderate,
recommended
}
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
/** @type { import('eslint').Linter.FlatConfig } */
export default {
"plugins": [
"@angular-eslint"
],
Expand Down
File renamed without changes.
37 changes: 19 additions & 18 deletions rules/es6.js → configs/es6.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
module.exports = {
"env": {
"es6": true
},
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"generators": false,
"objectLiteralDuplicateProperties": false
}
import unusedImports from 'eslint-plugin-unused-imports';
import js from '@eslint/js';

/** @type { import('eslint').Linter.FlatConfig } */
const config = {
plugins: {
"unused-imports": unusedImports
},
"plugins": [
"unused-imports"
],
"extends": [
"eslint:recommended"
],
"rules": {
rules: {
// Apply default recommended rules from Eslint
...js.configs.recommended.rules,

// Enforce or disallow the use of braces around arrow function body
// https://eslint.org/docs/rules/arrow-body-style
"arrow-body-style": "error",
Expand Down Expand Up @@ -454,3 +447,11 @@ module.exports = {
"use-isnan": "error"
}
};

/** @type { import('../index.js').HugConfig } */
export default {
configs: {
moderate: config,
recommended: config
}
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions configs/typescript/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import moderate from './moderate.js';
import recommended from './recommended.js';

/** @type { import('../../index.js').HugConfig } */
export default {
configs: {
moderate,
recommended
}
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const { namingConventions } = require('./utils');

module.exports = {
"extends": "./recommended",
"rules": {
import recommended from './recommended.js';

/** @type { import('eslint').Linter.FlatConfig } */
export default {
...recommended,
rules: {
// Require explicit accessibility modifiers on class properties and methods
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md
"@typescript-eslint/explicit-member-accessibility": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
const { namingConventions } = require('./utils');

module.exports = {
"plugins": [
import typescriptEslint from '@typescript-eslint';

/** @type { import('eslint').Linter.FlatConfig } */
export default {
/*"plugins": [
"@typescript-eslint"
],
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"rules": {
],*/
plugins: {
typescriptEslint
},
rules: {
"max-len": "off",
"no-shadow": "off",
"no-empty": "error",

// Enforce consistent brace style for blocks
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Linter } from 'eslint';

export interface HugConfig<T = Linter.FlatConfig> {
readonly configs: {
readonly recommended: T;
readonly moderate: T;
};
}

declare const hug: HugConfig<Linter.FlatConfig[]> & {
readonly es6: HugConfig;
readonly ts: HugConfig;
};
export = hug;
90 changes: 58 additions & 32 deletions _base.js → index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,70 @@
* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2022 HUG
*/
'use strict';
import { findUpSync } from 'find-up';
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
import path from 'node:path';

// This is a workaround for vscode not finding tsconfig.eslint.json when a workspace is opened
// instead of the root folder of the project
const filename = 'tsconfig.eslint.json';
const tsconfigEslintJson = require('find-up').sync(filename, { cwd: __dirname }) || filename;
import typescriptParser from '@typescript-eslint/parser';

import ts from './configs/typescript/index.js';
import es6 from './configs/es6.js';
import angular from './configs/angular.js';
import rxjs from './configs/rxjs.js';
import extras from './configs/extras.js';


const require = createRequire(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));

// This is a workaround for https://github.com/eslint/eslint/issues/3458
require('@rushstack/eslint-patch/modern-module-resolution');
// require('@rushstack/eslint-patch/levelrn-module-resolution');

// const isPackageInstalled = (name) => {
// try { require(name); return true; } catch { return false; }
// };

// const needCypress = isPackageInstalled('cypress');
const needCypress = false;


const isPackageInstalled = (name) => {
try { require(name); return true; } catch { return false; }
// This is a workaround for vscode not finding the `tsconfig.eslint.json` at the root of a project
// when the project is opened through a vscode's workspace
const filename = 'tsconfig.eslint.json';
const tsconfigEslintJson = findUpSync(filename, { cwd: __dirname }) || filename;

/** @type { (level: 'moderate' | 'recommended') => import('eslint').Linter.FlatConfig[] } */
const config = (level) => {
return [{
files: ['**/*.ts'],
ignores: ['e2e/**/*.ts'],
languageOptions: {
// @ts-ignore
parser: typescriptParser,
parserOptions: {
project: [tsconfigEslintJson]
}
},
...es6.configs[level],
...ts.configs[level],
...angular.configs[level]
...rxjs.configs[level],
...extras.configs[level]
}];
};

const needCypress = isPackageInstalled('cypress');
/** @type { import('./index.js') } */
export default {
configs: {
moderate: config('moderate'),
recommended: config('recommended'),
},
es6,
ts
};

module.exports = (mode = 'recommended') => {
const base2 = (level = 'recommended') => {
return {
"env": {
"browser": true,
Expand All @@ -33,27 +80,6 @@ module.exports = (mode = 'recommended') => {
"package-lock.json"
],
"overrides": [
{
"files": [
"**/*.ts"
],
"excludedFiles": [
"e2e/**/*.ts"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": [
tsconfigEslintJson
]
},
"extends": [
require.resolve("./rules/es6"),
require.resolve(`./rules/typescript/${mode}`),
require.resolve(`./rules/angular/${mode}`),
require.resolve(`./rules/rxjs/${mode}`),
require.resolve("./rules/extras")
]
},
{
"files": [
"e2e/**/*.ts"
Expand All @@ -66,7 +92,7 @@ module.exports = (mode = 'recommended') => {
},
"extends": [
require.resolve("./rules/es6"),
require.resolve(`./rules/typescript/${mode}`),
require.resolve(`./rules/typescript/${level}`),
require.resolve("./rules/extras"),
(needCypress) ? require.resolve("./rules/cypress") : undefined
].filter(Boolean)
Expand Down
12 changes: 12 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"checkJs": true,
"target": "ES2019",
"module": "ESNext",
"moduleResolution": "node"
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}
10 changes: 0 additions & 10 deletions moderate.js

This file was deleted.

Loading

0 comments on commit e595f54

Please sign in to comment.