diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-06-26 09:38:55 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-26 09:38:55 -0600 |
commit | fa935e553a9ec37d39d2274432a00f1b465cef0f (patch) | |
tree | d5024190474b6ee596977a6dde81335fb491d351 /tools/copyright_checker.js | |
parent | 801b9ec62d94f201e67d053ee90dae0b70e50a42 (diff) |
chore: Ensure copyright line is the first in the file (#19608)
The copyright checker was allowing files with code above the copyright
line in a few places, mainly as a result of IDEs ordering imports
improperly.
This makes the check more robust, and adds a whitelist of valid lines
that may appear before the copyright line.
Diffstat (limited to 'tools/copyright_checker.js')
-rw-r--r-- | tools/copyright_checker.js | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/tools/copyright_checker.js b/tools/copyright_checker.js index 4911d6a05..5f53eab08 100644 --- a/tools/copyright_checker.js +++ b/tools/copyright_checker.js @@ -42,29 +42,47 @@ export async function checkCopyright() { const errors = []; const sourceFilesSet = new Set(sourceFiles); + const ERROR_MSG = "Copyright header is missing: "; - for (const file of sourceFilesSet) { - const ERROR_MSG = "Copyright header is missing: "; + // Acceptable content before the copyright line + const ACCEPTABLE_LINES = + /^(\/\/ deno-lint-.*|\/\/ Copyright.*|\/\/ Ported.*|\s*|#!\/.*)\n/; + const COPYRIGHT_LINE = + "Copyright 2018-2023 the Deno authors. All rights reserved. MIT license."; + const TOML_COPYRIGHT_LINE = "# " + COPYRIGHT_LINE; + const C_STYLE_COPYRIGHT_LINE = "// " + COPYRIGHT_LINE; + for (const file of sourceFilesSet) { const fileText = await readFirstPartOfFile(file); if (file.endsWith("Cargo.toml")) { if ( - !fileText.startsWith( - "# Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.", - ) + !fileText.startsWith(TOML_COPYRIGHT_LINE) ) { errors.push(ERROR_MSG + file); } continue; } - // use .includes(...) because the first line might be a shebang if ( - !fileText.includes( - "// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.", - ) + !fileText.startsWith(C_STYLE_COPYRIGHT_LINE) ) { - errors.push(ERROR_MSG + file); + let trimmedText = fileText; + // Attempt to trim accceptable lines + while ( + ACCEPTABLE_LINES.test(trimmedText) && + !trimmedText.startsWith(C_STYLE_COPYRIGHT_LINE) + ) { + trimmedText = trimmedText.split("\n").slice(1).join("\n"); + } + if ( + !trimmedText.startsWith(C_STYLE_COPYRIGHT_LINE) + ) { + errors.push( + `${ERROR_MSG}${file} (incorrect line is '${ + trimmedText.split("\n", 1) + }')`, + ); + } } } |