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
53 changes: 39 additions & 14 deletions dist/core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/core.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 40 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,33 @@ function filterSupported(files) {
function toRef(sym, file) {
return { name: sym.name, file, kind: sym.kind };
}
function groupByKey(symbols) {
const groups = new Map();
for (const sym of symbols) {
const key = `${sym.name}:${sym.kind}`;
const group = groups.get(key);
if (group)
group.push(sym);
else
groups.set(key, [sym]);
}
return groups;
}
function hasSignatureChange(oldGroup, newGroup) {
const oldSigs = oldGroup
.map((s) => s.signature)
.filter(Boolean)
.sort();
const newSigs = newGroup
.map((s) => s.signature)
.filter(Boolean)
.sort();
if (oldSigs.length === 0 || newSigs.length === 0)
return false;
if (oldSigs.length !== newSigs.length)
return true;
return oldSigs.some((sig, i) => sig !== newSigs[i]);
}
/**
* Store a symbol footprint for a merged PR.
* Shared by CLI and GitHub Action entry points.
Expand Down Expand Up @@ -391,8 +418,9 @@ async function runCheck(opts) {
if (!oldSource)
continue;
const oldSymbols = await (0, ast_1.extractSymbols)(oldSource, (0, config_1.getLanguage)(filePath));
for (const sym of oldSymbols) {
deletedSymbols.push(toRef(sym, (0, resolve_1.canonicalizePath)(filePath)));
const canonical = (0, resolve_1.canonicalizePath)(filePath);
for (const [, group] of groupByKey(oldSymbols)) {
deletedSymbols.push(toRef(group[0], canonical));
}
continue;
}
Expand All @@ -405,19 +433,16 @@ async function runCheck(opts) {
const lang = (0, config_1.getLanguage)(file.newPath);
const oldSymbols = await (0, ast_1.extractSymbols)(oldSource, lang);
const newSymbols = await (0, ast_1.extractSymbols)(newSource, lang);
const newSymbolMap = new Map();
for (const sym of newSymbols) {
newSymbolMap.set(`${sym.name}:${sym.kind}`, sym);
}
for (const oldSym of oldSymbols) {
const key = `${oldSym.name}:${oldSym.kind}`;
const newSym = newSymbolMap.get(key);
const canonical = (0, resolve_1.canonicalizePath)(file.newPath ?? file.oldPath);
if (!newSym) {
deletedSymbols.push(toRef(oldSym, canonical));
}
else if (oldSym.signature && newSym.signature && oldSym.signature !== newSym.signature) {
modifiedSymbols.push(toRef(oldSym, canonical));
const canonical = (0, resolve_1.canonicalizePath)(file.newPath ?? file.oldPath);
const oldGroups = groupByKey(oldSymbols);
const newGroups = groupByKey(newSymbols);
for (const [key, oldGroup] of oldGroups) {
const newGroup = newGroups.get(key);
if (!newGroup) {
deletedSymbols.push(toRef(oldGroup[0], canonical));
}
else if (hasSignatureChange(oldGroup, newGroup)) {
modifiedSymbols.push(toRef(oldGroup[0], canonical));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

52 changes: 37 additions & 15 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ function toRef(sym: ExtractedSymbol, file: string): SymbolRef {
return { name: sym.name, file, kind: sym.kind };
}

function groupByKey(symbols: ExtractedSymbol[]): Map<string, ExtractedSymbol[]> {
const groups = new Map<string, ExtractedSymbol[]>();
for (const sym of symbols) {
const key = `${sym.name}:${sym.kind}`;
const group = groups.get(key);
if (group) group.push(sym);
else groups.set(key, [sym]);
}
return groups;
}

function hasSignatureChange(oldGroup: ExtractedSymbol[], newGroup: ExtractedSymbol[]): boolean {
const oldSigs = oldGroup
.map((s) => s.signature)
.filter(Boolean)
.sort();
const newSigs = newGroup
.map((s) => s.signature)
.filter(Boolean)
.sort();
if (oldSigs.length === 0 || newSigs.length === 0) return false;
if (oldSigs.length !== newSigs.length) return true;
return oldSigs.some((sig, i) => sig !== newSigs[i]);
}

/**
* Store a symbol footprint for a merged PR.
* Shared by CLI and GitHub Action entry points.
Expand Down Expand Up @@ -130,8 +155,9 @@ export async function runCheck(opts: CheckOptions): Promise<CheckResult> {
const oldSource = getFileAtRef(opts.base, filePath);
if (!oldSource) continue;
const oldSymbols = await extractSymbols(oldSource, getLanguage(filePath));
for (const sym of oldSymbols) {
deletedSymbols.push(toRef(sym, canonicalizePath(filePath)));
const canonical = canonicalizePath(filePath);
for (const [, group] of groupByKey(oldSymbols)) {
deletedSymbols.push(toRef(group[0], canonical));
}
continue;
}
Expand All @@ -145,21 +171,17 @@ export async function runCheck(opts: CheckOptions): Promise<CheckResult> {
const lang = getLanguage(file.newPath!);
const oldSymbols = await extractSymbols(oldSource, lang);
const newSymbols = await extractSymbols(newSource, lang);
const canonical = canonicalizePath(file.newPath ?? file.oldPath!);

const newSymbolMap = new Map<string, ExtractedSymbol>();
for (const sym of newSymbols) {
newSymbolMap.set(`${sym.name}:${sym.kind}`, sym);
}

for (const oldSym of oldSymbols) {
const key = `${oldSym.name}:${oldSym.kind}`;
const newSym = newSymbolMap.get(key);
const canonical = canonicalizePath(file.newPath ?? file.oldPath!);
const oldGroups = groupByKey(oldSymbols);
const newGroups = groupByKey(newSymbols);

if (!newSym) {
deletedSymbols.push(toRef(oldSym, canonical));
} else if (oldSym.signature && newSym.signature && oldSym.signature !== newSym.signature) {
modifiedSymbols.push(toRef(oldSym, canonical));
for (const [key, oldGroup] of oldGroups) {
const newGroup = newGroups.get(key);
if (!newGroup) {
deletedSymbols.push(toRef(oldGroup[0], canonical));
} else if (hasSignatureChange(oldGroup, newGroup)) {
modifiedSymbols.push(toRef(oldGroup[0], canonical));
}
}
}
Expand Down
Loading
Loading