Skip to content

Commit

Permalink
Flag resolution mode assertions in non-nightly builds (#49002)
Browse files Browse the repository at this point in the history
* Flag resolution mode assertions in non-nightly builds

* Add checker check
  • Loading branch information
RyanCavanaugh authored May 6, 2022
1 parent 8d0393d commit 1a4643b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
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) {
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

0 comments on commit 1a4643b

Please sign in to comment.