Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark file as skips typechecking if it contains ts-nocheck #58593

Merged
merged 7 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
canHaveModifiers,
canHaveModuleSpecifier,
canHaveSymbol,
canIncludeBindAndCheckDiagnsotics,
canUsePropertyAccess,
cartesianProduct,
CaseBlock,
Expand Down Expand Up @@ -49029,7 +49030,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function calculateNodeCheckFlagWorker(node: Node, flag: LazyNodeCheckFlags) {
if (!compilerOptions.noCheck) {
if (!compilerOptions.noCheck || !canIncludeBindAndCheckDiagnsotics(getSourceFileOfNode(node), compilerOptions)) {
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
// Unless noCheck is passed, assume calculation of node check flags has been done eagerly.
// This saves needing to mark up where in the eager traversal certain results are "done",
// just to reconcile the eager and lazy results. This wouldn't be hard if an eager typecheck
Expand Down
34 changes: 22 additions & 12 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
CallExpression,
CallSignatureDeclaration,
canHaveLocals,
canIncludeBindAndCheckDiagnsotics,
CaseBlock,
CaseClause,
CaseOrDefaultClause,
Expand Down Expand Up @@ -800,9 +801,14 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
return;
}

if (compilerOptions.noCheck) {
(isSourceFile(sourceFileOrBundle) ? [sourceFileOrBundle] : filter(sourceFileOrBundle.sourceFiles, isSourceFileNotJson)).forEach(markLinkedReferences);
}
(isSourceFile(sourceFileOrBundle) ? [sourceFileOrBundle] : filter(sourceFileOrBundle.sourceFiles, isSourceFileNotJson)).forEach(
sourceFile => {
if (
compilerOptions.noCheck ||
!canIncludeBindAndCheckDiagnsotics(sourceFile, compilerOptions)
) markLinkedReferences(sourceFile);
},
);

// Transform the source files
const transform = transformNodes(resolver, host, factory, compilerOptions, [sourceFileOrBundle], scriptTransformers, /*allowDtsFiles*/ false);
Expand Down Expand Up @@ -859,15 +865,19 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
const filesForEmit = forceDtsEmit ? sourceFiles : filter(sourceFiles, isSourceFileNotJson);
// Setup and perform the transformation to retrieve declarations from the input files
const inputListOrBundle = compilerOptions.outFile ? [factory.createBundle(filesForEmit)] : filesForEmit;
if (
(emitOnly && !getEmitDeclarations(compilerOptions)) ||
compilerOptions.noCheck ||
emitResolverSkipsTypeChecking(emitOnly, forceDtsEmit)
) {
// Checker wont collect the linked aliases since thats only done when declaration is enabled and checking is performed.
// Do that here when emitting only dts files
filesForEmit.forEach(collectLinkedAliases);
}
// Checker wont collect the linked aliases since thats only done when declaration is enabled and checking is performed.
// Do that here when emitting only dts files
filesForEmit.forEach(sourceFile => {
if (
(emitOnly && !getEmitDeclarations(compilerOptions)) ||
compilerOptions.noCheck ||
emitResolverSkipsTypeChecking(emitOnly, forceDtsEmit) ||
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
!canIncludeBindAndCheckDiagnsotics(sourceFile, compilerOptions)
) {
collectLinkedAliases(sourceFile);
}
});

const declarationTransform = transformNodes(resolver, host, factory, compilerOptions, inputListOrBundle, declarationTransformers, /*allowDtsFiles*/ false);
if (length(declarationTransform.diagnostics)) {
for (const diagnostic of declarationTransform.diagnostics!) {
Expand Down
9 changes: 3 additions & 6 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3016,22 +3016,19 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
const isJs = sourceFile.scriptKind === ScriptKind.JS || sourceFile.scriptKind === ScriptKind.JSX;
const isCheckJs = isJs && isCheckJsEnabledForFile(sourceFile, options);
const isPlainJs = isPlainJsFile(sourceFile, options.checkJs);
const isTsNoCheck = !!sourceFile.checkJsDirective && sourceFile.checkJsDirective.enabled === false;

// By default, only type-check .ts, .tsx, Deferred, plain JS, checked JS and External
// - plain JS: .js files with no // ts-check and checkJs: undefined
// - check JS: .js files with either // ts-check or checkJs: true
// - external: files that are added by plugins
const includeBindAndCheckDiagnostics = !isTsNoCheck && (sourceFile.scriptKind === ScriptKind.TS || sourceFile.scriptKind === ScriptKind.TSX
|| sourceFile.scriptKind === ScriptKind.External || isPlainJs || isCheckJs || sourceFile.scriptKind === ScriptKind.Deferred);
let bindDiagnostics: readonly Diagnostic[] = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray;
let checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : emptyArray;
let bindDiagnostics = sourceFile.bindDiagnostics;
let checkDiagnostics = typeChecker.getDiagnostics(sourceFile, cancellationToken);
if (isPlainJs) {
bindDiagnostics = filter(bindDiagnostics, d => plainJSErrors.has(d.code));
checkDiagnostics = filter(checkDiagnostics, d => plainJSErrors.has(d.code));
}
// skip ts-expect-error errors in plain JS files, and skip JSDoc errors except in checked JS
return getMergedBindAndCheckDiagnostics(sourceFile, includeBindAndCheckDiagnostics && !isPlainJs, bindDiagnostics, checkDiagnostics, isCheckJs ? sourceFile.jsDocDiagnostics : undefined);
return getMergedBindAndCheckDiagnostics(sourceFile, !isPlainJs, bindDiagnostics, checkDiagnostics, isCheckJs ? sourceFile.jsDocDiagnostics : undefined);
});
}

Expand Down
23 changes: 22 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10082,7 +10082,28 @@ export function skipTypeChecking(sourceFile: SourceFile, options: CompilerOption
return (options.skipLibCheck && sourceFile.isDeclarationFile ||
options.skipDefaultLibCheck && sourceFile.hasNoDefaultLib) ||
options.noCheck ||
host.isSourceOfProjectReferenceRedirect(sourceFile.fileName);
host.isSourceOfProjectReferenceRedirect(sourceFile.fileName) ||
!canIncludeBindAndCheckDiagnsotics(sourceFile, options);
}

/** @internal */
export function canIncludeBindAndCheckDiagnsotics(sourceFile: SourceFile, options: CompilerOptions) {
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
if (!!sourceFile.checkJsDirective && sourceFile.checkJsDirective.enabled === false) return false;
if (
sourceFile.scriptKind === ScriptKind.TS ||
sourceFile.scriptKind === ScriptKind.TSX ||
sourceFile.scriptKind === ScriptKind.External
) return true;

const isJs = sourceFile.scriptKind === ScriptKind.JS || sourceFile.scriptKind === ScriptKind.JSX;
const isCheckJs = isJs && isCheckJsEnabledForFile(sourceFile, options);
const isPlainJs = isPlainJsFile(sourceFile, options.checkJs);

// By default, only type-check .ts, .tsx, Deferred, plain JS, checked JS and External
// - plain JS: .js files with no // ts-check and checkJs: undefined
// - check JS: .js files with either // ts-check or checkJs: true
// - external: files that are added by plugins
return isPlainJs || isCheckJs || sourceFile.scriptKind === ScriptKind.Deferred;
}

/** @internal */
Expand Down