Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Use native parsers for lint exceptions

Manav Rathi65f2b1fd3970
diff --git a/android/checks/lint-exceptions/check.mjs b/android/checks/lint-exceptions/check.mjs
index 2df6b70b1e..1294f64ca5 100644
--- a/android/checks/lint-exceptions/check.mjs
+++ b/android/checks/lint-exceptions/check.mjs
@@ -1,193 +1,118 @@
import { execFileSync } from "node:child_process";
-import { existsSync, readFileSync } from "node:fs";
-import { resolve } from "node:path";
+import {
+ existsSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
+import { tmpdir } from "node:os";
+import { join, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
-const root = resolve(
- process.argv[2] ?? resolve(import.meta.dirname, "../../.."),
-);
const listPath = "android/checks/lint-exceptions/suppressions.json";
-const allowed = JSON.parse(readFileSync(resolve(root, listPath), "utf8"));
-const unused = new Map(
- Object.entries(allowed).map(([path, rules]) => [path, new Set(rules)]),
-);
-const files = execFileSync(
- "git",
- [
- "ls-files",
- "--cached",
- "--others",
- "--exclude-standard",
- "-z",
- "--",
- "android",
- ],
- { cwd: root, encoding: "utf8" },
-).split("\0");
-for (const path of new Set(files)) {
- if (
- !/\.(?:kt|kts|java|xml)$/.test(path) ||
- !existsSync(resolve(root, path))
- )
- continue;
- const source = readFileSync(resolve(root, path), "utf8");
- const check = (offset, rules) => {
- const line = source.slice(0, offset).split("\n").length;
+export function parse(sources) {
+ const temporary = mkdtempSync(join(tmpdir(), "ente-suppression-parser-"));
+ try {
+ writeFileSync(
+ join(temporary, "settings.gradle"),
+ 'rootProject.name = "lint-exceptions"\n',
+ );
+ writeFileSync(join(temporary, "sources.json"), JSON.stringify(sources));
+ execFileSync(
+ resolve(import.meta.dirname, "../../gradlew"),
+ [
+ "--quiet",
+ "--console=plain",
+ "--project-dir",
+ temporary,
+ "--init-script",
+ resolve(import.meta.dirname, "parse.gradle"),
+ "parseLintSuppressions",
+ ],
+ { stdio: ["ignore", "pipe", "inherit"] },
+ );
+ return JSON.parse(
+ readFileSync(join(temporary, "suppressions.json"), "utf8"),
+ );
+ } finally {
+ rmSync(temporary, { recursive: true, force: true });
+ }
+}
+
+export function check(records, allowed) {
+ const unused = new Map(
+ Object.entries(allowed).map(([path, rules]) => [path, new Set(rules)]),
+ );
+ const errors = [];
+ for (const { path, line, rules, error } of records) {
+ if (error) {
+ errors.push(`${path}:${line}: ${error}`);
+ continue;
+ }
if (
!rules.length ||
rules.some(
(rule) =>
- !/^[\w.:-]+$/.test(rule) || rule.toLowerCase() === "all",
+ typeof rule !== "string" ||
+ !/^[\w.:-]+$/.test(rule) ||
+ rule.toLowerCase() === "all",
)
) {
- reject(
- path,
- line,
- "Name the rules being suppressed using literal rule IDs",
+ errors.push(
+ `${path}:${line}: Name the rules being suppressed using literal rule IDs`,
);
- return;
+ continue;
}
for (const rule of rules) {
unused.get(path)?.delete(rule);
if (!allowed[path]?.includes(rule))
- reject(path, line, `${rule} is not listed in ${listPath}`);
- }
- };
- if (path.endsWith(".xml")) {
- const xml = source.replace(
- /<!--[\s\S]*?-->|<!\[CDATA\[[\s\S]*?\]\]>/g,
- (text) => " ".repeat(text.length),
- );
- const attributes = [
- ...xml.matchAll(/<(?:[^>"']|"[^"]*"|'[^']*')*>/g),
- ].flatMap((tag) =>
- [...tag[0].matchAll(/([\w.:-]+)\s*=\s*(["'])(.*?)\2/gs)].map(
- (attribute) => ({
- name: attribute[1],
- value: attribute[3],
- start: tag.index + attribute.index,
- }),
- ),
- );
- const prefixes = new Set(["tools"]);
- for (const { name, value } of attributes) {
- if (
- name.startsWith("xmlns:") &&
- value === "http://schemas.android.com/tools"
- ) {
- prefixes.add(name.slice(6));
- }
- }
- for (const { name, value, start } of attributes) {
- const [prefix, local] = name.split(":");
- if (local === "ignore" && prefixes.has(prefix)) {
- check(
- start,
- value.split(",").map((rule) => rule.trim()),
+ errors.push(
+ `${path}:${line}: ${rule} is not listed in ${listPath}`,
);
- }
- }
- continue;
- }
- const tokens = tokenize(source);
- const names = new Set(["Suppress", "SuppressLint", "SuppressWarnings"]);
- for (let i = 0; i < tokens.length; i++) {
- if (names.has(tokens[i].text) && tokens[i + 1]?.text === "as") {
- names.add(tokens[i + 2]?.text);
}
}
- let annotationGroup = false;
- for (let i = 0; i < tokens.length; i++) {
- const token = tokens[i];
- let prefix = i - 1;
- while (tokens[prefix]?.text === ".") prefix -= 2;
- if (tokens[prefix]?.text === ":") prefix -= 2;
- const annotated = tokens[prefix]?.text === "@";
- if (token.text === "[" && annotated) annotationGroup = true;
- if (token.text === "]") annotationGroup = false;
- if (token.text.startsWith("//") || token.text.startsWith("/*")) {
- const match = token.text.match(/\bnoinspection\b([^\r\n]*)/);
- if (match)
- check(
- token.start,
- match[1]
- .split("*/")[0]
- .trim()
- .split(/[\s,]+/)
- .filter(Boolean),
- );
- }
- if (
- !(annotated || annotationGroup) ||
- !names.has(token.text) ||
- tokens[i + 1]?.text !== "("
- )
- continue;
- const rules = [];
- let valid = true;
- let depth = 1;
- let j = i + 2;
- for (; j < tokens.length; j++) {
- const value = tokens[j].text;
- if (value === ")" && --depth === 0) break;
- if (value === "(") depth++;
- if (value === "(" || value === ")") continue;
- if (
- value.startsWith("//") ||
- value.startsWith("/*") ||
- /^[,{}\[\]]$/.test(value)
- )
- continue;
- if (
- (value === "names" || value === "value") &&
- tokens[j + 1]?.text === "="
- ) {
- j++;
- } else if (/^"[\w.:-]+"$/.test(value)) {
- rules.push(value.slice(1, -1));
- } else if (value === "arrayOf" && tokens[j + 1]?.text === "(") {
- continue;
- } else {
- valid = false;
- }
- }
- check(token.start, valid && j < tokens.length ? rules : []);
- i = j;
+ for (const [path, rules] of unused) {
+ for (const rule of rules)
+ errors.push(
+ `${path}: ${rule} has no suppression; remove it from ${listPath}`,
+ );
}
+ return errors;
}
-for (const [path, rules] of unused) {
- for (const rule of rules)
- reject(
+if (process.argv[1] === fileURLToPath(import.meta.url)) {
+ const root = resolve(
+ process.argv[2] ?? resolve(import.meta.dirname, "../../.."),
+ );
+ const files = execFileSync(
+ "git",
+ [
+ "ls-files",
+ "--cached",
+ "--others",
+ "--exclude-standard",
+ "-z",
+ "--",
+ "android",
+ ],
+ { cwd: root, encoding: "utf8" },
+ ).split("\0");
+ const sources = [...new Set(files)]
+ .filter(
+ (path) =>
+ /\.(?:kt|kts|java|xml)$/.test(path) &&
+ existsSync(resolve(root, path)),
+ )
+ .map((path) => ({
path,
- 1,
- `${rule} has no suppression; remove it from ${listPath}`,
- );
-}
-
-function tokenize(source) {
- const lexer =
- /\/\/[^\r\n]*|\/\*|"""[\s\S]*?"""|"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|[A-Za-z_]\w*|[^\s]/g;
- const tokens = [];
- let match;
- while ((match = lexer.exec(source))) {
- const start = match.index;
- if (match[0] === "/*") {
- const markers = /\/\*|\*\//g;
- markers.lastIndex = lexer.lastIndex;
- for (let depth = 1; depth; ) {
- const marker = markers.exec(source);
- if (!marker) throw new Error("Unclosed Android comment");
- depth += marker[0] === "/*" ? 1 : -1;
- }
- lexer.lastIndex = markers.lastIndex;
- }
- tokens.push({ text: source.slice(start, lexer.lastIndex), start });
- }
- return tokens;
-}
-
-function reject(path, line, message) {
- console.error(`${path}:${line}: ${message}`);
- process.exitCode = 1;
+ source: readFileSync(resolve(root, path), "utf8"),
+ }));
+ const errors = check(
+ parse(sources),
+ JSON.parse(readFileSync(resolve(root, listPath), "utf8")),
+ );
+ for (const error of errors) console.error(error);
+ process.exitCode = errors.length ? 1 : 0;
}
diff --git a/android/checks/lint-exceptions/parse.gradle b/android/checks/lint-exceptions/parse.gradle
new file mode 100644
index 0000000000..d6fdb36096
--- /dev/null
+++ b/android/checks/lint-exceptions/parse.gradle
@@ -0,0 +1,132 @@
+initscript {
+ dependencies {
+ classpath fileTree(dir: new File(gradle.gradleHomeDir, 'lib'), includes: [
+ 'kotlin-compiler-embeddable-*.jar', 'trove4j-*.jar', 'kotlinx-coroutines-*.jar'
+ ])
+ }
+}
+
+import groovy.json.JsonOutput
+import groovy.json.JsonSlurper
+import javax.xml.stream.XMLInputFactory
+import javax.xml.stream.XMLStreamConstants
+import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
+import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
+import org.jetbrains.kotlin.com.intellij.lang.java.JavaLanguage
+import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
+import org.jetbrains.kotlin.com.intellij.psi.*
+import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
+import org.jetbrains.kotlin.config.CompilerConfiguration
+import org.jetbrains.kotlin.psi.*
+
+List literalRules(expression) {
+ if (expression instanceof KtStringTemplateExpression) {
+ def literal = expression.entries.every {
+ it instanceof KtLiteralStringTemplateEntry || it instanceof KtEscapeStringTemplateEntry
+ }
+ if (!literal) return [null]
+ return [expression.entries.collect {
+ it instanceof KtEscapeStringTemplateEntry ? it.unescapedValue : it.text
+ }.join('')]
+ }
+ if (expression instanceof KtCollectionLiteralExpression) {
+ return expression.innerExpressions.collectMany { literalRules(it) }
+ }
+ if (expression instanceof KtCallExpression && expression.calleeExpression.text == 'arrayOf') {
+ return expression.valueArguments.collectMany { literalRules(it.argumentExpression) }
+ }
+ if (expression instanceof PsiLiteralExpression && expression.value instanceof String) {
+ return [expression.value]
+ }
+ if (expression instanceof PsiArrayInitializerMemberValue) {
+ return expression.initializers.collectMany { literalRules(it) }
+ }
+ return [null]
+}
+
+gradle.rootProject { project ->
+ project.tasks.register('parseLintSuppressions') {
+ doLast {
+ def inputs = new JsonSlurper().parse(project.file('sources.json'))
+ def records = []
+ def disposable = Disposer.newDisposable()
+ try {
+ def environment = KotlinCoreEnvironment.createForProduction(
+ disposable, new CompilerConfiguration(), EnvironmentConfigFiles.JVM_CONFIG_FILES
+ )
+ def kotlin = new KtPsiFactory(environment.project)
+ for (input in inputs) {
+ def record = { offset, rules, error = null ->
+ records.add([
+ path: input.path,
+ line: input.source.substring(0, offset).count('\n') + 1,
+ rules: rules, error: error
+ ])
+ }
+ if (input.path.endsWith('.xml')) {
+ def factory = XMLInputFactory.newFactory()
+ factory.setProperty(XMLInputFactory.SUPPORT_DTD, false)
+ factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false)
+ def reader = factory.createXMLStreamReader(new StringReader(input.source))
+ while (reader.hasNext()) {
+ if (reader.next() != XMLStreamConstants.START_ELEMENT) continue
+ def rules = reader.getAttributeValue(
+ 'http://schemas.android.com/tools', 'ignore'
+ )
+ if (rules != null) records.add([
+ path: input.path, line: reader.location.lineNumber,
+ rules: rules.split(',').collect { it.trim() }
+ ])
+ }
+ reader.close()
+ continue
+ }
+ def file = input.path.endsWith('.java')
+ ? PsiFileFactory.getInstance(environment.project).createFileFromText(
+ input.path, JavaLanguage.INSTANCE, input.source
+ )
+ : kotlin.createFile(input.path, input.source)
+ def names = ['Suppress', 'SuppressLint', 'SuppressWarnings'] as Set
+ if (file instanceof KtFile) {
+ file.importDirectives.each {
+ if (it.aliasName && it.importedFqName?.shortName()?.asString() in names) {
+ names.add(it.aliasName)
+ }
+ }
+ PsiTreeUtil.collectElementsOfType(file, KtTypeAlias).each { alias ->
+ if (PsiTreeUtil.collectElementsOfType(alias, KtUserType).any { it.referencedName in names }) {
+ record(alias.textOffset, [],
+ 'Use suppression annotations directly instead of type aliases')
+ }
+ }
+ PsiTreeUtil.collectElementsOfType(file, KtAnnotationEntry).each { annotation ->
+ if (annotation.shortName?.asString() in names) {
+ record(annotation.textOffset, annotation.valueArguments.collectMany {
+ literalRules(it.argumentExpression)
+ })
+ }
+ }
+ } else {
+ PsiTreeUtil.collectElementsOfType(file, PsiAnnotation).each { annotation ->
+ if (annotation.nameReferenceElement?.referenceName in names) {
+ record(annotation.textOffset, annotation.parameterList.attributes.collectMany {
+ literalRules(it.value)
+ })
+ }
+ }
+ }
+ PsiTreeUtil.collectElementsOfType(file, PsiComment).each { comment ->
+ def match = comment.text =~ /\bnoinspection\b([^\r\n]*)/
+ if (match.find()) {
+ record(comment.textOffset,
+ match.group(1).split('\\*/')[0].trim().split(/[\s,]+/).findAll { it })
+ }
+ }
+ }
+ } finally {
+ Disposer.dispose(disposable)
+ }
+ project.file('suppressions.json').text = JsonOutput.toJson(records)
+ }
+ }
+}
diff --git a/android/checks/lint-exceptions/test.mjs b/android/checks/lint-exceptions/test.mjs
index 786247a5a2..74cd2b8678 100644
--- a/android/checks/lint-exceptions/test.mjs
+++ b/android/checks/lint-exceptions/test.mjs
@@ -1,101 +1,94 @@
import assert from "node:assert/strict";
-import { spawnSync } from "node:child_process";
-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
-import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { check, parse } from "./check.mjs";
-const check = join(import.meta.dirname, "check.mjs");
-const root = mkdtempSync(join(tmpdir(), "ente-android-lint-exceptions-"));
-try {
- assert.equal(spawnSync("git", ["init", "-q"], { cwd: root }).status, 0);
- mkdirSync(join(root, "android/checks/lint-exceptions"), {
- recursive: true,
- });
- for (const [extension, source] of [
- ["kt", '@Suppress("NewApi") fun example() {}'],
- ["kts", '@file:Suppress("NewApi")'],
- ["kt", '@file:[Suppress("NewApi")]'],
- ["kt", '@Suppress(\n/* reason */ "NewApi",\n) fun example() {}'],
- ["kt", '@Suppress(names = ["NewApi"]) fun example() {}'],
- ["kt", '@Suppress(names = arrayOf("NewApi")) fun example() {}'],
- [
- "kt",
- 'import kotlin.Suppress as Quiet\n@Quiet("NewApi") fun example() {}',
- ],
- ["java", '@android.annotation.SuppressLint("NewApi") class Example {}'],
- ["java", '@SuppressWarnings(value = {"NewApi"}) class Example {}'],
- ["java", "//noinspection NewApi\nclass Example {}"],
- ["xml", '<view tools:ignore="NewApi" />'],
- [
- "xml",
- '<view xmlns:lint="http://schemas.android.com/tools" lint:ignore="NewApi" />',
- ],
- ]) {
- assert.equal(run(extension, source).status, 1);
- const file = `android/Example.${extension}`;
- const result = run(extension, source, { [file]: ["NewApi"] });
- assert.equal(result.status, 0, result.stderr);
- assert.equal(
- run(extension, source, { "android/Other.kt": ["NewApi"] }).status,
- 1,
+const suppressions = [
+ ["kt", '@Suppress("NewApi") fun example() {}'],
+ ["kts", '@file:Suppress("NewApi")'],
+ ["kt", '@file:[Suppress("NewApi")]'],
+ [
+ "kt",
+ '@[Other(values = ["value"]) SuppressLint("NewApi")] fun example() {}',
+ ],
+ ["kt", '@Suppress(\n/* reason */ "NewApi",\n) fun example() {}'],
+ ["kt", '@Suppress(names = ["NewApi"]) fun example() {}'],
+ ["kt", '@Suppress(names = arrayOf("NewApi")) fun example() {}'],
+ ["kt", '@Suppress(names = arrayOf<String>("NewApi")) fun example() {}'],
+ ["kt", '@Suppress(*arrayOf("NewApi")) fun example() {}'],
+ [
+ "kt",
+ 'import kotlin.Suppress as Quiet\n@Quiet("NewApi") fun example() {}',
+ ],
+ ["kt", 'val text = """${run {\n@Suppress("NewApi")\noldMethod()\n}}"""'],
+ ["java", '@android.annotation.SuppressLint("NewApi") class Example {}'],
+ ["java", '@SuppressWarnings(value = {"NewApi"}) class Example {}'],
+ ["java", "//noinspection NewApi\nclass Example {}"],
+ [
+ "xml",
+ '<root xmlns:tools="http://schemas.android.com/tools">\n <view\n tools:ignore="NewApi" />\n</root>',
+ 3,
+ ],
+ [
+ "xml",
+ '<view xmlns:lint="http://schemas.android.com/tools" lint:ignore="NewApi" />',
+ ],
+];
+const invalid = [
+ '@Suppress("NewApi", "MissingPermission") fun example() {}',
+ '@Suppress("all") fun example() {}',
+ "@Suppress(RULE) fun example() {}",
+ '@Suppress("New" + "Api") fun example() {}',
+ "//noinspection",
+ 'typealias Quiet = Suppress\n@Quiet("NewApi") fun example() {}',
+];
+const ordinary = [
+ [
+ "kt",
+ 'fun Suppress(text: String) {}\nfun example() { Suppress("NewApi") }',
+ ],
+ ["kt", '// @Suppress("NewApi")'],
+ ["kt", '/* outer /* nested */ @Suppress("NewApi") */'],
+ ["kt", 'val text = "@Suppress(\\"NewApi\\")"'],
+ ["kt", 'val text = """\n@Suppress("NewApi")\n//noinspection NewApi\n"""'],
+ ["xml", '<root><!-- <view tools:ignore="NewApi" /> --></root>'],
+ ["xml", '<value><![CDATA[tools:ignore="NewApi"]]></value>'],
+ ["xml", '<string name="example">tools:ignore="NewApi"</string>'],
+ ["xml", `<view text='tools:ignore="NewApi"' />`],
+];
+const sources = [
+ ...suppressions,
+ ...invalid.map((source) => ["kt", source]),
+ ...ordinary,
+].map(([extension, source], i) => ({
+ path: `android/Example${i}.${extension}`,
+ source,
+}));
+const parsed = parse(sources);
+for (const [i, { path, source }] of sources.entries()) {
+ const records = parsed.filter((record) => record.path === path);
+ const allowed = { [path]: ["NewApi"] };
+ if (i < suppressions.length) {
+ const line = suppressions[i][2];
+ if (line)
+ assert.deepEqual(
+ records.map((record) => record.line),
+ [line],
+ );
+ assert.ok(check(records, {}).length, source);
+ assert.deepEqual(check(records, allowed), [], source);
+ assert.ok(
+ check(records, { "android/Other.kt": ["NewApi"] }).length,
+ source,
);
+ assert.deepEqual(check([...records, ...records], allowed), []);
+ } else if (i < suppressions.length + invalid.length) {
+ assert.ok(check(records, {}).length, source);
+ assert.ok(check(records, allowed).length, source);
+ } else {
+ assert.deepEqual(check(records, {}), [], source);
}
- for (const source of [
- '@Suppress("NewApi", "MissingPermission")',
- '@Suppress("all")',
- "@Suppress(RULE)",
- '@Suppress("New" + "Api")',
- "//noinspection",
- ]) {
- assert.equal(
- run("kt", source, { "android/Example.kt": ["NewApi"] }).status,
- 1,
- );
- }
- for (const [extension, source] of [
- [
- "kt",
- 'fun Suppress(text: String) {}\nfun example() { Suppress("NewApi") }',
- ],
- ["kt", '// @Suppress("NewApi")'],
- ["kt", '/* outer /* nested */ @Suppress("NewApi") */'],
- ["kt", 'val text = "@Suppress(\\"NewApi\\")"'],
- [
- "kt",
- 'val text = """\n@Suppress("NewApi")\n//noinspection NewApi\n"""',
- ],
- ["xml", '<!-- <view tools:ignore="NewApi" /> -->'],
- ["xml", '<value><![CDATA[tools:ignore="NewApi"]]></value>'],
- ["xml", '<string name="example">tools:ignore="NewApi"</string>'],
- ["xml", `<view text='tools:ignore="NewApi"' />`],
- ]) {
- const result = run(extension, source);
- assert.equal(result.status, 0, result.stderr);
- }
- const suppression = '@SuppressLint("NewApi") fun example() {}\n';
- for (const source of [suppression + suppression, suppression]) {
- const result = run("kt", source, { "android/Example.kt": ["NewApi"] });
- assert.equal(result.status, 0, result.stderr);
- }
- for (const source of ["fun example() {}", null]) {
- const result = run("kt", source, { "android/Example.kt": ["NewApi"] });
- assert.equal(result.status, 1, result.stderr);
- assert.match(result.stderr, /NewApi has no suppression/);
- assert.equal(run("kt", source).status, 0);
- }
-} finally {
- rmSync(root, { recursive: true });
-}
-
-function run(extension, source, allowed = {}) {
- for (const extension of ["kt", "kts", "java", "xml"]) {
- rmSync(join(root, `android/Example.${extension}`), { force: true });
- }
- if (source !== null)
- writeFileSync(join(root, `android/Example.${extension}`), source);
- writeFileSync(
- join(root, "android/checks/lint-exceptions/suppressions.json"),
- JSON.stringify(allowed),
- );
- return spawnSync(process.execPath, [check, root], { encoding: "utf8" });
}
+assert.match(
+ check([], { "android/Deleted.kt": ["NewApi"] }).join("\n"),
+ /NewApi has no suppression/,
+);
+assert.deepEqual(check([], {}), []);
diff --git a/apple/checks/lint-exceptions/check.mjs b/apple/checks/lint-exceptions/check.mjs
index 641a23549c..b6fa2382ca 100644
--- a/apple/checks/lint-exceptions/check.mjs
+++ b/apple/checks/lint-exceptions/check.mjs
@@ -1,100 +1,101 @@
import { execFileSync } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
-import { resolve } from "node:path";
+import { dirname, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
-const root = resolve(
- process.argv[2] ?? resolve(import.meta.dirname, "../../.."),
-);
const listPath = "apple/checks/lint-exceptions/suppressions.json";
-const allowed = JSON.parse(readFileSync(resolve(root, listPath), "utf8"));
-const unused = new Map(
- Object.entries(allowed).map(([path, rules]) => [path, new Set(rules)]),
-);
-const files = execFileSync(
- "git",
- [
- "ls-files",
- "--cached",
- "--others",
- "--exclude-standard",
- "-z",
- "--",
- "apple",
- ],
- { cwd: root, encoding: "utf8" },
-).split("\0");
-for (const path of new Set(files)) {
- if (!path.endsWith(".swift") || !existsSync(resolve(root, path))) continue;
- const source = readFileSync(resolve(root, path), "utf8");
- for (const { text, start } of comments(source)) {
+export function parse(sources) {
+ const swift = execFileSync("xcrun", ["--find", "swift"], {
+ encoding: "utf8",
+ }).trim();
+ const libraries = resolve(dirname(swift), "../lib/swift/host");
+ return JSON.parse(
+ execFileSync(
+ swift,
+ [
+ "-module-cache-path",
+ resolve(import.meta.dirname, "../../.build/lint-exceptions"),
+ "-I",
+ libraries,
+ "-L",
+ libraries,
+ resolve(import.meta.dirname, "parse.swift"),
+ ],
+ { input: JSON.stringify(sources), encoding: "utf8" },
+ ),
+ );
+}
+
+export function check(comments, allowed) {
+ const unused = new Map(
+ Object.entries(allowed).map(([path, rules]) => [path, new Set(rules)]),
+ );
+ const errors = [];
+ for (const { path, text, line: startLine } of comments) {
const directives =
/\bswiftlint\s*:\s*disable(?::(?:next|previous|this))?\b([^\r\n]*)|\bswift-format-ignore(-file)?\b([^\r\n]*)/g;
for (const match of text.matchAll(directives)) {
- const line = source
- .slice(0, start + match.index)
- .split("\n").length;
+ const line =
+ startLine + text.slice(0, match.index).split("\n").length - 1;
const value = (match[1] ?? match[3]).split(/\/\/|\*\//)[0].trim();
const rules = value
.replace(/^:\s*/, "")
.split(/[\s,]+/)
.filter(Boolean);
if (match[2] || !rules.length || rules.includes("all")) {
- reject(path, line, "Name the rules being suppressed");
+ errors.push(`${path}:${line}: Name the rules being suppressed`);
continue;
}
for (const rule of rules) {
unused.get(path)?.delete(rule);
if (!allowed[path]?.includes(rule)) {
- reject(path, line, `${rule} is not listed in ${listPath}`);
+ errors.push(
+ `${path}:${line}: ${rule} is not listed in ${listPath}`,
+ );
}
}
}
}
-}
-
-for (const [path, rules] of unused) {
- for (const rule of rules) {
- reject(
- path,
- 1,
- `${rule} has no suppression; remove it from ${listPath}`,
- );
- }
-}
-
-function* comments(source) {
- const lexer = /\/\/[^\r\n]*|\/\*|(#*)("""|")/g;
- let match;
- while ((match = lexer.exec(source))) {
- const start = match.index;
- if (match[0].startsWith("//")) {
- yield { text: match[0], start };
- } else if (match[0] === "/*") {
- const markers = /\/\*|\*\//g;
- markers.lastIndex = lexer.lastIndex;
- for (let depth = 1; depth; ) {
- const marker = markers.exec(source);
- if (!marker) throw new Error("Unclosed Swift comment");
- depth += marker[0] === "/*" ? 1 : -1;
- }
- lexer.lastIndex = markers.lastIndex;
- yield { text: source.slice(start, lexer.lastIndex), start };
- } else {
- const end = match[2] + match[1];
- const escape = "\\" + match[1];
- let offset = lexer.lastIndex;
- while (offset < source.length && !source.startsWith(end, offset)) {
- offset += source.startsWith(escape, offset)
- ? escape.length + 1
- : 1;
- }
- lexer.lastIndex = offset + end.length;
- }
+ for (const [path, rules] of unused) {
+ for (const rule of rules)
+ errors.push(
+ `${path}: ${rule} has no suppression; remove it from ${listPath}`,
+ );
}
+ return errors;
}
-function reject(path, line, message) {
- console.error(`${path}:${line}: ${message}`);
- process.exitCode = 1;
+if (process.argv[1] === fileURLToPath(import.meta.url)) {
+ const root = resolve(
+ process.argv[2] ?? resolve(import.meta.dirname, "../../.."),
+ );
+ const files = execFileSync(
+ "git",
+ [
+ "ls-files",
+ "--cached",
+ "--others",
+ "--exclude-standard",
+ "-z",
+ "--",
+ "apple",
+ ],
+ { cwd: root, encoding: "utf8" },
+ ).split("\0");
+ const sources = [...new Set(files)]
+ .filter(
+ (path) =>
+ path.endsWith(".swift") && existsSync(resolve(root, path)),
+ )
+ .map((path) => ({
+ path,
+ source: readFileSync(resolve(root, path), "utf8"),
+ }));
+ const errors = check(
+ parse(sources),
+ JSON.parse(readFileSync(resolve(root, listPath), "utf8")),
+ );
+ for (const error of errors) console.error(error);
+ process.exitCode = errors.length ? 1 : 0;
}
diff --git a/apple/checks/lint-exceptions/parse.swift b/apple/checks/lint-exceptions/parse.swift
new file mode 100644
index 0000000000..de58a6852e
--- /dev/null
+++ b/apple/checks/lint-exceptions/parse.swift
@@ -0,0 +1,43 @@
+import Foundation
+import SwiftParser
+import SwiftSyntax
+
+struct Input: Decodable {
+ let path: String
+ let source: String
+}
+
+struct Comment: Encodable {
+ let path: String
+ let line: Int
+ let text: String
+}
+
+let inputs = try JSONDecoder().decode(
+ [Input].self, from: FileHandle.standardInput.readDataToEndOfFile())
+var comments: [Comment] = []
+for input in inputs {
+ let tree = Parser.parse(source: input.source)
+ let locations = SourceLocationConverter(fileName: input.path, tree: tree)
+ for token in tree.tokens(viewMode: .sourceAccurate) {
+ for (trivia, start) in [
+ (token.leadingTrivia, token.position),
+ (token.trailingTrivia, token.endPositionBeforeTrailingTrivia),
+ ] {
+ var position = start
+ for piece in trivia {
+ switch piece {
+ case .lineComment(let text), .blockComment(let text), .docLineComment(let text),
+ .docBlockComment(let text):
+ comments.append(
+ Comment(
+ path: input.path, line: locations.location(for: position).line,
+ text: text))
+ default: break
+ }
+ position = position.advanced(by: piece.sourceLength.utf8Length)
+ }
+ }
+ }
+}
+FileHandle.standardOutput.write(try JSONEncoder().encode(comments))
diff --git a/apple/checks/lint-exceptions/test.mjs b/apple/checks/lint-exceptions/test.mjs
index 11ece93bf7..1b60e9ef6b 100644
--- a/apple/checks/lint-exceptions/test.mjs
+++ b/apple/checks/lint-exceptions/test.mjs
@@ -1,80 +1,75 @@
import assert from "node:assert/strict";
-import { spawnSync } from "node:child_process";
-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
-import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { check, parse } from "./check.mjs";
-const check = join(import.meta.dirname, "check.mjs");
-const root = mkdtempSync(join(tmpdir(), "ente-apple-lint-exceptions-"));
-const file = "apple/Example.swift";
-try {
- assert.equal(spawnSync("git", ["init", "-q"], { cwd: root }).status, 0);
- mkdirSync(join(root, "apple/checks/lint-exceptions"), { recursive: true });
- for (const [source, rule] of [
- [
- "// swift-format-ignore: NeverForceUnwrap\nlet value = optional!",
- "NeverForceUnwrap",
- ],
- [
- "// swiftlint:disable:next empty_count\nlet empty = values.count == 0",
- "empty_count",
- ],
- [
- "let empty = values.count == 0 // swiftlint:disable:this empty_count",
- "empty_count",
- ],
- ["/* swiftlint:disable empty_count */", "empty_count"],
- [
- "/* outer /* nested */ swiftlint:disable empty_count */",
- "empty_count",
- ],
- ]) {
- assert.equal(run(source).status, 1);
- const result = run(source, { [file]: [rule] });
- assert.equal(result.status, 0, result.stderr);
- assert.equal(run(source, { "apple/Other.swift": [rule] }).status, 1);
+const suppressions = [
+ [
+ "// swift-format-ignore: NeverForceUnwrap\nlet value = optional!",
+ "NeverForceUnwrap",
+ ],
+ [
+ "// swiftlint:disable:next empty_count\nlet empty = values.count == 0",
+ "empty_count",
+ ],
+ [
+ "let empty = values.count == 0 // swiftlint:disable:this empty_count",
+ "empty_count",
+ ],
+ ["/* swiftlint:disable empty_count */", "empty_count"],
+ ["/* outer /* nested */ swiftlint:disable empty_count */", "empty_count"],
+ [
+ String.raw`let text = """
+\({
+ // swift-format-ignore: NeverForceUnwrap
+ return value!
+}())
+"""`,
+ "NeverForceUnwrap",
+ ],
+];
+const invalid = [
+ "// swift-format-ignore",
+ "// swift-format-ignore-file",
+ "// swiftlint:disable all",
+ "// swiftlint:disable",
+ "// swiftlint:disable empty_count force_cast",
+];
+const ordinary = [
+ "// swiftlint:enable empty_count",
+ 'let text = "// swiftlint:disable empty_count"',
+ 'let text = #"a "quote" // swiftlint:disable empty_count"#',
+ 'let text = """\n// swift-format-ignore-file\n"""',
+ 'let text = #"""\n""" // swift-format-ignore-file\n"""#',
+ "let expression = #/[//] swiftlint:disable all/#",
+];
+const sources = [
+ ...suppressions.map(([source]) => source),
+ ...invalid,
+ ...ordinary,
+].map((source, i) => ({ path: `apple/Example${i}.swift`, source }));
+const parsed = parse(sources);
+for (const [i, { path, source }] of sources.entries()) {
+ const comments = parsed.filter((record) => record.path === path);
+ if (i < suppressions.length) {
+ const rule = suppressions[i][1];
+ assert.ok(check(comments, {}).length, source);
+ assert.deepEqual(check(comments, { [path]: [rule] }), [], source);
+ assert.ok(
+ check(comments, { "apple/Other.swift": [rule] }).length,
+ source,
+ );
+ assert.deepEqual(
+ check([...comments, ...comments], { [path]: [rule] }),
+ [],
+ );
+ } else if (i < suppressions.length + invalid.length) {
+ assert.ok(check(comments, {}).length, source);
+ assert.ok(check(comments, { [path]: ["empty_count"] }).length, source);
+ } else {
+ assert.deepEqual(check(comments, {}), [], source);
}
- for (const source of [
- "// swift-format-ignore",
- "// swift-format-ignore-file",
- "// swiftlint:disable all",
- "// swiftlint:disable",
- "// swiftlint:disable empty_count force_cast",
- ]) {
- assert.equal(run(source, { [file]: ["empty_count"] }).status, 1);
- }
- for (const source of [
- "// swiftlint:enable empty_count",
- 'let text = "// swiftlint:disable empty_count"',
- 'let text = #"a "quote" // swiftlint:disable empty_count"#',
- 'let text = """\n// swift-format-ignore-file\n"""',
- 'let text = #"""\n""" // swift-format-ignore-file\n"""#',
- ]) {
- const result = run(source);
- assert.equal(result.status, 0, result.stderr);
- }
- const suppression =
- "// swiftlint:disable:next empty_count\nlet empty = values.count == 0\n";
- for (const source of [suppression + suppression, suppression]) {
- const result = run(source, { [file]: ["empty_count"] });
- assert.equal(result.status, 0, result.stderr);
- }
- for (const source of ["let value = 1", null]) {
- const result = run(source, { [file]: ["empty_count"] });
- assert.equal(result.status, 1, result.stderr);
- assert.match(result.stderr, /empty_count has no suppression/);
- assert.equal(run(source).status, 0);
- }
-} finally {
- rmSync(root, { recursive: true });
-}
-
-function run(source, allowed = {}) {
- if (source === null) rmSync(join(root, file), { force: true });
- else writeFileSync(join(root, file), source);
- writeFileSync(
- join(root, "apple/checks/lint-exceptions/suppressions.json"),
- JSON.stringify(allowed),
- );
- return spawnSync(process.execPath, [check, root], { encoding: "utf8" });
}
+assert.match(
+ check([], { "apple/Deleted.swift": ["empty_count"] }).join("\n"),
+ /empty_count has no suppression/,
+);
+assert.deepEqual(check([], {}), []);