feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
98
frontend/config/eslint-config/src/define-config.ts
Normal file
98
frontend/config/eslint-config/src/define-config.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
import type { Linter } from 'eslint';
|
||||
import { includeIgnoreFile } from '@eslint/compat';
|
||||
|
||||
type ESLintConfigMode = 'web' | 'node' | 'base';
|
||||
|
||||
type ESLintConfig = Linter.Config;
|
||||
|
||||
type Rules = Linter.RulesRecord;
|
||||
|
||||
export interface EnhanceESLintConfig extends ESLintConfig {
|
||||
/**
|
||||
* project root dir
|
||||
*/
|
||||
packageRoot: string;
|
||||
/**
|
||||
* config mode
|
||||
*/
|
||||
preset: ESLintConfigMode;
|
||||
/** overrides config */
|
||||
overrides: ESLintConfig[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Define an ESLint config.
|
||||
*
|
||||
* @param config ESLint config.
|
||||
* @returns ESLint config.
|
||||
*/
|
||||
export const defineConfig = (config: EnhanceESLintConfig): ESLintConfig[] => {
|
||||
const {
|
||||
packageRoot,
|
||||
preset,
|
||||
overrides = [],
|
||||
ignores = [],
|
||||
rules,
|
||||
settings,
|
||||
|
||||
...userConfig
|
||||
} = config;
|
||||
|
||||
if (!packageRoot) {
|
||||
throw new Error('should provider "packageRoot" params');
|
||||
}
|
||||
|
||||
const defaultRules: Rules = {};
|
||||
|
||||
const ignorePath = path.resolve(packageRoot, '.gitignore');
|
||||
|
||||
return [
|
||||
...require(`../eslint.config.${preset}.js`),
|
||||
|
||||
fs.existsSync(ignorePath) ? includeIgnoreFile(ignorePath) : {},
|
||||
// root ignore file
|
||||
includeIgnoreFile(path.resolve(__dirname, '../../../.gitignore')),
|
||||
|
||||
{
|
||||
ignores,
|
||||
},
|
||||
|
||||
{
|
||||
files: ['**/*.?(m|c)?(j|t)s?(x)'],
|
||||
settings: {
|
||||
...settings,
|
||||
'import/resolver': {
|
||||
typescript: {
|
||||
project: packageRoot,
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: {},
|
||||
rules: {
|
||||
...defaultRules,
|
||||
...rules,
|
||||
},
|
||||
...userConfig,
|
||||
},
|
||||
...overrides,
|
||||
];
|
||||
};
|
||||
12
frontend/config/eslint-config/src/index.js
Normal file
12
frontend/config/eslint-config/src/index.js
Normal file
@@ -0,0 +1,12 @@
|
||||
require('sucrase/register/ts');
|
||||
|
||||
const { defineConfig } = require('./define-config');
|
||||
|
||||
// node@16 没有 structuredClone 方法导致报错:
|
||||
// ReferenceError: Error while loading rule '@typescript-eslint/naming-convention': structuredClone is not defined
|
||||
// 此处做个简单 polyfill
|
||||
if (typeof structuredClone === 'undefined') {
|
||||
global.structuredClone = obj => JSON.parse(JSON.stringify(obj));
|
||||
}
|
||||
|
||||
module.exports = { defineConfig };
|
||||
Reference in New Issue
Block a user