| 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 | const path = require('path'); |
| 9 | |
| 10 | module.exports = { |
| 11 | meta: { |
| 12 | type: 'problem', |
| 13 | docs: { |
| 14 | description: 'disallow imports from facebook paths in non-facebook files', |
| 15 | }, |
| 16 | fixable: null, // Not automatically fixable |
| 17 | messages: { |
| 18 | noFacebookImports: |
| 19 | 'Imports from facebook paths are only allowed in files inside facebook folders or files named "InternalImports".', |
| 20 | }, |
| 21 | schema: [], // no options |
| 22 | }, |
| 23 | create(context) { |
| 24 | return { |
| 25 | ImportDeclaration(node) { |
| 26 | const importPath = node.source.value; |
| 27 | |
| 28 | // Check if the import path matches .*/facebook/.* |
| 29 | if (!/.*\/facebook\/.*/.test(importPath)) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | // Get the current file path |
| 34 | const filename = context.getFilename(); |
| 35 | const relativePath = path.relative(process.cwd(), filename); |
| 36 | |
| 37 | // Extract the file name without extension |
| 38 | const baseName = path.basename(filename, path.extname(filename)); |
| 39 | |
| 40 | // Check if the file is named "InternalImports" |
| 41 | if ( |
| 42 | baseName === 'InternalImports' || |
| 43 | baseName === 'Internal' || |
| 44 | baseName === 'InternalTypes' |
| 45 | ) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Check if the file is inside a facebook folder |
| 50 | const pathParts = relativePath.split(path.sep); |
| 51 | if (pathParts.includes('facebook')) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Report the violation |
| 56 | context.report({ |
| 57 | node, |
| 58 | messageId: 'noFacebookImports', |
| 59 | }); |
| 60 | }, |
| 61 | }; |
| 62 | }, |
| 63 | }; |
| 64 | |