main
ts 41 lines 1.22 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 // @ts-ignore-line
9 import {Linter} from '../../../node_modules/eslint/lib/linter';
10 // @ts-ignore-line
11 import * as HermesESLint from 'hermes-eslint';
12 // @ts-ignore-line
13 import {NoUseBeforeDefineRule} from '../..';
14
15 const ESLINT_CONFIG: Linter.Config = {
16 parser: 'hermes-eslint',
17 parserOptions: {
18 sourceType: 'module',
19 },
20 rules: {
21 'custom-no-use-before-define': [
22 'error',
23 {variables: false, functions: false},
24 ],
25 },
26 };
27
28 /**
29 * Post-codegen pass to validate that the generated code does not introduce bugs.
30 * Note that the compiler currently incorrectly reorders code in some cases: this
31 * step detects this using ESLint's no-use-before-define rule at its strictest
32 * setting.
33 */
34 export default function validateNoUseBeforeDefine(
35 source: string,
36 ): Array<{line: number; column: number; message: string}> | null {
37 const linter = new Linter();
38 linter.defineParser('hermes-eslint', HermesESLint);
39 linter.defineRule('custom-no-use-before-define', NoUseBeforeDefineRule);
40 return linter.verify(source, ESLINT_CONFIG);
41 }