|
| 1 | +import * as path from 'path'; |
| 2 | +import * as ts from 'typescript'; |
| 3 | + |
| 4 | +import { Collector } from './Collector'; |
| 5 | +import { AstSymbol } from '../analyzer/AstSymbol'; |
| 6 | +import { AstDeclaration } from '../analyzer/AstDeclaration'; |
| 7 | +// import { SymbolMetadata } from './SymbolMetadata'; |
| 8 | +import { CollectorEntity } from './CollectorEntity'; |
| 9 | +import { ExtractorMessageId } from '../api/ExtractorMessageId'; |
| 10 | + |
| 11 | +export class VisibilityChecker { |
| 12 | + |
| 13 | + public static check(collector: Collector): void { |
| 14 | + const alreadyWarnedSymbols: Set<AstSymbol> = new Set<AstSymbol>(); |
| 15 | + |
| 16 | + for (const entity of collector.entities) { |
| 17 | + if (entity.astEntity instanceof AstSymbol) { |
| 18 | + if (entity.exported) { |
| 19 | + entity.astEntity.forEachDeclarationRecursive((astDeclaration: AstDeclaration) => { |
| 20 | + VisibilityChecker._checkReferences(collector, astDeclaration, alreadyWarnedSymbols); |
| 21 | + }); |
| 22 | + |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private static _checkReferences(collector: Collector, astDeclaration: AstDeclaration, |
| 29 | + alreadyWarnedSymbols: Set<AstSymbol>): void { |
| 30 | + |
| 31 | + for (const referencedEntity of astDeclaration.referencedAstEntities) { |
| 32 | + |
| 33 | + if (referencedEntity instanceof AstSymbol) { |
| 34 | + if (!referencedEntity.isExternal) { |
| 35 | + const collectorEntity: CollectorEntity | undefined = collector.tryGetCollectorEntity(referencedEntity); |
| 36 | + |
| 37 | + if (collectorEntity && collectorEntity.exported) { |
| 38 | + // const metadata: SymbolMetadata = collector.fetchMetadata(referencedEntity); |
| 39 | + return; |
| 40 | + } else { |
| 41 | + const entryPointFilename: string = path.basename(collector.workingPackage.entryPointSourceFile.fileName); |
| 42 | + |
| 43 | + if (!alreadyWarnedSymbols.has(referencedEntity)) { |
| 44 | + alreadyWarnedSymbols.add(referencedEntity); |
| 45 | + |
| 46 | + // The main usage scenario for ECMAScript symbols is to attach private data to a JavaScript object, |
| 47 | + // so as a special case, we do NOT report them as forgotten exports. |
| 48 | + if (!VisibilityChecker._isEcmaScriptSymbol(referencedEntity)) { |
| 49 | + |
| 50 | + collector.messageRouter.addAnalyzerIssue(ExtractorMessageId.ForgottenExport, |
| 51 | + `The symbol ${referencedEntity.localName} needs to be exported` |
| 52 | + + ` from the entry point ${entryPointFilename}`, |
| 53 | + astDeclaration); |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Detect an AstSymbol that refers to an ECMAScript symbol declaration such as: |
| 65 | + // |
| 66 | + // const mySymbol: unique symbol = Symbol('mySymbol'); |
| 67 | + private static _isEcmaScriptSymbol(astSymbol: AstSymbol): boolean { |
| 68 | + if (astSymbol.astDeclarations.length !== 1) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + // We are matching a form like this: |
| 73 | + // |
| 74 | + // - VariableDeclaration: |
| 75 | + // - Identifier: pre=[mySymbol] |
| 76 | + // - ColonToken: pre=[:] sep=[ ] |
| 77 | + // - TypeOperator: |
| 78 | + // - UniqueKeyword: pre=[unique] sep=[ ] |
| 79 | + // - SymbolKeyword: pre=[symbol] |
| 80 | + const astDeclaration: AstDeclaration = astSymbol.astDeclarations[0]; |
| 81 | + if (ts.isVariableDeclaration(astDeclaration.declaration)) { |
| 82 | + const variableTypeNode: ts.TypeNode | undefined = astDeclaration.declaration.type; |
| 83 | + if (variableTypeNode) { |
| 84 | + for (const token of variableTypeNode.getChildren()) { |
| 85 | + if (token.kind === ts.SyntaxKind.SymbolKeyword) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments