Skip to content

Commit

Permalink
Support for ESLint Flat Config (#15)
Browse files Browse the repository at this point in the history
* - Support for ESLint Flat Config

* Change files

* - Updating just-scripts version

* - Converting to Flat Config

* Change files

* Change files

* - Fixing legacy file name
  • Loading branch information
frgarc authored Feb 29, 2024
1 parent 5b42b3c commit 945df92
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Support for ESLint Flat Config",
"packageName": "@minecraft/core-build-tasks",
"email": "frgarc@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Using flat config",
"packageName": "@minecraft/math",
"email": "frgarc@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Support for ESLint Flat Config",
"packageName": "eslint-plugin-minecraft-linting",
"email": "frgarc@microsoft.com",
"dependentChangeType": "patch"
}
8 changes: 0 additions & 8 deletions libraries/math/.eslintrc.js

This file was deleted.

3 changes: 3 additions & 0 deletions libraries/math/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import configMinecraftScripting from 'eslint-config-minecraft-scripting';

export default [...configMinecraftScripting];
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tools/core-build-tasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@microsoft/api-extractor": "^7.38.3",
"esbuild": "^0.20.1",
"dotenv": "^16.0.2",
"just-scripts": "^2.2.1",
"prettier": "^2.8.2",
"rimraf": "^3.0.2",
"vitest": "^0.34.6",
Expand All @@ -27,9 +28,8 @@
"devDependencies": {
"@types/node": "^14.0.0 || ^16.0.0 || ^18.0.0",
"@types/rimraf": "^3.0.2",
"just-scripts": "^2.1.3",
"ts-node": "^10.9.1",
"tsconfig": "*",
"typescript": "^5.2.2"
}
}
}
54 changes: 47 additions & 7 deletions tools/core-build-tasks/src/tasks/coreLint.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,61 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { execSync } from 'child_process';
import { existsSync } from 'fs';
import type { TaskFunction } from 'just-scripts';
import { condition, eslintTask, prettierCheckTask, prettierTask, series, task } from 'just-scripts';
import { TaskFunction, condition, logger, prettierCheckTask, prettierTask, series, task } from 'just-scripts';
import path from 'path';
import process from 'process';

const LEGACY_CONFIG_FILES = ['.eslintrc.js'];
const FLAT_CONFIG_FILES = ['eslint.config.js', 'eslint.config.mjs', 'eslint.config.cjs'];
const POSSIBLE_CONFIG_FILES = [...LEGACY_CONFIG_FILES, ...FLAT_CONFIG_FILES];

function getConfigFilePath(): string | undefined {
for (const file of POSSIBLE_CONFIG_FILES) {
const configPath = path.resolve(process.cwd(), file);
if (existsSync(configPath)) {
return configPath;
}
}

return undefined;
}

function eslintTask(files: string[], fix?: boolean): TaskFunction {
return () => {
const configFilePath = getConfigFilePath();
if (configFilePath) {
// Setting ESLINT_USE_FLAT_CONFIG environment variable to indicate if the config file is flat or not.
// ESLint is not able to determine the type in all the cases, so we need to help it.
process.env['ESLINT_USE_FLAT_CONFIG'] = FLAT_CONFIG_FILES.some(file => configFilePath.endsWith(file))
? 'true'
: 'false';
const cmd = ['eslint', ...files, '--config', configFilePath, ...(fix ? ['--fix'] : []), '--color'].join(
' '
);
logger.info(`Running command: ${cmd}`);
return execSync(cmd, { stdio: 'inherit' });
}

// no-op if the config file does not exist.
return Promise.resolve();
};
}

export function coreLint(files: string[], fix?: boolean): TaskFunction {
task('verify-lint', () => {
// If the process working directory does not have an `.eslintrc.js` file, fail the build
const lintConfig = path.resolve(process.cwd(), '.eslintrc.js');
if (!existsSync(lintConfig)) {
throw new Error(`.eslintrc.js not found at ${lintConfig}.`);
// If the process working directory does not have an eslint configuration file, fail the build:
// https://eslint.org/docs/latest/use/configure/configuration-files-new
if (!getConfigFilePath()) {
throw new Error(
`ESLint config file not found at ${process.cwd()}. Possible values: [${POSSIBLE_CONFIG_FILES.join(
', '
)}]`
);
}
});
task('eslint', eslintTask({ files, fix }));
task('eslint', eslintTask(files, fix));
task('prettier-fix', prettierTask({ files }));
task('prettier-check', prettierCheckTask({ files }));

Expand Down
35 changes: 0 additions & 35 deletions tools/eslint-config-minecraft-scripting/index.js

This file was deleted.

63 changes: 63 additions & 0 deletions tools/eslint-config-minecraft-scripting/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import eslintConfigPrettier from 'eslint-config-prettier';
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
import eslint from '@eslint/js';
import globals from 'globals';
import header from 'eslint-plugin-header';
import minecraftLinting from 'eslint-plugin-minecraft-linting';
import tsEslint from '@typescript-eslint/eslint-plugin';
import tsParser from '@typescript-eslint/parser';

export default [
eslintConfigPrettier,
eslint.configs.recommended,
{
files: ['**/*.{ts,tsx,js,jsx}', '*.ts'],
languageOptions: {
parser: tsParser,
parserOptions: {
ecmaVersion: 'latest',
project: './tsconfig.json',
},
globals: {
...globals.node,
},
},
plugins: {
header,
tsEslint,
'@typescript-eslint': tsEslint,
unicorn: eslintPluginUnicorn,
'minecraft-linting': minecraftLinting,
},
rules: {
...tsEslint.configs['eslint-recommended'].rules,
...tsEslint.configs['recommended'].rules,
'unicorn/no-abusive-eslint-disable': 'error',
'unicorn/no-null': ['error', { checkStrictEquality: true }],
'@typescript-eslint/no-empty-function': 'off',
'no-unused-vars': 'off',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-floating-promises': ['error', { ignoreVoid: true }],
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
'@typescript-eslint/restrict-template-expressions': ['error', { allowNumber: true, allowBoolean: true }],
eqeqeq: ['error', 'always'],
'@typescript-eslint/no-unsafe-assignment': 'error',
'@typescript-eslint/no-unsafe-member-access': 'error',
'@typescript-eslint/no-unsafe-call': 'error',
'header/header': [
2,
'line',
[' Copyright (c) Microsoft Corporation.', ` Licensed under the MIT License.`],
1,
],
'minecraft-linting/avoid-unnecessary-command': 'error',
},
},
];
2 changes: 1 addition & 1 deletion tools/eslint-config-minecraft-scripting/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "eslint-config-minecraft-scripting",
"version": "0.0.1",
"main": "index.js",
"main": "index.mjs",
"license": "MIT",
"private": true,
"dependencies": {
Expand Down
8 changes: 0 additions & 8 deletions tools/eslint-plugin-minecraft-linting/.eslintrc.js

This file was deleted.

3 changes: 3 additions & 0 deletions tools/eslint-plugin-minecraft-linting/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import configMinecraftScripting from 'eslint-config-minecraft-scripting';

export default [...configMinecraftScripting];
4 changes: 2 additions & 2 deletions tools/eslint-plugin-minecraft-linting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"eslint-plugin-header": "^3.1.1",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-unicorn": "^42.0.0",
"just-scripts": "^2.1.3",
"just-scripts": "^2.2.1",
"tsconfig": "*"
},
"publishConfig": {
"access": "public"
}
}
}
7 changes: 7 additions & 0 deletions tools/eslint-plugin-minecraft-linting/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

import AvoidUnnecessaryCommand from './Rules/AvoidUnnecessaryCommand';

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
const { name, version }: { name: string; version: string } = require('../package.json');

module.exports = {
meta: {
name,
version,
},
rules: {
'avoid-unnecessary-command': AvoidUnnecessaryCommand,
},
Expand Down

0 comments on commit 945df92

Please sign in to comment.