import * as path from 'path' ; import Config from '../types/config/Config' ; import NormalizedPath from '../types/NormalizedPath' ; import getConfigsForFile from '../utils/getConfigsForFile' ; import fileMatchesConfigGlob from '../utils/fileMatchesConfigGlob' ; import fileHasNecessaryTag from '../utils/fileHasNecessaryTag' ; import { reportViolation } from '../core/result' ; import ImportRecord from '../core/ImportRecord' ; export default function validateExportRules ( sourceFile , importRecord ) { let configsForImport = getConfigsForFile ( importRecord . filePath ) ; configsForImport . forEach ( config => validateConfig ( config , sourceFile , importRecord ) ) ; } function validateConfig ( config , sourceFile , importRecord ) { if ( ! path . relative ( config . path , sourceFile ) . startsWith ( '..' ) ) { return ; } if ( ! config . exports ) { return ; } if ( hasMatchingExport ( config , sourceFile , importRecord . filePath ) ) { return ; } reportViolation ( 'Module is not exported' , sourceFile , importRecord , config ) ; } function hasMatchingExport ( config , sourceFile , importFile ) { let isExported = false ; for ( const exportRule of config . exports ) { if ( fileMatchesConfigGlob ( importFile , config . path , exportRule . modules ) && fileHasNecessaryTag ( sourceFile , exportRule . accessibleTo ) ) { isExported = true ; } } return isExported ; }