|
| 1 | +/** |
| 2 | + * ESLint Rule: require-platform-declaration |
| 3 | + * |
| 4 | + * Ensures that all non-test source files export __supportedPlatforms |
| 5 | + * |
| 6 | + * Valid: |
| 7 | + * export const __supportedPlatforms = ['browser'] as const; |
| 8 | + * export const __supportedPlatforms = ['__universal__'] as const; |
| 9 | + * export const __supportedPlatforms: Platform[] = ['browser', 'node']; |
| 10 | + * |
| 11 | + * Invalid: |
| 12 | + * // Missing __supportedPlatforms export |
| 13 | + */ |
| 14 | + |
| 15 | +module.exports = { |
| 16 | + meta: { |
| 17 | + type: 'problem', |
| 18 | + docs: { |
| 19 | + description: 'Require __supportedPlatforms export in all source files', |
| 20 | + category: 'Best Practices', |
| 21 | + recommended: true, |
| 22 | + }, |
| 23 | + messages: { |
| 24 | + missingPlatformDeclaration: 'File must export __supportedPlatforms to declare which platforms it supports. Example: export const __supportedPlatforms = [\'__universal__\'] as const;', |
| 25 | + invalidPlatformDeclaration: '__supportedPlatforms must be exported as a const array. Example: export const __supportedPlatforms = [\'browser\', \'node\'] as const;', |
| 26 | + }, |
| 27 | + schema: [], |
| 28 | + }, |
| 29 | + |
| 30 | + create(context) { |
| 31 | + const filename = context.getFilename(); |
| 32 | + |
| 33 | + // Skip test files |
| 34 | + if (filename.endsWith('.spec.ts') || |
| 35 | + filename.endsWith('.test.ts') || |
| 36 | + filename.endsWith('.tests.ts') || |
| 37 | + filename.endsWith('.test.js') || |
| 38 | + filename.endsWith('.spec.js') || |
| 39 | + filename.endsWith('.tests.js') || |
| 40 | + filename.endsWith('.test-d.ts') || |
| 41 | + filename.endsWith('.d.ts') || |
| 42 | + filename.includes('/__mocks__/') || |
| 43 | + filename.includes('/tests/')) { |
| 44 | + return {}; |
| 45 | + } |
| 46 | + |
| 47 | + // Skip non-source files |
| 48 | + if (!filename.includes('/lib/') && !filename.includes('/src/')) { |
| 49 | + return {}; |
| 50 | + } |
| 51 | + |
| 52 | + let hasPlatformExport = false; |
| 53 | + let isValidExport = false; |
| 54 | + |
| 55 | + return { |
| 56 | + ExportNamedDeclaration(node) { |
| 57 | + // Check for: export const __supportedPlatforms = [...] |
| 58 | + if (node.declaration && |
| 59 | + node.declaration.type === 'VariableDeclaration') { |
| 60 | + |
| 61 | + for (const declarator of node.declaration.declarations) { |
| 62 | + if (declarator.id.type === 'Identifier' && |
| 63 | + declarator.id.name === '__supportedPlatforms') { |
| 64 | + |
| 65 | + hasPlatformExport = true; |
| 66 | + |
| 67 | + // Validate it's a const |
| 68 | + if (node.declaration.kind !== 'const') { |
| 69 | + context.report({ |
| 70 | + node: declarator, |
| 71 | + messageId: 'invalidPlatformDeclaration', |
| 72 | + }); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // Validate it's an array |
| 77 | + let init = declarator.init; |
| 78 | + |
| 79 | + // Handle TSAsExpression: [...] as const |
| 80 | + if (init && init.type === 'TSAsExpression') { |
| 81 | + init = init.expression; |
| 82 | + } |
| 83 | + |
| 84 | + // Handle TSTypeAssertion: <const>[...] |
| 85 | + if (init && init.type === 'TSTypeAssertion') { |
| 86 | + init = init.expression; |
| 87 | + } |
| 88 | + |
| 89 | + if (init && init.type === 'ArrayExpression') { |
| 90 | + isValidExport = true; |
| 91 | + } else { |
| 92 | + context.report({ |
| 93 | + node: declarator, |
| 94 | + messageId: 'invalidPlatformDeclaration', |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + |
| 102 | + 'Program:exit'(node) { |
| 103 | + // At the end of the file, check if __supportedPlatforms was exported |
| 104 | + if (!hasPlatformExport) { |
| 105 | + context.report({ |
| 106 | + node, |
| 107 | + messageId: 'missingPlatformDeclaration', |
| 108 | + }); |
| 109 | + } |
| 110 | + }, |
| 111 | + }; |
| 112 | + }, |
| 113 | +}; |
0 commit comments