Skip to content

Commit fa4c8f5

Browse files
committed
fix: ensure that REUSE-Ignore is applied correctly
1 parent 8098941 commit fa4c8f5

File tree

4 files changed

+135
-54
lines changed

4 files changed

+135
-54
lines changed

src/parser.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,33 @@ import * as commentIt from "@dev-build-deploy/comment-it";
1010
* @internal
1111
*/
1212
const SPDXTokens = {
13-
copyright: "SPDX-FileCopyrightText",
14-
license: "SPDX-License-Identifier",
15-
attributionText: "SPDX-FileAttributionText", // TODO: Multiline support
16-
comment: "SPDX-FileComment", // TODO: Multiline support
17-
contributor: "SPDX-FileContributor",
18-
licenseComments: "SPDX-LicenseComments", // TODO: Multiline support
19-
licenseConcluded: "SPDX-LicenseConcluded",
20-
licenseInfoInFile: "SPDX-LicenseInfoInFile",
21-
notice: "SPDX-FileNotice", // TODO: Multiline support
22-
type: "SPDX-FileType",
13+
copyright: "SPDX-FileCopyrightText:",
14+
license: "SPDX-License-Identifier:",
15+
attributionText: "SPDX-FileAttributionText:", // TODO: Multiline support
16+
comment: "SPDX-FileComment:", // TODO: Multiline support
17+
contributor: "SPDX-FileContributor:",
18+
licenseComments: "SPDX-LicenseComments:", // TODO: Multiline support
19+
licenseConcluded: "SPDX-LicenseConcluded:",
20+
licenseInfoInFile: "SPDX-LicenseInfoInFile:",
21+
notice: "SPDX-FileNotice:", // TODO: Multiline support
22+
type: "SPDX-FileType:",
23+
ignore: "REUSE-Ignore",
2324
} as const;
2425

26+
/** @internal */
27+
export type Token = {
28+
start: number;
29+
end: number;
30+
type: keyof typeof SPDXTokens;
31+
};
32+
2533
/**
26-
* Resulting token
34+
* Token and Data extracted from content
2735
* @internal
2836
*/
29-
type Token = {
30-
start: number;
31-
end: number;
37+
export type ExtractedToken = {
3238
type: keyof typeof SPDXTokens;
39+
data: string;
3340
};
3441

3542
/**
@@ -41,14 +48,14 @@ function generateToken(line: string): Token | undefined {
4148
const tokenKeys = Object.keys(SPDXTokens) as (keyof typeof SPDXTokens)[];
4249

4350
for (const key of tokenKeys) {
44-
const match = new RegExp(`${SPDXTokens[key]}:`, "i").exec(line);
51+
const match = new RegExp(`${SPDXTokens[key]}`, "i").exec(line);
4552
if (match === null) {
4653
continue;
4754
}
4855

4956
return {
5057
start: match.index,
51-
end: line.indexOf(":") + 1,
58+
end: line.indexOf(SPDXTokens[key]) + SPDXTokens[key].length,
5259
type: key as keyof typeof SPDXTokens,
5360
};
5461
}
@@ -59,7 +66,7 @@ function generateToken(line: string): Token | undefined {
5966
* @param comment The comment to extract the data from
6067
* @internal
6168
*/
62-
export function* extractData(comment: commentIt.IComment) {
69+
export function* extractData(comment: commentIt.IComment): Generator<ExtractedToken> {
6370
for (const line of comment.contents) {
6471
const match = generateToken(line.value);
6572
if (match) {

src/spdx/file.ts

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,17 @@ export class SpdxFile {
142142
index += 1;
143143
}
144144

145-
return await parseComment(comment, file);
145+
let ignore = false;
146+
for await (const token of parser.extractData(comment)) {
147+
if (token.type === "ignore") {
148+
ignore = token.data.startsWith("Start");
149+
}
150+
if (ignore) continue;
151+
152+
file = await updateFromToken(token, file);
153+
}
154+
155+
return file;
146156
}
147157

148158
/**
@@ -151,9 +161,19 @@ export class SpdxFile {
151161
* @returns The updated SPDX file
152162
*/
153163
async function parseFile(file: SpdxFile): Promise<SpdxFile> {
164+
let ignore = false;
165+
154166
for await (const comment of commentIt.extractComments(file.fileName)) {
155-
file = await parseComment(comment, file);
167+
for await (const token of parser.extractData(comment)) {
168+
if (token.type === "ignore") {
169+
ignore = token.data.startsWith("Start");
170+
}
171+
if (ignore) continue;
172+
173+
file = await updateFromToken(token, file);
174+
}
156175
}
176+
157177
return file;
158178
}
159179

@@ -163,42 +183,40 @@ export class SpdxFile {
163183
* @param file SPDX File to update
164184
* @returns The updated SPDX file
165185
*/
166-
async function parseComment(comment: commentIt.IComment, file: SpdxFile) {
167-
for await (const token of parser.extractData(comment)) {
168-
switch (token.type) {
169-
case "attributionText":
170-
file.attributionTexts.push(token.data);
171-
break;
172-
case "comment":
173-
file.comment = token.data;
174-
break;
175-
case "contributor":
176-
file.fileContributors.push(token.data);
177-
break;
178-
case "licenseComments":
179-
file.licenseComments = token.data;
180-
break;
181-
case "licenseConcluded":
182-
file.licenseConcluded = token.data;
183-
break;
184-
case "copyright":
185-
file.copyrightText = token.data;
186-
break;
187-
case "notice":
188-
file.noticeText = token.data;
189-
break;
190-
case "type":
191-
file.fileTypes.push(token.data as IFileType);
192-
break;
193-
case "licenseInfoInFile":
194-
case "license": {
195-
if (file.licenseInfoInFiles.includes("NOASSERTION")) {
196-
file.licenseInfoInFiles = [token.data];
197-
} else {
198-
file.licenseInfoInFiles.push(token.data);
199-
}
200-
break;
186+
async function updateFromToken(token: parser.ExtractedToken, file: SpdxFile) {
187+
switch (token.type) {
188+
case "attributionText":
189+
file.attributionTexts.push(token.data);
190+
break;
191+
case "comment":
192+
file.comment = token.data;
193+
break;
194+
case "contributor":
195+
file.fileContributors.push(token.data);
196+
break;
197+
case "licenseComments":
198+
file.licenseComments = token.data;
199+
break;
200+
case "licenseConcluded":
201+
file.licenseConcluded = token.data;
202+
break;
203+
case "copyright":
204+
file.copyrightText = token.data;
205+
break;
206+
case "notice":
207+
file.noticeText = token.data;
208+
break;
209+
case "type":
210+
file.fileTypes.push(token.data as IFileType);
211+
break;
212+
case "licenseInfoInFile":
213+
case "license": {
214+
if (file.licenseInfoInFiles.includes("NOASSERTION")) {
215+
file.licenseInfoInFiles = [token.data];
216+
} else {
217+
file.licenseInfoInFiles.push(token.data);
201218
}
219+
break;
202220
}
203221
}
204222
return file;

test/fixtures/reuse-ignore.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// REUSE-IgnoreStart
2+
/*
3+
* SPDX-FileCopyrightText: 2022 Kevin de Jong <monkaii@hotmail.com>
4+
* SPDX-License-Identifier: DOES-NOT-EXIST
5+
*/
6+
// REUSE-IgnoreEnd
7+
8+
/*
9+
* SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com>
10+
* SPDX-License-Identifier: MIT
11+
*/
12+
13+
export function foo() {
14+
return "bar";
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"SPDXID": "SPDXRef-DOCUMENT",
3+
"spdxVersion": "SPDX-2.3",
4+
"documentNamespace": "http://spdx.org/spdxdocs/spdx-v2.3-45eae250-b782-46dd-9723-62ec3bed2a7c",
5+
"dataLicense": "CC0-1.0",
6+
"relationships": [
7+
{
8+
"spdxElementId": "SPDXRef-DOCUMENT",
9+
"relationshipType": "DESCRIBES",
10+
"relatedSpdxElement": "SPDXRef-20b324b89aea17f44303a8a3b7baa63059044142"
11+
}
12+
],
13+
"files": [
14+
{
15+
"checksums": [
16+
{
17+
"algorithm": "SHA1",
18+
"checksumValue": "9eb7d3759a535f1442fadb2f9ebcdbcf707d5cb3"
19+
}
20+
],
21+
"fileContributors": [],
22+
"fileTypes": [],
23+
"licenseConcluded": "NOASSERTION",
24+
"licenseInfoInFiles": [
25+
"MIT"
26+
],
27+
"attributionTexts": [],
28+
"fileName": "./test/fixtures/reuse-ignore.ts",
29+
"SPDXID": "SPDXRef-20b324b89aea17f44303a8a3b7baa63059044142",
30+
"copyrightText": "2023 Kevin de Jong <monkaii@hotmail.com>"
31+
}
32+
],
33+
"name": "test-project",
34+
"creationInfo": {
35+
"comment": "Generated by ReuseIt-v0",
36+
"created": "2023-01-01T00:00:00.000Z",
37+
"creators": [
38+
"Tool: ReuseIt-v0"
39+
]
40+
}
41+
}

0 commit comments

Comments
 (0)