chore(providence-analytics): getSourceCodeFragmentOfDeclaration edge cases

This commit is contained in:
Thijs Louisse 2022-09-21 22:28:26 +02:00 committed by Thijs Louisse
parent ba16e23b00
commit 7e4cbecdc2
2 changed files with 78 additions and 2 deletions

View file

@ -76,10 +76,14 @@ async function getSourceCodeFragmentOfDeclaration({ filePath, exportedIdentifier
if (!isReferenced) { if (!isReferenced) {
// it must be an exported declaration // it must be an exported declaration
finalNodePath = variableDeclaratorPath.get('init'); finalNodePath = variableDeclaratorPath.node.init
? variableDeclaratorPath.get('init')
: variableDeclaratorPath;
} else { } else {
finalNodePath = getReferencedDeclaration({ finalNodePath = getReferencedDeclaration({
referencedIdentifierName: variableDeclaratorPath.node.init.name, referencedIdentifierName: variableDeclaratorPath.node.init
? variableDeclaratorPath.node.init.name
: variableDeclaratorPath.node.id.name,
globalScopeBindings, globalScopeBindings,
}); });
} }

View file

@ -52,6 +52,42 @@ describe('getSourceCodeFragmentOfDeclaration', () => {
expect(sourceFragment).to.equal('88'); 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', () => { describe('[default] specifiers', () => {
@ -103,5 +139,41 @@ describe('getSourceCodeFragmentOfDeclaration', () => {
expect(sourceFragment).to.equal('88'); 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() {}');
});
});
}); });
}); });