Skip to content

Commit 6fe01e6

Browse files
josephperrottalan-agius4
authored andcommitted
feat(bazel): allow ignoring strict deps using @ts-ignore comment
1 parent b281f43 commit 6fe01e6

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

bazel/ts_project/strict_deps/index.mts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ function checkPathsForMatch(moduleSpecifier: string, paths?: ts.MapLike<string[]
5656
const knownModuleSpecifiersWithExtensions = new Set(['highlight.js', 'zone.js']);
5757

5858
for (const fileExecPath of manifest.testFiles) {
59-
const content = await fs.readFile(fileExecPath, 'utf8');
60-
const sf = ts.createSourceFile(fileExecPath, content, ts.ScriptTarget.ESNext, true);
61-
const imports = getImportsInSourceFile(sf);
59+
const imports = getImportsInSourceFile(fileExecPath);
6260

6361
for (const i of imports) {
6462
const moduleSpecifier = knownModuleSpecifiersWithExtensions.has(i.moduleSpecifier)

bazel/ts_project/strict_deps/visitor.mts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,28 @@
77
*/
88

99
import ts from 'typescript';
10+
import fs from 'node:fs';
1011

1112
export interface Import {
1213
diagnosticNode: ts.Node;
1314
moduleSpecifier: string;
1415
}
1516

16-
export function getImportsInSourceFile(sf: ts.SourceFile): Import[] {
17+
export function getImportsInSourceFile(fileExecPath: string): Import[] {
18+
const content = fs.readFileSync(fileExecPath, 'utf8');
19+
const sf = ts.createSourceFile(fileExecPath, content, ts.ScriptTarget.ESNext, true);
1720
const result: Import[] = [];
1821

1922
const visitor = (node: ts.Node) => {
2023
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
24+
const leadingComments = ts.getLeadingCommentRanges(content, node.pos) || [];
25+
for (const comment of leadingComments) {
26+
const commentText = content.substring(comment.pos, comment.end);
27+
if (/@ts-ignore.*strict-deps/.test(commentText)) {
28+
return;
29+
}
30+
}
31+
2132
const moduleSpecifier = node.moduleSpecifier as ts.StringLiteral;
2233
// If not moduleSpecifier is included in the declaration, it is infered to be the local file,
2334
// essentially a self import and can be ignored.

0 commit comments

Comments
 (0)