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

Add nightly-only error on ImportType resolution mode assertion #49356

Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6224,6 +6224,7 @@ namespace ts {
factory.createStringLiteral("import")
)
])));
context.tracker.reportImportTypeNodeResolutionModeOverride?.();
}
}
if (!specifier) {
Expand All @@ -6247,6 +6248,7 @@ namespace ts {
factory.createStringLiteral(swappedMode === ModuleKind.ESNext ? "import" : "require")
)
])));
context.tracker.reportImportTypeNodeResolutionModeOverride?.();
}
}

Expand Down Expand Up @@ -36029,8 +36031,11 @@ namespace ts {
if (node.assertions) {
const override = getResolutionModeOverrideForClause(node.assertions.assertClause, grammarErrorOnNode);
if (override) {
if (!isNightly()) {
grammarErrorOnNode(node.assertions.assertClause, Diagnostics.resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next);
}
if (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeNext) {
grammarErrorOnNode(node.assertions.assertClause, Diagnostics.Resolution_modes_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
grammarErrorOnNode(node.assertions.assertClause, Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
}
}
}
Expand Down Expand Up @@ -40961,11 +40966,11 @@ namespace ts {
const override = getResolutionModeOverrideForClause(declaration.assertClause, validForTypeAssertions ? grammarErrorOnNode : undefined);
if (validForTypeAssertions && override) {
if (!isNightly()) {
grammarErrorOnNode(declaration.assertClause, Diagnostics.Resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next);
grammarErrorOnNode(declaration.assertClause, Diagnostics.resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next);
}

if (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeNext) {
return grammarErrorOnNode(declaration.assertClause, Diagnostics.Resolution_modes_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
return grammarErrorOnNode(declaration.assertClause, Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext);
}
return; // Other grammar checks do not apply to type-only imports with resolution mode assertions
}
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@
"category": "Error",
"code": 1451
},
"Resolution modes are only supported when `moduleResolution` is `node16` or `nodenext`.": {
"'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.": {
"category": "Error",
"code": 1452
},
Expand Down Expand Up @@ -3479,6 +3479,10 @@
"category": "Error",
"code": 2840
},
"The type of this expression cannot be named without a 'resolution-mode' assertion, which is an unstable feature. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
"category": "Error",
"code": 2841
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down Expand Up @@ -3908,7 +3912,7 @@
"category": "Error",
"code": 4124
},
"Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
"'resolution-mode' assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
"category": "Error",
"code": 4125
},
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3121,7 +3121,7 @@ namespace ts {
setResolvedTypeReferenceDirective(file, fileName, resolvedTypeReferenceDirective);
const mode = ref.resolutionMode || file.impliedNodeFormat;
if (mode && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeNext) {
programDiagnostics.add(createDiagnosticForRange(file, ref, Diagnostics.Resolution_modes_are_only_supported_when_moduleResolution_is_node16_or_nodenext));
programDiagnostics.add(createDiagnosticForRange(file, ref, Diagnostics.resolution_mode_assertions_are_only_supported_when_moduleResolution_is_node16_or_nodenext));
}
processTypeReferenceDirective(fileName, mode, resolvedTypeReferenceDirective, { kind: FileIncludeKind.TypeReferenceDirective, file: file.path, index, });
}
Expand Down
11 changes: 9 additions & 2 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ namespace ts {
trackReferencedAmbientModule,
trackExternalModuleSymbolOfImportTypeNode,
reportNonlocalAugmentation,
reportNonSerializableProperty
reportNonSerializableProperty,
reportImportTypeNodeResolutionModeOverride,
};
let errorNameNode: DeclarationName | undefined;
let errorFallbackNode: Declaration | undefined;
Expand Down Expand Up @@ -235,6 +236,12 @@ namespace ts {
}
}

function reportImportTypeNodeResolutionModeOverride() {
if (!isNightly() && (errorNameNode || errorFallbackNode)) {
context.addDiagnostic(createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.The_type_of_this_expression_cannot_be_named_without_a_resolution_mode_assertion_which_is_an_unstable_feature_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next));
}
}

function transformDeclarationsForJS(sourceFile: SourceFile, bundled?: boolean) {
const oldDiag = getSymbolAccessibilityDiagnostic;
getSymbolAccessibilityDiagnostic = (s) => (s.errorNode && canProduceDiagnostics(s.errorNode) ? createGetSymbolAccessibilityDiagnosticForNode(s.errorNode)(s) : ({
Expand Down Expand Up @@ -792,7 +799,7 @@ namespace ts {
const mode = getResolutionModeOverrideForClause(assertClause);
if (mode !== undefined) {
if (!isNightly()) {
context.addDiagnostic(createDiagnosticForNode(assertClause!, Diagnostics.Resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next));
context.addDiagnostic(createDiagnosticForNode(assertClause!, Diagnostics.resolution_mode_assertions_are_unstable_Use_nightly_TypeScript_to_silence_this_error_Try_updating_with_npm_install_D_typescript_next));
}
return assertClause;
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8524,6 +8524,7 @@ namespace ts {
trackExternalModuleSymbolOfImportTypeNode?(symbol: Symbol): void;
reportNonlocalAugmentation?(containingFile: SourceFile, parentSymbol: Symbol, augmentingSymbol: Symbol): void;
reportNonSerializableProperty?(propertyName: string): void;
reportImportTypeNodeResolutionModeOverride?(): void;
}

export interface TextSpan {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/index.ts(1,23): error TS1452: Resolution modes are only supported when `moduleResolution` is `node16` or `nodenext`.
/index.ts(1,23): error TS1452: 'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.
/index.ts(1,23): error TS2688: Cannot find type definition file for 'pkg'.
/index.ts(2,23): error TS1452: Resolution modes are only supported when `moduleResolution` is `node16` or `nodenext`.
/index.ts(2,23): error TS1452: 'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.
/index.ts(2,23): error TS2688: Cannot find type definition file for 'pkg'.
/index.ts(3,1): error TS2304: Cannot find name 'foo'.
/index.ts(4,1): error TS2304: Cannot find name 'bar'.
Expand All @@ -9,12 +9,12 @@
==== /index.ts (6 errors) ====
/// <reference types="pkg" resolution-mode="require" />
~~~
!!! error TS1452: Resolution modes are only supported when `moduleResolution` is `node16` or `nodenext`.
!!! error TS1452: 'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.
~~~
!!! error TS2688: Cannot find type definition file for 'pkg'.
/// <reference types="pkg" resolution-mode="import" />
~~~
!!! error TS1452: Resolution modes are only supported when `moduleResolution` is `node16` or `nodenext`.
!!! error TS1452: 'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.
~~~
!!! error TS2688: Cannot find type definition file for 'pkg'.
foo; // `resolution-mode` is an error in old resolution settings, which resolves is arbitrary
Expand Down