Skip to content

Commit

Permalink
Add ignore directive for boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Feb 11, 2025
1 parent 5f255df commit 8d46bba
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
38 changes: 37 additions & 1 deletion crates/turborepo-lib/src/boundaries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use globwalk::Settings;
use miette::{Diagnostic, NamedSource, Report, SourceSpan};
use regex::Regex;
use swc_common::{
comments::SingleThreadedComments, errors::Handler, input::StringInput, FileName, SourceMap,
comments::{Comments, SingleThreadedComments},
errors::Handler,
input::StringInput,
FileName, SourceMap, Span,
};
use swc_ecma_ast::EsVersion;
use swc_ecma_parser::{lexer::Lexer, Capturing, EsSyntax, Parser, Syntax, TsSyntax};
Expand Down Expand Up @@ -133,6 +136,9 @@ static PACKAGE_NAME_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$").unwrap()
});

/// Maximum number of ignored import warnings to show
const MAX_IGNORE_WARNINGS: usize = 10;

pub struct BoundariesResult {
files_checked: usize,
packages_checked: usize,
Expand Down Expand Up @@ -223,6 +229,18 @@ impl Run {
})
}

fn is_ignored_import(comments: &SingleThreadedComments, span: Span) -> bool {
if let Some(import_comments) = comments.get_leading(span.lo()) {
for comment in import_comments {
if comment.text.trim() == "@boundaries-ignore" {
return true;
}
}
}

false
}

/// Either returns a list of errors and number of files checked or a single,
/// fatal error
async fn check_package(
Expand Down Expand Up @@ -363,7 +381,25 @@ impl Run {
// Visit the AST and find imports
let mut finder = ImportFinder::default();
module.visit_with(&mut finder);
let mut warning_count = 0;
for (import, span, import_type) in finder.imports() {
// If the import is prefixed with `@boundaries-ignore`, we ignore it, but print
// a warning
if Self::is_ignored_import(&comments, *span) {
if warning_count <= MAX_IGNORE_WARNINGS {
// Try to get the line number for warning
let line = source_map.lookup_line(span.lo()).map(|l| l.line);
if let Ok(line) = line {
warn!("ignoring import on line {} in {}", line, file_path);
} else {
warn!("ignoring import in {}", file_path);
}
}
warning_count += 1;

continue;
}

let (start, end) = source_map.span_to_char_offset(&source_file, *span);
let start = start as usize;
let end = end as usize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@ import { Ship } from "ship";
import { Ship } from "@types/ship";
// Import package that is not specified
import { walkThePlank } from "module-package";

// Import from a package that is not specified, but we have `@boundaries-ignore` on it
// @boundaries-ignore
import { walkThePlank } from "module-package";

// Import also works with other ignore directives
// @boundaries-ignore
// @ts-ignore
import { walkThePlank } from "module-package";

// import also works with whitespace
// @boundaries-ignore
import { walkThePlank } from "module-package";

// @boundaries-ignore

import { walkThePlank } from "module-package";

0 comments on commit 8d46bba

Please sign in to comment.