summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/tools/eslint/src/extensions.ts
blob: 89b92796258258671468a936a93fd2278c724664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * @license
 * Copyright 2023 Google Inc.
 * SPDX-License-Identifier: Apache-2.0
 */

import {ESLintUtils} from '@typescript-eslint/utils';

const createRule = ESLintUtils.RuleCreator(name => {
  return `https://github.com/puppeteer/puppeteer/tree/main/tools/eslint/${name}.js`;
});

const enforceExtensionRule = createRule<[], 'extensionsRule'>({
  name: 'extensions',
  meta: {
    docs: {
      description: 'Requires `.js` for imports',
      requiresTypeChecking: false,
    },
    messages: {
      extensionsRule: 'Add `.js` to import.',
    },
    schema: [],
    fixable: 'code',
    type: 'problem',
  },
  defaultOptions: [],
  create(context) {
    return {
      ImportDeclaration(node): void {
        const file = node.source.value.split('/').pop();

        if (!node.source.value.startsWith('.') || file?.includes('.')) {
          return;
        }
        context.report({
          node: node.source,
          messageId: 'extensionsRule',
          fix(fixer) {
            return fixer.replaceText(node.source, `'${node.source.value}.js'`);
          },
        });
      },
    };
  },
});

export = enforceExtensionRule;