addons/eslint-rules/no-facebook-imports.jsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318const path = require('path');
b69ab319
b69ab3110module.exports = {
b69ab3111 meta: {
b69ab3112 type: 'problem',
b69ab3113 docs: {
b69ab3114 description: 'disallow imports from facebook paths in non-facebook files',
b69ab3115 },
b69ab3116 fixable: null, // Not automatically fixable
b69ab3117 messages: {
b69ab3118 noFacebookImports:
b69ab3119 'Imports from facebook paths are only allowed in files inside facebook folders or files named "InternalImports".',
b69ab3120 },
b69ab3121 schema: [], // no options
b69ab3122 },
b69ab3123 create(context) {
b69ab3124 return {
b69ab3125 ImportDeclaration(node) {
b69ab3126 const importPath = node.source.value;
b69ab3127
b69ab3128 // Check if the import path matches .*/facebook/.*
b69ab3129 if (!/.*\/facebook\/.*/.test(importPath)) {
b69ab3130 return;
b69ab3131 }
b69ab3132
b69ab3133 // Get the current file path
b69ab3134 const filename = context.getFilename();
b69ab3135 const relativePath = path.relative(process.cwd(), filename);
b69ab3136
b69ab3137 // Extract the file name without extension
b69ab3138 const baseName = path.basename(filename, path.extname(filename));
b69ab3139
b69ab3140 // Check if the file is named "InternalImports"
b69ab3141 if (
b69ab3142 baseName === 'InternalImports' ||
b69ab3143 baseName === 'Internal' ||
b69ab3144 baseName === 'InternalTypes'
b69ab3145 ) {
b69ab3146 return;
b69ab3147 }
b69ab3148
b69ab3149 // Check if the file is inside a facebook folder
b69ab3150 const pathParts = relativePath.split(path.sep);
b69ab3151 if (pathParts.includes('facebook')) {
b69ab3152 return;
b69ab3153 }
b69ab3154
b69ab3155 // Report the violation
b69ab3156 context.report({
b69ab3157 node,
b69ab3158 messageId: 'noFacebookImports',
b69ab3159 });
b69ab3160 },
b69ab3161 };
b69ab3162 },
b69ab3163};