Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions libs/core/src/true-affected.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ describe('trueAffected', () => {
filePath: 'proj1/index.ts',
changedLines: [2],
},
{
filePath: 'proj2/index.ts',
changedLines: [6],
},
];
jest.spyOn(git, 'getChangedFiles').mockReturnValue(changedFiles);

Expand Down Expand Up @@ -512,10 +516,26 @@ describe('trueAffected', () => {
`Added package proj1 to affected packages for changed line ${changedFiles[0].changedLines[0]} in ${changedFiles[0].filePath}`
);
expect(debug).toHaveBeenCalledWith(
expect.stringMatching(
new RegExp(`^Found identifier .* in .*${changedFiles[0].filePath}$`)
)
`Added package proj2 to affected packages for changed line ${changedFiles[1].changedLines[0]} in ${changedFiles[1].filePath}`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EladBezalel Could not write a viable test for my change without massively splitting the trueAffected func, so decided to go in a different direction: there was no test I could find ensuring we're not revisiting nodes, so I tweaked this one for the purpose. not exactly the same test, but as useful WDYT?

);

const project1VisitingLogs = debug.mock.calls.filter(
([arg]) => expect.stringMatching(
new RegExp(`^Visiting .* in .*${changedFiles[0].filePath}$`)
).asymmetricMatch(arg)
);

expect(project1VisitingLogs).toHaveLength(1);

const project2VisitingLogs = debug.mock.calls.filter(
([arg]) => expect.stringMatching(
new RegExp(`^Visiting .* in .*${changedFiles[1].filePath}$`)
).asymmetricMatch(arg)
);
// would have duplicates if visited multiple times
const uniqueProject2VisitingLogs = new Set(project2VisitingLogs.map(([arg]) => arg));
expect(uniqueProject2VisitingLogs.size).toBe(project2VisitingLogs.length);

expect(debug).toHaveBeenCalledWith(
'Added package proj2 to affected packages'
);
Expand Down
14 changes: 9 additions & 5 deletions libs/core/src/true-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export const trueAffected = async ({
}

const affectedPackages = new Set<string>(changedIncludedFilesPackages);
const visitedIdentifiers = new Map<string, string[]>();
const visitedIdentifiers = new Map<string, Set<string>>();

const findReferencesLibs = (node: Node<ts.Node>) => {
const rootNode = findRootNode(node);
Expand Down Expand Up @@ -269,7 +269,6 @@ export const trueAffected = async ({
/* istanbul ignore next */
if (identifier == null) return;

const refs = identifier.findReferencesAsNodes();
const identifierName = identifier.getText();
const path = rootNode.getSourceFile().getFilePath();

Expand All @@ -278,22 +277,27 @@ export const trueAffected = async ({
);

if (identifierName && path) {
const visited = visitedIdentifiers.get(path) ?? [];
if (visited.includes(identifierName)) {
if (!visitedIdentifiers.has(path)) {
visitedIdentifiers.set(path, new Set());
}
const visited = visitedIdentifiers.get(path)!;
if (visited.has(identifierName)) {
logger.debug(
`Already visited ${chalk.bold(identifierName)} in ${chalk.bold(path)}`
);

return;
}

visitedIdentifiers.set(path, [...visited, identifierName]);
visited.add(identifierName);

logger.debug(
`Visiting ${chalk.bold(identifierName)} in ${chalk.bold(path)}`
);
}

const refs = identifier.findReferencesAsNodes();

refs.forEach((node) => {
const sourceFile = node.getSourceFile();
const pkg = getPackageNameByPath(sourceFile.getFilePath(), projects);
Expand Down