From 7e4cbecdc2870752976bccaa3e5fc4c85d24d683 Mon Sep 17 00:00:00 2001 From: Thijs Louisse Date: Wed, 21 Sep 2022 22:28:26 +0200 Subject: [PATCH] chore(providence-analytics): getSourceCodeFragmentOfDeclaration edge cases --- ...get-source-code-fragment-of-declaration.js | 8 ++- ...getSourceCodeFragmentOfDeclaration.test.js | 72 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/packages-node/providence-analytics/src/program/utils/get-source-code-fragment-of-declaration.js b/packages-node/providence-analytics/src/program/utils/get-source-code-fragment-of-declaration.js index ab8ae07f1..8d28f1323 100644 --- a/packages-node/providence-analytics/src/program/utils/get-source-code-fragment-of-declaration.js +++ b/packages-node/providence-analytics/src/program/utils/get-source-code-fragment-of-declaration.js @@ -76,10 +76,14 @@ async function getSourceCodeFragmentOfDeclaration({ filePath, exportedIdentifier if (!isReferenced) { // it must be an exported declaration - finalNodePath = variableDeclaratorPath.get('init'); + finalNodePath = variableDeclaratorPath.node.init + ? variableDeclaratorPath.get('init') + : variableDeclaratorPath; } else { finalNodePath = getReferencedDeclaration({ - referencedIdentifierName: variableDeclaratorPath.node.init.name, + referencedIdentifierName: variableDeclaratorPath.node.init + ? variableDeclaratorPath.node.init.name + : variableDeclaratorPath.node.id.name, globalScopeBindings, }); } diff --git a/packages-node/providence-analytics/test-node/program/utils/getSourceCodeFragmentOfDeclaration.test.js b/packages-node/providence-analytics/test-node/program/utils/getSourceCodeFragmentOfDeclaration.test.js index faa361ff7..c13989017 100644 --- a/packages-node/providence-analytics/test-node/program/utils/getSourceCodeFragmentOfDeclaration.test.js +++ b/packages-node/providence-analytics/test-node/program/utils/getSourceCodeFragmentOfDeclaration.test.js @@ -52,6 +52,42 @@ describe('getSourceCodeFragmentOfDeclaration', () => { expect(sourceFragment).to.equal('88'); }); + + describe('Different types of declarations', () => { + it('handles class declarations', async () => { + const fakeFs = { + '/my/proj/exports/ajax.js': ` + import { AjaxClass as LionAjaxClass } from '../_legacy/ajax/index.js'; + + export class AjaxClass extends LionAjaxClass {} + `, + }; + mock(fakeFs); + + const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({ + filePath: '/my/proj/exports/ajax.js', + exportedIdentifier: 'AjaxClass', + }); + + expect(sourceFragment).to.equal('class AjaxClass extends LionAjaxClass {}'); + }); + + it('handles function declarations', async () => { + const fakeFs = { + '/my/proj/exports/myFn.js': ` + export function myFn() {} + `, + }; + mock(fakeFs); + + const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({ + filePath: '/my/proj/exports/myFn.js', + exportedIdentifier: 'myFn', + }); + + expect(sourceFragment).to.equal('function myFn() {}'); + }); + }); }); describe('[default] specifiers', () => { @@ -103,5 +139,41 @@ describe('getSourceCodeFragmentOfDeclaration', () => { expect(sourceFragment).to.equal('88'); }); + + describe('Different types of declarations', () => { + it('handles class declarations', async () => { + const fakeFs = { + '/my/proj/exports/ajax.js': ` + import { AjaxClass as LionAjaxClass } from '../_legacy/ajax/index.js'; + + export default class AjaxClass extends LionAjaxClass {} + `, + }; + mock(fakeFs); + + const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({ + filePath: '/my/proj/exports/ajax.js', + exportedIdentifier: '[default]', + }); + + expect(sourceFragment).to.equal('class AjaxClass extends LionAjaxClass {}'); + }); + + it('handles function declarations', async () => { + const fakeFs = { + '/my/proj/exports/myFn.js': ` + export default function myFn() {} + `, + }; + mock(fakeFs); + + const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({ + filePath: '/my/proj/exports/myFn.js', + exportedIdentifier: '[default]', + }); + + expect(sourceFragment).to.equal('function myFn() {}'); + }); + }); }); });