diff --git a/eslint.config.cjs b/eslint.config.cjs new file mode 100644 index 000000000000..afe715d2d4a2 --- /dev/null +++ b/eslint.config.cjs @@ -0,0 +1,202 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib 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. +*/ + +'use strict'; + +// MODULES // + +var path = require( 'path' ); +var globals = require( 'globals' ); +var tsParser = require( '@typescript-eslint/parser' ); +var tsPlugin = require( '@typescript-eslint/eslint-plugin' ); +var stylisticTs = require( '@stylistic/eslint-plugin-ts' ); +var pluginN = require( 'eslint-plugin-n' ); +var pluginCspell = require( '@cspell/eslint-plugin' ); +var pluginJsdoc = require( 'eslint-plugin-jsdoc' ); +var pluginImport = require( 'eslint-plugin-import' ); +var pluginExpectType = require( 'eslint-plugin-expect-type' ); +var stdlibPlugin = require( './lib/node_modules/@stdlib/_tools/eslint/rules/scripts/plugin.js' ); +var allRules = require( './etc/eslint/rules' ); +var tsRules = require( './etc/eslint/rules/typescript.js' ); +var overrides = require( './etc/eslint/overrides' ); + + +// VARIABLES // + +var restrictedSyntaxConfig = overrides[ 1 ].rules[ 'no-restricted-syntax' ]; +var nonClonableRules = {}; +var rules = {}; +var val; +var key; +var i; + + +// FUNCTIONS // + +/** +* Tests whether a value can be structured-cloned. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether the value is clonable +*/ +function isClonable( value ) { + try { + // eslint-disable-next-line n/no-unsupported-features/es-builtins + if ( typeof structuredClone === 'function' ) { + structuredClone( value ); + } + return true; + } catch ( e ) { + return false; + } +} + + +// MAIN // + +// Separate rules that contain non-clonable values (e.g., remark plugin +// instances) because ESLint flat config internally clones config objects: +for ( key in allRules ) { + val = allRules[ key ]; + if ( isClonable( val ) ) { + rules[ key ] = val; + } else { + nonClonableRules[ key ] = val; + } +} + +module.exports = [ + // Global ignores: + { + 'ignores': [ + '**/build/', + '**/reports/', + 'dist/', + '.git*' + ] + }, + + // Base JavaScript config: + { + 'files': [ '**/*.js' ], + 'languageOptions': { + 'ecmaVersion': 6, + 'sourceType': 'script', + 'globals': { + ...globals.browser, + ...globals.node, + ...globals.commonjs, + ...globals.worker + } + }, + 'plugins': { + 'n': pluginN, + 'jsdoc': pluginJsdoc, + '@cspell': pluginCspell, + 'stdlib': stdlibPlugin + }, + 'rules': rules + }, + + // REPL namespace files: + { + 'files': [ '**/lib/node_modules/@stdlib/**/lib/[a-z].js' ], + 'rules': { + 'stdlib/repl-namespace-order': 'error' + } + }, + + // Benchmarks: + { + 'files': [ '**/benchmark/**/*.js' ], + 'rules': { + 'no-new-wrappers': 'warn', + 'max-lines': [ 'warn', { + 'max': 1000, + 'skipBlankLines': true, + 'skipComments': true + }], + 'jsdoc/require-jsdoc': 'off', + 'no-restricted-syntax': restrictedSyntaxConfig + } + }, + + // Examples: + { + 'files': [ '**/examples/**/*.js' ], + 'rules': { + 'no-console': 'off', + 'vars-on-top': 'off', + 'jsdoc/require-jsdoc': 'off', + 'stdlib/jsdoc-private-annotation': 'off', + 'stdlib/require-order': 'off', + 'stdlib/require-file-extensions': 'off', + 'no-restricted-syntax': restrictedSyntaxConfig + } + }, + + // Tests: + { + 'files': [ '**/test/**/*.js' ], + 'rules': { + 'no-empty-function': 'off', + 'jsdoc/require-jsdoc': 'off', + 'no-undefined': 'off', + 'max-lines': [ 'warn', { + 'max': 1000, + 'skipBlankLines': true, + 'skipComments': true + }], + 'no-restricted-syntax': restrictedSyntaxConfig + } + }, + + // TypeScript declarations: + { + 'files': [ '**/*.d.ts' ], + 'languageOptions': { + 'parser': tsParser, + 'sourceType': 'module', + 'parserOptions': { + 'project': path.join( __dirname, 'tsconfig.json' ) + }, + 'globals': { + ...globals.browser, + ...globals.node + } + }, + 'plugins': { + '@typescript-eslint': tsPlugin, + '@stylistic/ts': stylisticTs, + 'jsdoc': pluginJsdoc, + 'import': pluginImport, + 'expect-type': pluginExpectType, + 'stdlib': stdlibPlugin + }, + 'rules': tsRules + }, + + // TypeScript test files: + { + 'files': [ '**/test/**/*.ts' ], + 'rules': { + 'jsdoc/require-jsdoc': 'off' + } + } +]; diff --git a/package.json b/package.json index 35dfeafe6c9d..a97d6981985a 100644 --- a/package.json +++ b/package.json @@ -145,6 +145,7 @@ "eslint-plugin-expect-type": "^0.2.3", "eslint-plugin-import": "^2.29.0", "eslint-plugin-jsdoc": "^46.8.2", + "globals": "^16.1.0", "exorcist": "^2.0.0", "factor-bundle": "^2.5.0", "gh-pages": "git+https://github.com/Planeshifter/gh-pages.git#main",