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