1// Copyright (C) 2024 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15const {FlatCompat} = require('@eslint/eslintrc'); 16const fs = require('fs'); 17const globals = require('globals'); 18const js = require('@eslint/js'); 19const jsdoc = require('eslint-plugin-jsdoc'); 20const path = require('node:path'); 21const tsParser = require('@typescript-eslint/parser'); 22const typescriptEslint = require('@typescript-eslint/eslint-plugin'); 23 24const compat = new FlatCompat({ 25 baseDirectory: __dirname, 26 recommendedConfig: js.configs.recommended, 27 allConfig: js.configs.all, 28}); 29 30// The eslint-config-google uses deprecated jsdoc options that break with the 31// latest version of eslint. This has been fixed upstram [1] but no npm package 32// has been released since then. Hence patching the config manually. 33// [1] https://github.com/google/eslint-config-google/pull/72. 34const googleCfg = compat.extends('google'); 35delete googleCfg[0].rules['valid-jsdoc']; 36delete googleCfg[0].rules['require-jsdoc']; 37 38const ignorePath = path.resolve(__dirname, '.prettierignore'); 39const ignores = fs 40 .readFileSync(ignorePath, {encoding: 'utf8'}) 41 .split('\n') 42 .filter((l) => l !== '' && !l.startsWith('#')); 43 44module.exports = [ 45 // `ignores` has to go on a standalone block at the start otherwise gets 46 // overridden by the googleCfg and jsdoc.configs, because the new eslint 47 // flat config is so clever. 48 {ignores: ignores}, 49 50 ...googleCfg, 51 52 jsdoc.configs['flat/recommended'], 53 54 { 55 files: ['src/**/*.ts'], 56 plugins: { 57 '@typescript-eslint': typescriptEslint, 58 jsdoc, 59 }, 60 61 languageOptions: { 62 globals: { 63 ...globals.browser, 64 ...globals.node, 65 }, 66 ecmaVersion: 'latest', 67 sourceType: 'module', 68 parser: tsParser, 69 parserOptions: { 70 project: './tsconfig.json', 71 }, 72 }, 73 74 rules: { 75 'indent': 'off', 76 'max-len': 'off', 77 'operator-linebreak': 'off', 78 'quotes': 'off', 79 'brace-style': 'off', 80 'space-before-function-paren': 'off', 81 'generator-star-spacing': 'off', 82 'semi-spacing': 'off', 83 84 'no-multi-spaces': [ 85 'error', 86 { 87 ignoreEOLComments: true, 88 }, 89 ], 90 91 'no-unused-vars': 'off', 92 93 '@typescript-eslint/no-unused-vars': [ 94 'error', 95 { 96 argsIgnorePattern: '^_.*', 97 varsIgnorePattern: '^_.*', 98 }, 99 ], 100 101 'no-array-constructor': 'off', 102 '@typescript-eslint/no-array-constructor': ['error'], 103 'prefer-rest-params': 'off', 104 105 'new-cap': [ 106 'error', 107 { 108 capIsNew: false, 109 properties: false, 110 }, 111 ], 112 113 'jsdoc/require-jsdoc': 'off', 114 'jsdoc/require-param': 'off', 115 'jsdoc/require-param-type': 'off', 116 'jsdoc/require-returns': 'off', 117 'jsdoc/require-returns-type': 'off', 118 'jsdoc/tag-lines': 'off', 119 120 '@typescript-eslint/no-explicit-any': 'error', 121 122 '@typescript-eslint/strict-boolean-expressions': [ 123 'error', 124 { 125 allowNullableBoolean: true, 126 allowNullableObject: true, 127 allowNullableString: true, 128 }, 129 ], 130 }, 131 }, 132]; 133