mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Add new option "noUncheckedSideEffectImports" (#58941)
This commit is contained in:
+10
-1
@@ -720,6 +720,7 @@ import {
|
||||
isSetAccessorDeclaration,
|
||||
isShorthandAmbientModuleSymbol,
|
||||
isShorthandPropertyAssignment,
|
||||
isSideEffectImport,
|
||||
isSingleOrDoubleQuote,
|
||||
isSourceFile,
|
||||
isSourceFileJS,
|
||||
@@ -1505,6 +1506,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
var noImplicitThis = getStrictOptionValue(compilerOptions, "noImplicitThis");
|
||||
var useUnknownInCatchVariables = getStrictOptionValue(compilerOptions, "useUnknownInCatchVariables");
|
||||
var exactOptionalPropertyTypes = compilerOptions.exactOptionalPropertyTypes;
|
||||
var noUncheckedSideEffectImports = !!compilerOptions.noUncheckedSideEffectImports;
|
||||
|
||||
var checkBinaryExpression = createCheckBinaryExpression();
|
||||
var emitResolver = createResolver();
|
||||
@@ -4660,7 +4662,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
// merged symbol is module declaration symbol combined with all augmentations
|
||||
return getMergedSymbol(sourceFile.symbol);
|
||||
}
|
||||
if (errorNode && moduleNotFoundError) {
|
||||
if (errorNode && moduleNotFoundError && !isSideEffectImport(errorNode)) {
|
||||
// report errors only if it was requested
|
||||
error(errorNode, Diagnostics.File_0_is_not_a_module, sourceFile.fileName);
|
||||
}
|
||||
@@ -4765,6 +4767,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
|
||||
function errorOnImplicitAnyModule(isError: boolean, errorNode: Node, sourceFile: SourceFile, mode: ResolutionMode, { packageId, resolvedFileName }: ResolvedModuleFull, moduleReference: string): void {
|
||||
if (isSideEffectImport(errorNode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let errorInfo: DiagnosticMessageChain | undefined;
|
||||
if (!isExternalModuleNameRelative(moduleReference) && packageId) {
|
||||
errorInfo = createModuleNotFoundChain(sourceFile, host, moduleReference, mode, packageId.name);
|
||||
@@ -47246,6 +47252,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (noUncheckedSideEffectImports && !importClause) {
|
||||
void resolveExternalModuleName(node, node.moduleSpecifier);
|
||||
}
|
||||
}
|
||||
checkImportAttributes(node);
|
||||
}
|
||||
|
||||
@@ -1202,6 +1202,15 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
|
||||
category: Diagnostics.Modules,
|
||||
description: Diagnostics.Conditions_to_set_in_addition_to_the_resolver_specific_defaults_when_resolving_imports,
|
||||
},
|
||||
{
|
||||
name: "noUncheckedSideEffectImports",
|
||||
type: "boolean",
|
||||
affectsSemanticDiagnostics: true,
|
||||
affectsBuildInfo: true,
|
||||
category: Diagnostics.Modules,
|
||||
description: Diagnostics.Check_side_effect_imports,
|
||||
defaultValueDescription: false,
|
||||
},
|
||||
|
||||
// Source Maps
|
||||
{
|
||||
|
||||
@@ -6388,6 +6388,10 @@
|
||||
"category": "Message",
|
||||
"code": 6805
|
||||
},
|
||||
"Check side effect imports.": {
|
||||
"category": "Message",
|
||||
"code": 6806
|
||||
},
|
||||
|
||||
"one of:": {
|
||||
"category": "Message",
|
||||
|
||||
@@ -7418,6 +7418,7 @@ export interface CompilerOptions {
|
||||
target?: ScriptTarget;
|
||||
traceResolution?: boolean;
|
||||
useUnknownInCatchVariables?: boolean;
|
||||
noUncheckedSideEffectImports?: boolean;
|
||||
resolveJsonModule?: boolean;
|
||||
types?: string[];
|
||||
/** Paths used to compute primary types search locations */
|
||||
|
||||
@@ -287,6 +287,7 @@ import {
|
||||
isIdentifier,
|
||||
isIdentifierStart,
|
||||
isIdentifierText,
|
||||
isImportDeclaration,
|
||||
isImportTypeNode,
|
||||
isInterfaceDeclaration,
|
||||
isJSDoc,
|
||||
@@ -11758,3 +11759,9 @@ export function hasInferredType(node: Node): node is HasInferredType {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function isSideEffectImport(node: Node): boolean {
|
||||
const ancestor = findAncestor(node, isImportDeclaration);
|
||||
return !!ancestor && !ancestor.importClause;
|
||||
}
|
||||
|
||||
@@ -2245,4 +2245,40 @@ import { x } from "../b";`,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
verifyTscWatch({
|
||||
scenario,
|
||||
subScenario: "when changing noUncheckedSideEffectImports of config file",
|
||||
commandLineArgs: ["-w", "-p", ".", "--extendedDiagnostics"],
|
||||
sys: () => {
|
||||
const module1: File = {
|
||||
path: `/user/username/projects/myproject/a.ts`,
|
||||
content: `import "does-not-exist";`,
|
||||
};
|
||||
const config: File = {
|
||||
path: `/user/username/projects/myproject/tsconfig.json`,
|
||||
content: jsonToReadableText({
|
||||
compilerOptions: {
|
||||
noUncheckedSideEffectImports: false,
|
||||
},
|
||||
}),
|
||||
};
|
||||
return createWatchedSystem([module1, config, libFile], { currentDirectory: "/user/username/projects/myproject" });
|
||||
},
|
||||
edits: [
|
||||
{
|
||||
caption: "Change noUncheckedSideEffectImports to true",
|
||||
edit: sys =>
|
||||
sys.writeFile(
|
||||
`/user/username/projects/myproject/tsconfig.json`,
|
||||
jsonToReadableText({
|
||||
compilerOptions: {
|
||||
noUncheckedSideEffectImports: true,
|
||||
},
|
||||
}),
|
||||
),
|
||||
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user