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 | |
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.
-rw-r--r-- | ops/op2/dispatch_slow.rs | 2 | ||||
-rw-r--r-- | serde_v8/magic/any_value.rs | 3 | ||||
-rw-r--r-- | test_util/src/factory.rs | 5 | ||||
-rw-r--r-- | tools/copyright_checker.js | 38 |
4 files changed, 32 insertions, 16 deletions
diff --git a/ops/op2/dispatch_slow.rs b/ops/op2/dispatch_slow.rs index f10217a2d..478f872cd 100644 --- a/ops/op2/dispatch_slow.rs +++ b/ops/op2/dispatch_slow.rs @@ -1,4 +1,3 @@ -use super::MacroConfig; // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use super::generator_state::GeneratorState; use super::signature::Arg; @@ -6,6 +5,7 @@ use super::signature::NumericArg; use super::signature::ParsedSignature; use super::signature::RetVal; use super::signature::Special; +use super::MacroConfig; use super::V8MappingError; use proc_macro2::TokenStream; use quote::quote; diff --git a/serde_v8/magic/any_value.rs b/serde_v8/magic/any_value.rs index 9ae39bb2e..df85f90d8 100644 --- a/serde_v8/magic/any_value.rs +++ b/serde_v8/magic/any_value.rs @@ -1,5 +1,3 @@ -use num_bigint::BigInt; - // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use super::buffer::JsBuffer; use super::transl8::FromV8; @@ -7,6 +5,7 @@ use super::transl8::ToV8; use crate::magic::transl8::impl_magic; use crate::Error; use crate::ToJsBuffer; +use num_bigint::BigInt; /// An untagged enum type that can be any of number, string, bool, bigint, or /// buffer. diff --git a/test_util/src/factory.rs b/test_util/src/factory.rs index f11d774d4..f29b64485 100644 --- a/test_util/src/factory.rs +++ b/test_util/src/factory.rs @@ -1,8 +1,7 @@ -use std::collections::HashSet; -use std::path::PathBuf; - // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use glob::glob; +use std::collections::HashSet; +use std::path::PathBuf; /// Generate a unit test factory verified and backed by a glob. #[macro_export] 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) + }')`, + ); + } } } |