|
| 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 | + * @flow |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +'use strict'; |
| 13 | + |
| 14 | +const rule = require('../metro-deep-imports.js'); |
| 15 | +const ESLintTester = require('eslint').RuleTester; |
| 16 | + |
| 17 | +ESLintTester.setDefaultConfig({ |
| 18 | + parser: require.resolve('hermes-eslint'), |
| 19 | + parserOptions: { |
| 20 | + ecmaVersion: 6, |
| 21 | + sourceType: 'module', |
| 22 | + }, |
| 23 | +}); |
| 24 | + |
| 25 | +const eslintTester = new ESLintTester(); |
| 26 | + |
| 27 | +eslintTester.run('../metro-deep-imports', rule, { |
| 28 | + valid: [ |
| 29 | + 'require("metro")', |
| 30 | + 'const Foo = require("metro-subpkg")', |
| 31 | + 'require("metro/private/Bar")', |
| 32 | + 'import Baz from "metro-baz/private/Baz"', |
| 33 | + 'import NotMetro from "foo/src/bar"', |
| 34 | + |
| 35 | + // metro-runtime allows subpath imports. We can't rely on package#exports |
| 36 | + // redirections as they may be disabled under Metro, and we must be able |
| 37 | + // to import single modules as polyfills are side-effectful. |
| 38 | + 'import Polyfill from "metro-runtime/src/polyfills/foo"', |
| 39 | + 'const Polyfill = require("metro-runtime/src/polyfills/foo")', |
| 40 | + ], |
| 41 | + invalid: [ |
| 42 | + { |
| 43 | + code: 'const myLib = require("metro/src/lib")', |
| 44 | + output: "const myLib = require('metro/private/lib')", |
| 45 | + }, |
| 46 | + { |
| 47 | + code: "import MetroInternal from 'metro-pkg/src/internal'", |
| 48 | + output: "import MetroInternal from 'metro-pkg/private/internal'", |
| 49 | + }, |
| 50 | + { |
| 51 | + code: "import type {Bar} from 'metro-types/src/bar'", |
| 52 | + output: "import type {Bar} from 'metro-types/private/bar'", |
| 53 | + }, |
| 54 | + ].map(obj => ({...obj, errors: [{messageId: 'METRO_DEEP_IMPORT'}]})), |
| 55 | +}); |
0 commit comments