|
| 1 | +/* eslint-disable unicorn/filename-case */ |
| 2 | +const path = require('path') |
| 3 | + |
| 4 | +module.exports = { |
| 5 | + 'disallow-potential-side-effects': { |
| 6 | + meta: { |
| 7 | + docs: { |
| 8 | + description: 'Disallow potential side effects in types, constants and internal files', |
| 9 | + recommended: false, |
| 10 | + }, |
| 11 | + schema: [], |
| 12 | + }, |
| 13 | + create(context) { |
| 14 | + const filename = path.basename(context.getFilename()) |
| 15 | + const dotIndex = filename.lastIndexOf('.') |
| 16 | + const filenameWithoutExtension = dotIndex < 0 ? filename : filename.slice(0, dotIndex) |
| 17 | + if (!isRestrictedFile(filenameWithoutExtension)) { |
| 18 | + return {} |
| 19 | + } |
| 20 | + return { |
| 21 | + Program(node) { |
| 22 | + node.body.forEach((child) => { |
| 23 | + reportRestrictedDeclarations(context, child) |
| 24 | + }) |
| 25 | + }, |
| 26 | + } |
| 27 | + }, |
| 28 | + }, |
| 29 | +} |
| 30 | + |
| 31 | +function isRestrictedFile(filenameWithoutExtension) { |
| 32 | + return ( |
| 33 | + filenameWithoutExtension === 'types' || |
| 34 | + filenameWithoutExtension === 'internal' || |
| 35 | + filenameWithoutExtension === 'constants' || |
| 36 | + filenameWithoutExtension.endsWith('.constants') || |
| 37 | + filenameWithoutExtension.endsWith('.types') |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +function reportRestrictedDeclarations(context, node) { |
| 42 | + switch (node.type) { |
| 43 | + case 'TemplateLiteral': |
| 44 | + node.expressions.forEach((child) => reportRestrictedDeclarations(context, child)) |
| 45 | + break |
| 46 | + case 'ExportNamedDeclaration': |
| 47 | + case 'ExportAllDeclaration': |
| 48 | + case 'ImportDeclaration': |
| 49 | + if (node.declaration) { |
| 50 | + reportRestrictedDeclarations(context, node.declaration) |
| 51 | + } else if (node.source && !isRestrictedFile(path.basename(node.source.value))) { |
| 52 | + context.report({ |
| 53 | + node: node.source, |
| 54 | + message: `This file can only import types, constants and internal files`, |
| 55 | + }) |
| 56 | + } |
| 57 | + break |
| 58 | + case 'VariableDeclaration': |
| 59 | + node.declarations.forEach((child) => reportRestrictedDeclarations(context, child)) |
| 60 | + break |
| 61 | + case 'VariableDeclarator': |
| 62 | + if (node.init) { |
| 63 | + reportRestrictedDeclarations(context, node.init) |
| 64 | + } |
| 65 | + break |
| 66 | + case 'ArrayExpression': |
| 67 | + node.elements.forEach((child) => reportRestrictedDeclarations(context, child)) |
| 68 | + break |
| 69 | + case 'UnaryExpression': |
| 70 | + reportRestrictedDeclarations(context, node.argument) |
| 71 | + break |
| 72 | + case 'ObjectExpression': |
| 73 | + node.properties.forEach((child) => reportRestrictedDeclarations(context, child)) |
| 74 | + break |
| 75 | + case 'SpreadElement': |
| 76 | + reportRestrictedDeclarations(context, node.argument) |
| 77 | + break |
| 78 | + case 'Property': |
| 79 | + reportRestrictedDeclarations(context, node.key) |
| 80 | + reportRestrictedDeclarations(context, node.value) |
| 81 | + break |
| 82 | + case 'AssignmentExpression': |
| 83 | + case 'BinaryExpression': |
| 84 | + reportRestrictedDeclarations(context, node.left) |
| 85 | + reportRestrictedDeclarations(context, node.right) |
| 86 | + break |
| 87 | + case 'TSAsExpression': |
| 88 | + case 'ExpressionStatement': |
| 89 | + reportRestrictedDeclarations(context, node.expression) |
| 90 | + break |
| 91 | + case 'MemberExpression': |
| 92 | + reportRestrictedDeclarations(context, node.object) |
| 93 | + reportRestrictedDeclarations(context, node.property) |
| 94 | + break |
| 95 | + case 'FunctionExpression': |
| 96 | + case 'ArrowFunctionExpression': |
| 97 | + case 'FunctionDeclaration': |
| 98 | + case 'ClassDeclaration': |
| 99 | + case 'TSEnumDeclaration': |
| 100 | + case 'TSInterfaceDeclaration': |
| 101 | + case 'TSTypeAliasDeclaration': |
| 102 | + case 'TSDeclareFunction': |
| 103 | + case 'Literal': |
| 104 | + case 'Identifier': |
| 105 | + break |
| 106 | + default: |
| 107 | + context.report({ node, message: `${node.type} not allowed in types, constants and internal files` }) |
| 108 | + break |
| 109 | + } |
| 110 | +} |
0 commit comments