From 9f0c5ce1418df7ffb14eb7fefca4910a436dd3d2 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Fri, 6 Jan 2017 19:03:17 -0800 Subject: [PATCH] Add support for `//@check` directives --- src/compiler/parser.ts | 5 +++++ src/compiler/program.ts | 3 ++- src/compiler/types.ts | 1 + src/services/services.ts | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 308d3a317f9..48fe3ce5426 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5817,6 +5817,7 @@ namespace ts { const typeReferenceDirectives: FileReference[] = []; const amdDependencies: { path: string; name: string }[] = []; let amdModuleName: string; + let hasCheckDirective = false; // Keep scanning all the leading trivia in the file until we get to something that // isn't trivia. Any single line comment will be analyzed to see if it is a @@ -5878,6 +5879,9 @@ namespace ts { amdDependencies.push(amdDependency); } } + + const checkDirectiveRegEx = /^\/\/\s*@check\s*/gim; + hasCheckDirective = hasCheckDirective || !!checkDirectiveRegEx.exec(comment); } } @@ -5885,6 +5889,7 @@ namespace ts { sourceFile.typeReferenceDirectives = typeReferenceDirectives; sourceFile.amdDependencies = amdDependencies; sourceFile.moduleName = amdModuleName; + sourceFile.hasCheckDirective = hasCheckDirective; } function setExternalModuleIndicator(sourceFile: SourceFile) { diff --git a/src/compiler/program.ts b/src/compiler/program.ts index ecef2377bae..64da4f6de71 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -907,7 +907,8 @@ namespace ts { // For JavaScript files, we don't want to report semantic errors. // Instead, we'll report errors for using TypeScript-only constructs from within a // JavaScript file when we get syntactic diagnostics for the file. - const checkDiagnostics = !options.checkJsFiles && isSourceFileJavaScript(sourceFile) ? [] : typeChecker.getDiagnostics(sourceFile, cancellationToken); + const includeCheckDiagnostics = options.checkJsFiles || sourceFile.hasCheckDirective || !isSourceFileJavaScript(sourceFile); + const checkDiagnostics = includeCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : []; const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName); const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 02d0a784b3d..3028b295d27 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2272,6 +2272,7 @@ /* @internal */ moduleAugmentations: LiteralExpression[]; /* @internal */ patternAmbientModules?: PatternAmbientModule[]; /* @internal */ ambientModuleNames: string[]; + /* @internal */ hasCheckDirective: boolean; } export interface Bundle extends Node { diff --git a/src/services/services.ts b/src/services/services.ts index f122e39bcc6..037894661ff 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -473,6 +473,7 @@ namespace ts { public moduleAugmentations: LiteralExpression[]; private namedDeclarations: Map; public ambientModuleNames: string[]; + public hasCheckDirective: boolean; constructor(kind: SyntaxKind, pos: number, end: number) { super(kind, pos, end);