Skip to content

Commit

Permalink
chore: Ensure copyright line is the first in the file (denoland#19608)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mmastrac committed Jun 26, 2023
1 parent 801b9ec commit fa935e5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ops/op2/dispatch_slow.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::MacroConfig;
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use super::generator_state::GeneratorState;
use super::signature::Arg;
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;
Expand Down
3 changes: 1 addition & 2 deletions serde_v8/magic/any_value.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use num_bigint::BigInt;

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use super::buffer::JsBuffer;
use super::transl8::FromV8;
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.
Expand Down
5 changes: 2 additions & 3 deletions test_util/src/factory.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
38 changes: 28 additions & 10 deletions tools/copyright_checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}')`,
);
}
}
}

Expand Down

0 comments on commit fa935e5

Please sign in to comment.