|
| 1 | +const SYMBOL_DISPOSE_PROPERTY_NAMES = new Set([ |
| 2 | + 'dispose', |
| 3 | + 'asyncDispose', |
| 4 | + 'disposeAsync', |
| 5 | +]) |
| 6 | + |
| 7 | +function unwrapChainExpression(node) { |
| 8 | + if (node?.type === 'ChainExpression') { |
| 9 | + return node.expression |
| 10 | + } |
| 11 | + return node |
| 12 | +} |
| 13 | + |
| 14 | +function isSymbolDisposeProperty(node) { |
| 15 | + const candidate = unwrapChainExpression(node) |
| 16 | + |
| 17 | + if (candidate?.type !== 'MemberExpression') return false |
| 18 | + if (candidate.computed || candidate.optional) return false |
| 19 | + if (candidate.object.type !== 'Identifier') return false |
| 20 | + if (candidate.object.name !== 'Symbol') return false |
| 21 | + if (candidate.property.type !== 'Identifier') return false |
| 22 | + |
| 23 | + return SYMBOL_DISPOSE_PROPERTY_NAMES.has(candidate.property.name) |
| 24 | +} |
| 25 | + |
| 26 | +function getManualDisposeCallKind(node) { |
| 27 | + const callee = unwrapChainExpression(node.callee) |
| 28 | + |
| 29 | + if (callee?.type !== 'MemberExpression') return null |
| 30 | + if (isSymbolDisposeProperty(callee.property)) return 'symbol' |
| 31 | + |
| 32 | + if ( |
| 33 | + !callee.computed && |
| 34 | + callee.property.type === 'Identifier' && |
| 35 | + callee.property.name === 'dispose' |
| 36 | + ) { |
| 37 | + return 'method' |
| 38 | + } |
| 39 | + |
| 40 | + if ( |
| 41 | + callee.computed && |
| 42 | + callee.property.type === 'Literal' && |
| 43 | + callee.property.value === 'dispose' |
| 44 | + ) { |
| 45 | + return 'method' |
| 46 | + } |
| 47 | + |
| 48 | + return null |
| 49 | +} |
| 50 | + |
| 51 | +function isFunctionBoundary(node) { |
| 52 | + return ( |
| 53 | + node?.type === 'FunctionDeclaration' || |
| 54 | + node?.type === 'FunctionExpression' || |
| 55 | + node?.type === 'ArrowFunctionExpression' |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +function isInFinallyBlock(node) { |
| 60 | + let current = node |
| 61 | + |
| 62 | + while (current?.parent) { |
| 63 | + if (isFunctionBoundary(current.parent)) { |
| 64 | + return false |
| 65 | + } |
| 66 | + |
| 67 | + if ( |
| 68 | + current.parent.type === 'TryStatement' && |
| 69 | + current.parent.finalizer === current |
| 70 | + ) { |
| 71 | + return true |
| 72 | + } |
| 73 | + |
| 74 | + current = current.parent |
| 75 | + } |
| 76 | + |
| 77 | + return false |
| 78 | +} |
| 79 | + |
| 80 | +const rule = { |
| 81 | + meta: { |
| 82 | + type: 'suggestion', |
| 83 | + docs: { |
| 84 | + description: |
| 85 | + 'Prefer `using`/`await using` over manual disposable cleanup patterns', |
| 86 | + }, |
| 87 | + schema: [], |
| 88 | + messages: { |
| 89 | + preferUsingInFinally: |
| 90 | + 'Avoid manual disposal in `finally`; prefer `using` or `await using`.', |
| 91 | + avoidManualSymbolDispose: |
| 92 | + 'Do not call `[Symbol.dispose]`/`[Symbol.asyncDispose]` directly; prefer `using` or `await using`.', |
| 93 | + }, |
| 94 | + }, |
| 95 | + createOnce(context) { |
| 96 | + return { |
| 97 | + CallExpression(node) { |
| 98 | + const callKind = getManualDisposeCallKind(node) |
| 99 | + if (callKind === null) return |
| 100 | + |
| 101 | + if (callKind === 'symbol') { |
| 102 | + context.report({ |
| 103 | + node, |
| 104 | + messageId: 'avoidManualSymbolDispose', |
| 105 | + }) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + if (isInFinallyBlock(node)) { |
| 110 | + context.report({ |
| 111 | + node, |
| 112 | + messageId: 'preferUsingInFinally', |
| 113 | + }) |
| 114 | + } |
| 115 | + }, |
| 116 | + } |
| 117 | + }, |
| 118 | +} |
| 119 | + |
| 120 | +export default rule |
0 commit comments