Skip to content

tools: lint typescript files #58256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ format-md: tools/lint-md/node_modules/remark-parse/package.json ## Format the ma



LINT_JS_TARGETS = eslint.config.mjs benchmark doc lib test tools
LINT_JS_TARGETS = eslint.config.mjs benchmark doc lib test tools typings

run-lint-js = tools/eslint/node_modules/eslint/bin/eslint.js --cache \
--max-warnings=0 --report-unused-disable-directives $(LINT_JS_TARGETS)
Expand Down
2 changes: 1 addition & 1 deletion benchmark/eslint.config_partial.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { globals } from '../tools/eslint/eslint.config_utils.mjs';

export default [
{
files: ['benchmark/**/*.{js,mjs,cjs}'],
files: ['benchmark/**/*.{js,mjs,cjs,ts,mts,cts}'],
languageOptions: {
globals: {
...globals.node,
Expand Down
2 changes: 1 addition & 1 deletion doc/api/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ This will result in [`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`][] error:
```ts
// This namespace is exporting a value
namespace A {
export let x = 1
export const x = 1;
}
```

Expand Down
9 changes: 8 additions & 1 deletion doc/eslint.config_partial.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const builtin = builtinModules.filter((name) => !name.startsWith('node:'));

export default [
{
files: ['doc/**/*.md/*.{js,mjs,cjs}'],
files: ['doc/**/*.md/*.{js,mjs,cjs,ts,cts,mts}'],
rules: {
// Ease some restrictions in doc examples.
'no-restricted-properties': 'off',
Expand All @@ -22,6 +22,13 @@ export default [
message: 'Use `node:` prefix.',
},
],

// Typescript rules
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/no-unused-vars': 'off',
// Removed since it is used on how not to use examples.
'@typescript-eslint/no-namespace': 'off',

'no-undef': 'off',
'no-unused-expressions': 'off',
'no-unused-vars': 'off',
Expand Down
96 changes: 63 additions & 33 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const babelPluginSyntaxImportSource = resolveEslintTool('@babel/plugin-syntax-im
const { default: jsdoc } = await importEslintTool('eslint-plugin-jsdoc');
const { default: markdown } = await importEslintTool('eslint-plugin-markdown');
const { default: stylisticJs } = await importEslintTool('@stylistic/eslint-plugin-js');
const { default: tseslint } = await importEslintTool('typescript-eslint');

nodeCore.RULES_DIR = fileURLToPath(new URL('./tools/eslint-rules', import.meta.url));

Expand Down Expand Up @@ -86,7 +87,7 @@ export default [
js.configs.recommended,
jsdoc.configs['flat/recommended'],
{
files: ['**/*.{js,cjs}'],
files: ['**/*.{js,cjs,ts,cts}'],
languageOptions: {
// The default is `commonjs` but it's not supported by the Babel parser.
sourceType: 'script',
Expand Down Expand Up @@ -352,7 +353,7 @@ export default [
processor: 'markdown/markdown',
},
{
files: ['**/*.md/*.{js,cjs}'],
files: ['**/*.md/*.{js,cjs,ts,cts}'],
languageOptions: {
parserOptions: {
ecmaFeatures: { impliedStrict: true },
Expand All @@ -369,38 +370,67 @@ export default [
languageOptions: {
sourceType: 'module',
},
rules: { 'no-restricted-globals': [
'error',
{
name: '__filename',
message: 'Use import.meta.url instead.',
},
{
name: '__dirname',
message: 'Not available in ESM.',
},
{
name: 'exports',
message: 'Not available in ESM.',
},
{
name: 'module',
message: 'Not available in ESM.',
},
{
name: 'require',
message: 'Use import instead.',
},
{
name: 'Buffer',
message: "Import 'Buffer' instead of using the global.",
},
{
name: 'process',
message: "Import 'process' instead of using the global.",
},
] },
rules: {
'no-restricted-globals': [
'error',
{
name: '__filename',
message: 'Use import.meta.url instead.',
},
{
name: '__dirname',
message: 'Not available in ESM.',
},
{
name: 'exports',
message: 'Not available in ESM.',
},
{
name: 'module',
message: 'Not available in ESM.',
},
{
name: 'require',
message: 'Use import instead.',
},
{
name: 'Buffer',
message: "Import 'Buffer' instead of using the global.",
},
{
name: 'process',
message: "Import 'process' instead of using the global.",
},
],
},
},
// #region Typescript rules
{

files: ['**/*.{ts,mts,cts}'],
...tseslint.configs.base,
rules: {
...tseslint.configs.eslintRecommended.rules,
// Index 0 contains the base configuration,
// index 1 provides eslint-specific overrides,
// and index 2 includes the recommended rules.
...tseslint.configs.recommended[2].rules,
// This is handled by 'no-restricted-globals'
'@typescript-eslint/no-require-imports': 'off',
// We use ignore comments throughout the codebase
'@typescript-eslint/ban-ts-comment': 'off',
},
},
{
files: ['typings/**'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-empty-object-type': 'off',

'no-use-before-define': 'off',
},
},
// #endregion
// #endregion
// #region partials
...benchmarkConfig,
Expand Down
20 changes: 16 additions & 4 deletions test/eslint.config_partial.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {

export default [
{
files: ['test/**/*.{js,mjs,cjs}'],
ignores: ['test/fixtures/**/*.{ts,mts,cts}'],
},
{
files: ['test/**/*.{js,mjs,cjs,ts,mts,cts}'],
languageOptions: {
globals: {
...globals.node,
Expand Down Expand Up @@ -120,9 +123,9 @@ export default [
},
{
files: [
'test/es-module/**/*.{js,mjs}',
'test/parallel/**/*.{js,mjs}',
'test/sequential/**/*.{js,mjs}',
'test/es-module/**/*.{js,mjs,ts,mts,cts}',
'test/parallel/**/*.{js,mjs,ts,mts,cts}',
'test/sequential/**/*.{js,mjs,ts,mts,cts}',
],
rules: {
'@stylistic/js/comma-dangle': [
Expand All @@ -147,6 +150,15 @@ export default [
'node-core/require-common-first': 'off',
},
},
{
files: [
'test/{common,wpt}/**/*.{ts,mts,cts}',
],
rules: {
'node-core/required-modules': 'off',
'node-core/require-common-first': 'off',
},
},
{
files: [
'test/es-module/test-esm-example-loader.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import '../../../common/index.mjs';

interface Foo {
/** line
*
* blocks */
*
* blocks */
}

async function Throw() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import '../../../common/index.mjs';

interface Block {
/** line
*
* blocks */
/* line
*
* blocks */
}

class Foo {
Expand Down
2 changes: 1 addition & 1 deletion tools/eslint/eslint.config_partial.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { globals } from './eslint.config_utils.mjs';

export default [
{
files: ['tools/**/*.{js,mjs,cjs}'],
files: ['tools/**/*.{js,mjs,cjs,ts,mts,cts}'],
languageOptions: {
globals: {
...globals.node,
Expand Down
Loading
Loading