play build versionCode source check: follow a variable (def / ext. / val) to its definition; a variable read from the property is not flagged

This commit is contained in:
CRTkafa 2026-10-12 11:34:41 +08:00
parent 0b1451800c
commit 2e7dd44ae6
3 changed files with 23 additions and 2 deletions

View file

@ -128,8 +128,15 @@ export function versionSourceIssues(file: string, src = readFileSync(file, 'utf8
continue;
}
const h = hit as { line: number; text: string; value: string };
const reads = new RegExp(`(?:findProperty|property|hasProperty|getProperty|gradleProperty)\\s*\\(\\s*['"]${field}['"]|properties\\s*\\[\\s*['"]${field}['"]`).test(h.value);
if (reads) continue;
const readsRe = new RegExp(`(?:findProperty|property|hasProperty|getProperty|gradleProperty)\\s*\\(\\s*['"]${field}['"]|properties\\s*\\[\\s*['"]${field}['"]`);
if (readsRe.test(h.value)) continue;
// a variable set from the property first (`def code = (project.findProperty('versionCode') ?: ...).toInteger()`,
// then `versionCode code`): follow the name to its definition anywhere in the file
const id = /^\(?\s*(?:project\.)?(?:ext\.)?([A-Za-z_]\w*)\s*(?:\.\s*\w+\s*\(\s*\)\s*)*(?:as\s+\w+\s*)?\)?$/.exec(h.value)?.[1];
if (id && !/^\d/.test(id)) {
const def = new RegExp(`(?:\\b(?:def|val|var|final)\\s+(?:\\w+\\s+)?|\\bext\\.|\\bextra\\[\\s*["']|^\\s*)${id}\\b["']?\\s*\\]?\\s*(?::\\s*\\w+\\??\\s*)?=\\s*([^\\n]+)`, 'm').exec(code);
if (def && readsRe.test(def[1]!)) continue;
}
const lit = /^\(?\s*(\d+|"[^"]*"|'[^']*')\s*\)?$/.exec(h.value);
const current = lit ? lit[1]!.replace(/^['"]|['"]$/g, '')
: field === 'versionCode' ? (/(?:\?:|\|\||,)\s*['"]?(\d+)['"]?/.exec(h.value)?.[1] ?? /\b(\d+)\b/.exec(h.value)?.[1] ?? fallback)