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
168 changes: 141 additions & 27 deletions src/core-open/scanners/swift-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,45 +72,159 @@ interface SwiftDependency {

/**
* Parse Package.swift to extract dependencies.
*
* Example:
*
* Remote and registry dependencies:
* .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0")
* .package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.0.0"))
* .package(url: "https://github.com/realm/realm-swift.git", exact: "10.0.0")
* .package(url: "https://github.com/apple/swift-argument-parser", "1.2.0"..<"2.0.0")
* .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", "1.10.0"..."1.17.0")
* .package(name: "Kingfisher", url: "https://github.com/onevcat/Kingfisher.git", from: "7.0.0")
* .package(id: "mona.LinkedList", from: "1.0.0")
*
* Local `.package(path:)` dependencies are ignored: they have no version to drift.
*/
function parsePackageSwift(content: string): SwiftDependency[] {
const deps: SwiftDependency[] = [];

// Match .package(url: "...", ...)
const packageRegex = /\.package\s*\(\s*url:\s*"([^"]+)"[^)]*?(from|exact|upToNextMajor|revision|branch):\s*"([^"]+)"/g;

const callRe = /\.package\s*\(/g;
let match: RegExpExecArray | null;
while ((match = packageRegex.exec(content)) !== null) {
const url = match[1]!;
const versionType = match[2]!;
const versionValue = match[3]!;

// Extract package name from URL
const nameMatch = url.match(/\/([^/]+?)(\.git)?$/);
const name = nameMatch?.[1] ?? url;

let type: SwiftDependency['type'] = 'range';
let version = versionValue;

if (versionType === 'exact') {
type = 'exact';
} else if (versionType === 'branch') {
type = 'branch';
} else if (versionType === 'revision') {
type = 'revision';
while ((match = callRe.exec(content)) !== null) {
const openParen = match.index + match[0].length - 1;
const extracted = extractPackageCall(content, openParen);
if (!extracted) {
callRe.lastIndex = openParen + 1;
continue;
}

deps.push({ name, url, version, type });
callRe.lastIndex = extracted.end;
const dep = dependencyFromPackageArgs(extracted.args);
if (dep) deps.push(dep);
}

return deps;
}

/** Args of one `.package(` call, starting at the opening parenthesis. */
function extractPackageCall(content: string, openParen: number): { args: string; end: number } | null {
let depth = 0;
let inString = false;
let escaped = false;
for (let i = openParen; i < content.length; i++) {
const c = content[i]!;
if (inString) {
if (escaped) {
escaped = false;
continue;
}
if (c === '\\') {
escaped = true;
continue;
}
if (c === '"') inString = false;
continue;
}
if (c === '"') {
inString = true;
continue;
}
if (c === '(') depth++;
else if (c === ')') {
depth--;
if (depth === 0) return { args: content.slice(openParen + 1, i), end: i + 1 };
}
}
return null;
}

/** Drop `//` and block comments without eating `//` inside string literals. */
function stripSwiftComments(input: string): string {
let out = '';
let i = 0;
while (i < input.length) {
const c = input[i]!;
if (c === '"') {
const start = i;
i++;
while (i < input.length) {
if (input[i] === '\\') {
i += 2;
continue;
}
if (input[i] === '"') {
i++;
break;
}
i++;
}
out += input.slice(start, i);
continue;
}
if (c === '/' && input[i + 1] === '/') {
while (i < input.length && input[i] !== '\n') i++;
continue;
}
if (c === '/' && input[i + 1] === '*') {
i += 2;
while (i < input.length && !(input[i] === '*' && input[i + 1] === '/')) i++;
i = Math.min(input.length, i + 2);
continue;
}
out += c;
i++;
}
return out;
}

function packageNameFromUrl(url: string): string {
const nameMatch = url.match(/\/([^/]+?)(\.git)?$/);
return nameMatch?.[1] ?? url;
}

/**
* Version requirement on a `.package` call.
* `from:` / `.upToNextMajor(from:)` / `.upToNextMinor(from:)` stay ranges whose
* current spec is the declared lower bound, matching the labeled forms.
* `"lo"..<"hi"` and `"lo"..."hi"` use that same lower bound.
*/
function versionFromPackageArgs(args: string): { value: string; type: SwiftDependency['type'] } | null {
const exact = args.match(/\bexact\s*:\s*"([^"]+)"/);
if (exact?.[1]) return { value: exact[1], type: 'exact' };
const branch = args.match(/\bbranch\s*:\s*"([^"]+)"/);
if (branch?.[1]) return { value: branch[1], type: 'branch' };
const revision = args.match(/\brevision\s*:\s*"([^"]+)"/);
if (revision?.[1]) return { value: revision[1], type: 'revision' };
const from = args.match(/\bfrom\s*:\s*"([^"]+)"/);
if (from?.[1]) return { value: from[1], type: 'range' };
const halfOpen = args.match(/"([^"]+)"\s*\.\.<\s*"[^"]*"/);
if (halfOpen?.[1]) return { value: halfOpen[1], type: 'range' };
const closed = args.match(/"([^"]+)"\s*\.\.\.\s*"[^"]*"/);
if (closed?.[1]) return { value: closed[1], type: 'range' };
return null;
}

function dependencyFromPackageArgs(rawArgs: string): SwiftDependency | null {
const args = stripSwiftComments(rawArgs);
// Local packages (`.package(path:)`, including `name:` + `path:`) have no registry version.
const withoutStrings = args.replace(/"(?:\\.|[^"\\])*"/g, '""');
if (/\bpath\s*:/.test(withoutStrings)) return null;

const version = versionFromPackageArgs(args);
if (!version) return null;

const urlMatch = args.match(/\burl\s*:\s*"([^"]+)"/);
if (urlMatch?.[1]) {
const url = urlMatch[1];
// Identity follows the URL, including when the deprecated `name:` label comes first.
// Package.resolved pins that identity, not the manifest's name label.
return { name: packageNameFromUrl(url), url, version: version.value, type: version.type };
}

const idMatch = args.match(/\bid\s*:\s*"([^"]+)"/);
if (idMatch?.[1]) {
const id = idMatch[1];
return { name: id, url: id, version: version.value, type: version.type };
}
return null;
}

/**
* Parse Package.resolved to get exact resolved versions.
*
Expand Down
Loading
Loading