feat(providence-analytics): enhance getSourceCodeFragmentOfDeclaration api
This commit is contained in:
parent
bfc0ad112f
commit
f7e73f8f23
3 changed files with 25 additions and 7 deletions
5
.changeset/lovely-pigs-wash.md
Normal file
5
.changeset/lovely-pigs-wash.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'providence-analytics': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
enhance getSourceCodeFragmentOfDeclaration api
|
||||||
|
|
@ -98,7 +98,7 @@ export function getReferencedDeclaration({ referencedIdentifierName, globalScope
|
||||||
* await getSourceCodeFragmentOfDeclaration(code) // finds "88"
|
* await getSourceCodeFragmentOfDeclaration(code) // finds "88"
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @param {{ filePath: PathFromSystemRoot; exportedIdentifier: string; projectRootPath: PathFromSystemRoot; parser: AnalyzerAst }} opts
|
* @param {{ code?: string; ast?: object; filePath: PathFromSystemRoot; exportedIdentifier: string; projectRootPath: PathFromSystemRoot; parser?: AnalyzerAst }} opts
|
||||||
* @returns {Promise<{ sourceNodePath: SwcPath; sourceFragment: string|null; externalImportSource: string|null; }>}
|
* @returns {Promise<{ sourceNodePath: SwcPath; sourceFragment: string|null; externalImportSource: string|null; }>}
|
||||||
*/
|
*/
|
||||||
export async function getSourceCodeFragmentOfDeclaration({
|
export async function getSourceCodeFragmentOfDeclaration({
|
||||||
|
|
@ -106,17 +106,28 @@ export async function getSourceCodeFragmentOfDeclaration({
|
||||||
projectRootPath,
|
projectRootPath,
|
||||||
parser = 'oxc',
|
parser = 'oxc',
|
||||||
filePath,
|
filePath,
|
||||||
|
code,
|
||||||
|
ast,
|
||||||
}) {
|
}) {
|
||||||
const code = await fsAdapter.fs.promises.readFile(filePath, 'utf8');
|
if (!code) {
|
||||||
|
// eslint-disable-next-line no-param-reassign
|
||||||
|
code = await fsAdapter.fs.promises.readFile(filePath, 'utf8');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ast) {
|
||||||
|
// eslint-disable-next-line no-param-reassign
|
||||||
|
ast = await AstService.getAst(code, parser);
|
||||||
|
}
|
||||||
|
|
||||||
// compensate for swc span bug: https://github.com/swc-project/swc/issues/1366#issuecomment-1516539812
|
// compensate for swc span bug: https://github.com/swc-project/swc/issues/1366#issuecomment-1516539812
|
||||||
const offset = parser === 'swc' ? await AstService._getSwcOffset() : -1;
|
const offset = parser === 'swc' ? await AstService._getSwcOffset() : -1;
|
||||||
const ast = await AstService.getAst(code, parser);
|
|
||||||
|
|
||||||
/** @type {SwcPath} */
|
/** @type {SwcPath} */
|
||||||
let finalNodePath;
|
let finalNodePath;
|
||||||
|
|
||||||
const moduleOrProgramHandler = astPath => {
|
const moduleOrProgramHandler = (
|
||||||
|
/** @type {{ stop: () => void; node: { body: any[]; }; scope: { bindings: { [x: string]: { path: any; }; }; }; }} */ astPath,
|
||||||
|
) => {
|
||||||
astPath.stop();
|
astPath.stop();
|
||||||
|
|
||||||
// Situations
|
// Situations
|
||||||
|
|
@ -209,6 +220,7 @@ export async function getSourceCodeFragmentOfDeclaration({
|
||||||
currentFilePath,
|
currentFilePath,
|
||||||
projectRootPath,
|
projectRootPath,
|
||||||
);
|
);
|
||||||
|
|
||||||
const filePathOrSrc = getFilePathOrExternalSource({
|
const filePathOrSrc = getFilePathOrExternalSource({
|
||||||
rootPath: projectRootPath,
|
rootPath: projectRootPath,
|
||||||
localPath: /** @type {PathRelativeFromProjectRoot} */ (rootFile.file),
|
localPath: /** @type {PathRelativeFromProjectRoot} */ (rootFile.file),
|
||||||
|
|
@ -233,8 +245,10 @@ export async function getSourceCodeFragmentOfDeclaration({
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const startOf = node => node.start || node.span.start;
|
const startOf = (/** @type {{ start: number; span: { start: number; }; }} */ node) =>
|
||||||
const endOf = node => node.end || node.span.end;
|
node.start || node.span.start;
|
||||||
|
const endOf = (/** @type {{ end: number; span: { end: number; }; }} */ node) =>
|
||||||
|
node.end || node.span.end;
|
||||||
return {
|
return {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
sourceNodePath: finalNodePath,
|
sourceNodePath: finalNodePath,
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,6 @@ async function resolveImportPathFn(importee, importer, opts = {}) {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
rollupResolve.buildStart.call(fakePluginContext, { preserveSymlinks });
|
rollupResolve.buildStart.call(fakePluginContext, { preserveSymlinks });
|
||||||
|
|
||||||
// @ts-expect-error
|
|
||||||
const result = await rollupResolve.resolveId.handler.call(
|
const result = await rollupResolve.resolveId.handler.call(
|
||||||
fakePluginContext,
|
fakePluginContext,
|
||||||
importee,
|
importee,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue