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

Flag resolution mode assertions in non-nightly builds #49002

Merged
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
4 changes: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40718,6 +40718,10 @@ namespace ts {
const validForTypeAssertions = isExclusivelyTypeOnlyImportOrExport(declaration);
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);
}

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);
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3880,6 +3880,10 @@
"category": "Error",
"code": 4124
},
"Resolution mode assertions are unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.": {
"category": "Error",
"code": 4125
},

"The current host does not support the '{0}' option.": {
"category": "Error",
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ namespace ts {
}

/* @internal */
export function getResolutionModeOverrideForClause(clause: AssertClause | undefined, grammarErrorOnNode?: (node: Node, diagnostic: DiagnosticMessage) => boolean) {
export function getResolutionModeOverrideForClause(clause: AssertClause | undefined, grammarErrorOnNode?: (node: Node, diagnostic: DiagnosticMessage) => void) {
if (!clause) return undefined;
if (length(clause.elements) !== 1) {
grammarErrorOnNode?.(clause, Diagnostics.Type_import_assertions_should_have_exactly_one_key_resolution_mode_with_value_import_or_require);
Expand Down
21 changes: 16 additions & 5 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ namespace ts {
decl.modifiers,
decl.importClause,
rewriteModuleSpecifier(decl, decl.moduleSpecifier),
getResolutionModeOverrideForClause(decl.assertClause) ? decl.assertClause : undefined
getResolutionModeOverrideForClauseInNightly(decl.assertClause)
);
}
// The `importClause` visibility corresponds to the default's visibility.
Expand All @@ -745,7 +745,7 @@ namespace ts {
decl.importClause.isTypeOnly,
visibleDefaultBinding,
/*namedBindings*/ undefined,
), rewriteModuleSpecifier(decl, decl.moduleSpecifier), getResolutionModeOverrideForClause(decl.assertClause) ? decl.assertClause : undefined);
), rewriteModuleSpecifier(decl, decl.moduleSpecifier), getResolutionModeOverrideForClauseInNightly(decl.assertClause));
}
if (decl.importClause.namedBindings.kind === SyntaxKind.NamespaceImport) {
// Namespace import (optionally with visible default)
Expand All @@ -755,7 +755,7 @@ namespace ts {
decl.importClause.isTypeOnly,
visibleDefaultBinding,
namedBindings,
), rewriteModuleSpecifier(decl, decl.moduleSpecifier), getResolutionModeOverrideForClause(decl.assertClause) ? decl.assertClause : undefined) : undefined;
), rewriteModuleSpecifier(decl, decl.moduleSpecifier), getResolutionModeOverrideForClauseInNightly(decl.assertClause)) : undefined;
}
// Named imports (optionally with visible default)
const bindingList = mapDefined(decl.importClause.namedBindings.elements, b => resolver.isDeclarationVisible(b) ? b : undefined);
Expand All @@ -771,7 +771,7 @@ namespace ts {
bindingList && bindingList.length ? factory.updateNamedImports(decl.importClause.namedBindings, bindingList) : undefined,
),
rewriteModuleSpecifier(decl, decl.moduleSpecifier),
getResolutionModeOverrideForClause(decl.assertClause) ? decl.assertClause : undefined
getResolutionModeOverrideForClauseInNightly(decl.assertClause)
);
}
// Augmentation of export depends on import
Expand All @@ -782,12 +782,23 @@ namespace ts {
decl.modifiers,
/*importClause*/ undefined,
rewriteModuleSpecifier(decl, decl.moduleSpecifier),
getResolutionModeOverrideForClause(decl.assertClause) ? decl.assertClause : undefined
getResolutionModeOverrideForClauseInNightly(decl.assertClause)
);
}
// Nothing visible
}

function getResolutionModeOverrideForClauseInNightly(assertClause: AssertClause | undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be getting called in the checker? Seems like you'll only get an error in .d.ts generation scenarios

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));
}
return assertClause;
}
return undefined;
}

function transformAndReplaceLatePaintedStatements(statements: NodeArray<Statement>): NodeArray<Statement> {
// This is a `while` loop because `handleSymbolAccessibilityError` can see additional import aliases marked as visible during
// error handling which must now be included in the output and themselves checked for errors.
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4121,6 +4121,10 @@ namespace ts {
return indentStrings[1].length;
}

export function isNightly() {
return stringContains(version, "-dev") || stringContains(version, "-insiders");
}

export function createTextWriter(newLine: string): EmitTextWriter {
let output: string;
let indent: number;
Expand Down