fix(providence-analytics): from-import-to-export without fs lookup

This commit is contained in:
Thijs Louisse 2020-08-06 00:31:37 +02:00
parent d7f0807903
commit 7d1bde66c4
2 changed files with 36 additions and 16 deletions

View file

@ -49,17 +49,21 @@ function fromImportToExportPerspective({
// we still need to check if we're not dealing with a folder. // we still need to check if we're not dealing with a folder.
// - '@open-wc/x/y.js' -> '@open-wc/x/y.js' or... '@open-wc/x/y.js/index.js' ? // - '@open-wc/x/y.js' -> '@open-wc/x/y.js' or... '@open-wc/x/y.js/index.js' ?
// - or 'lion-based-ui/test' -> 'lion-based-ui/test/index.js' or 'lion-based-ui/test' ? // - or 'lion-based-ui/test' -> 'lion-based-ui/test/index.js' or 'lion-based-ui/test' ?
const pathToCheck = pathLib.resolve(externalRootPath, `./${localPath}`); if (externalRootPath) {
const pathToCheck = pathLib.resolve(externalRootPath, `./${localPath}`);
if (fs.existsSync(pathToCheck)) { if (fs.existsSync(pathToCheck)) {
const stat = fs.statSync(pathToCheck); const stat = fs.statSync(pathToCheck);
if (stat && stat.isFile()) { if (stat && stat.isFile()) {
return `./${localPath}`; // '/path/to/lion-based-ui/fol.der' is a file return `./${localPath}`; // '/path/to/lion-based-ui/fol.der' is a file
}
return `./${localPath}/index.js`; // '/path/to/lion-based-ui/fol.der' is a folder
// eslint-disable-next-line no-else-return
} else if (fs.existsSync(`${pathToCheck}.js`)) {
return `./${localPath}.js`; // '/path/to/lion-based-ui/fol.der' is file '/path/to/lion-based-ui/fol.der.js'
} }
return `./${localPath}/index.js`; // '/path/to/lion-based-ui/fol.der' is a folder } else {
// eslint-disable-next-line no-else-return return `./${localPath}`;
} else if (fs.existsSync(`${pathToCheck}.js`)) {
return `./${localPath}.js`; // '/path/to/lion-based-ui/fol.der' is file '/path/to/lion-based-ui/fol.der.js'
} }
} else { } else {
// like '@lion/core' // like '@lion/core'

View file

@ -19,6 +19,20 @@ function storeResult(resultsObj, exportId, filteredList, meta) {
resultsObj[exportId].files = [...(resultsObj[exportId].files || []), ...Array.from(filteredList)]; resultsObj[exportId].files = [...(resultsObj[exportId].files || []), ...Array.from(filteredList)];
} }
/**
* Needed in case fromImportToExportPerspective does not have a
* externalRootPath supplied.
* @param {string} exportPath exportEntry.file
* @param {string} translatedImportPath result of fromImportToExportPerspective
*/
function compareImportAndExportPaths(exportPath, translatedImportPath) {
return (
exportPath === translatedImportPath ||
exportPath === `${translatedImportPath}.js` ||
exportPath === `${translatedImportPath}/index.js`
);
}
/** /**
* @param {FindExportsAnalyzerResult} exportsAnalyzerResult * @param {FindExportsAnalyzerResult} exportsAnalyzerResult
* @param {FindImportsAnalyzerResult} importsAnalyzerResult * @param {FindImportsAnalyzerResult} importsAnalyzerResult
@ -94,13 +108,15 @@ function matchImportsPostprocess(exportsAnalyzerResult, importsAnalyzerResult, c
* importFile 'importing-target-project/file.js' * importFile 'importing-target-project/file.js'
* => import { z } from '@reference/foo.js' * => import { z } from '@reference/foo.js'
*/ */
const isFromSameSource = const fromImportToExport = fromImportToExportPerspective({
exportEntry.file === requestedExternalSource: importEntryResult.normalizedSource,
fromImportToExportPerspective({ externalProjectMeta: exportsProjectObj,
requestedExternalSource: importEntryResult.normalizedSource, externalRootPath: cfg.referenceProjectResult ? null : cfg.referenceProjectPath,
externalProjectMeta: exportsProjectObj, });
externalRootPath: cfg.referenceProjectPath, const isFromSameSource = compareImportAndExportPaths(
}); exportEntry.file,
fromImportToExport,
);
if (!isFromSameSource) { if (!isFromSameSource) {
return; return;