Skip to content

Commit d3dde7e

Browse files
committed
Add a fixer to fix reference directive errors
also, reword the error messages to be explicit on which directives to add. Signed-off-by: Hana Joo <hanajoo@google.com>
1 parent c5a4d89 commit d3dde7e

32 files changed

Lines changed: 127 additions & 82 deletions

File tree

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7227,8 +7227,11 @@
72277227
"Annotate types of properties expando function in a namespace": {
72287228
"category": "Message",
72297229
"code": 90071
7230-
},
7231-
7230+
},
7231+
"Add triple slash directives for import resolution": {
7232+
"category": "Message",
7233+
"code": 90072
7234+
},
72327235
"Convert function to an ES2015 class": {
72337236
"category": "Message",
72347237
"code": 95001

src/compiler/transformers/declarations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ export function transformDeclarations(context: TransformationContext) {
722722
if (!isPresentInSource && isolatedDeclarations) {
723723
context.addDiagnostic(createDiagnosticForNode(
724724
requestingNode,
725-
Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations,
726-
specifier,
725+
Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_path_reference_directive_to_0_with_isolatedDeclarations,
726+
fileName,
727727
));
728728
}
729729
references.push({ pos: -1, end: -1, fileName });

src/harness/isolatedDeclarationFixer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as vfs from "./_namespaces/vfs";
44

55
export const isolatedDeclarationsErrors = new Set([
66
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations,
7+
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_path_reference_directive_to_0_with_isolatedDeclarations,
78
ts.Diagnostics.Assigning_properties_to_functions_without_declaring_them_is_not_supported_with_isolatedDeclarations_Add_an_explicit_declaration_for_the_properties_assigned_to_this_function,
89
ts.Diagnostics.Declaration_emit_for_this_parameter_requires_implicitly_adding_undefined_to_it_s_type_This_is_not_supported_with_isolatedDeclarations,
910
ts.Diagnostics.Function_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations,
@@ -98,6 +99,8 @@ export function fixProjectInternal(
9899

99100
const fixAll = service.getCombinedCodeFix({ type: "file", fileName: file.fileName }, "fixMissingTypeAnnotationOnExports", defaultFormatOptions, userPreferences);
100101
applyFix(fixAll.changes);
102+
const fixAll2 = service.getCombinedCodeFix({ type: "file", fileName: file.fileName }, "fixMissingReferenceDirectivesForIsolatedDeclarations", defaultFormatOptions, userPreferences);
103+
applyFix(fixAll2.changes);
101104

102105
// Some fixes need to be applied individually such as fixing `export =`
103106
diagnostics = getIsolatedDeclarationsErrors(file.fileName);

src/services/_namespaces/ts.codefix.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export * from "../codefixes/fixUnreachableCode";
4949
export * from "../codefixes/fixUnusedLabel";
5050
export * from "../codefixes/fixJSDocTypes";
5151
export * from "../codefixes/fixMissingCallParentheses";
52+
export * from "../codefixes/fixMissingReferenceDirectivesForIsolatedDeclarations";
5253
export * from "../codefixes/fixMissingTypeAnnotationOnExports";
5354
export * from "../codefixes/fixAwaitInSyncFunction";
5455
export * from "../codefixes/fixPropertyOverrideAccessor";
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {CodeFixContextBase, Diagnostics, DiagnosticWithLocation, textChanges} from "../_namespaces/ts";
2+
import {codeFixAll, createCodeFixAction, registerCodeFix} from "../_namespaces/ts.codefix";
3+
import {ChangeTracker} from "../textChanges";
4+
5+
6+
const fixId = "fixMissingReferenceDirectivesForIsolatedDeclarations";
7+
const errorCodes = [
8+
Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations,
9+
Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_path_reference_directive_to_0_with_isolatedDeclarations
10+
].map((diag) => diag.code);
11+
12+
registerCodeFix({
13+
errorCodes,
14+
fixIds: [fixId],
15+
getCodeActions(context) {
16+
const {span, sourceFile, program} = context;
17+
const diags = program.getDeclarationDiagnostics(sourceFile);
18+
const diagOnNode = diags.find((diag) => diag.start === span.start);
19+
if (diagOnNode) {
20+
const changes = textChanges.ChangeTracker.with(context, (changeTracker) => fixDiag(context, diagOnNode, changeTracker));
21+
return [
22+
createCodeFixAction(
23+
fixId,
24+
changes,
25+
Diagnostics.Add_triple_slash_directives_for_import_resolution,
26+
fixId,
27+
// TODO: ADD
28+
Diagnostics.Add_triple_slash_directives_for_import_resolution)];
29+
}
30+
},
31+
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
32+
fixDiag(context, diag, changes)
33+
}),
34+
});
35+
36+
function fixDiag(context: CodeFixContextBase, diag: DiagnosticWithLocation, changeTracker: ChangeTracker) {
37+
const messageText = diag.messageText as string;
38+
const first = messageText.indexOf("'");
39+
const last = messageText.lastIndexOf("'");
40+
const tripleSlashType = diag.code === Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations.code ? 'types' : 'path'
41+
changeTracker.insertText(context.sourceFile, 0, `/// <reference ${tripleSlashType}='${messageText.substring(first + 1, last)}' />\n`);
42+
}

src/testRunner/compilerRunner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ class IsolatedDeclarationTest extends CompilerTestBase {
599599
ts.Diagnostics.Enum_member_initializers_must_be_computable_without_references_to_external_symbols_with_isolatedDeclarations,
600600
ts.Diagnostics.Extends_clause_can_t_contain_an_expression_with_isolatedDeclarations,
601601
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations,
602+
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_path_reference_directive_to_0_with_isolatedDeclarations,
602603
ts.Diagnostics.Assigning_properties_to_functions_without_declaring_them_is_not_supported_with_isolatedDeclarations_Add_an_explicit_declaration_for_the_properties_assigned_to_this_function,
603604
ts.Diagnostics.Objects_that_contain_shorthand_properties_can_t_be_inferred_with_isolatedDeclarations,
604605
ts.Diagnostics.Objects_that_contain_spread_assignments_can_t_be_inferred_with_isolatedDeclarations,
@@ -747,6 +748,7 @@ export class FixedIsolatedDeclarationTest extends IsolatedDeclarationTest {
747748
}
748749
private static referenceDirectiveErrors = new Set([
749750
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations.code,
751+
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_path_reference_directive_to_0_with_isolatedDeclarations.code,
750752
]);
751753

752754
protected override get baselinePath() {

tests/baselines/reference/isolated-declarations/original/diff/declarationEmitBundleWithAmbientReferences.d.ts.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-
1717
-/// [Errors] ////
1818
-
19-
-src/datastore_result.ts(1,1): error TS9024: Declaration emit for this expression requires adding a type reference directive to './lib/lib' with --isolatedDeclarations.
19+
-src/datastore_result.ts(1,1): error TS9025: Declaration emit for this expression requires adding a path reference directive to '../lib/lib.d.ts' with --isolatedDeclarations.
2020
-
2121
-
2222
-==== lib/lib.d.ts (0 errors) ====
@@ -29,7 +29,7 @@
2929
-==== src/datastore_result.ts (1 errors) ====
3030
- import { Result } from "lib/result";
3131
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32-
-!!! error TS9024: Declaration emit for this expression requires adding a type reference directive to './lib/lib' with --isolatedDeclarations.
32+
-!!! error TS9025: Declaration emit for this expression requires adding a path reference directive to '../lib/lib.d.ts' with --isolatedDeclarations.
3333
-
3434
- export type T<T> = Result<Error, T>;
3535
-

tests/baselines/reference/isolated-declarations/original/diff/moduleAugmentationsImports2.d.ts.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
-
2424
-/// [Errors] ////
2525
-
26-
-e.ts(2,1): error TS9024: Declaration emit for this expression requires adding a type reference directive to '.c' with --isolatedDeclarations.
26+
-e.ts(2,1): error TS9025: Declaration emit for this expression requires adding a path reference directive to 'c.d.ts' with --isolatedDeclarations.
2727
-
2828
-
2929
-==== a.ts (0 errors) ====
@@ -55,7 +55,7 @@
5555
- import {A} from "./a";
5656
- import {Cls} from "C";
5757
- ~~~~~~~~~~~~~~~~~~~~~~
58-
-!!! error TS9024: Declaration emit for this expression requires adding a type reference directive to '.c' with --isolatedDeclarations.
58+
-!!! error TS9025: Declaration emit for this expression requires adding a path reference directive to 'c.d.ts' with --isolatedDeclarations.
5959
-
6060
- A.prototype.getCls = function () { return undefined; }
6161
-

tests/baselines/reference/isolated-declarations/original/diff/nodeModulesAllowJsImportHelpersCollisions2(module=node16).d.ts.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
The file is in the program because:
2020
Root file specified for compilation
2121
subfolder/index.ts(2,1): error TS2343: This syntax requires an imported helper named '__exportStar' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
22-
-subfolder/index.ts(2,1): error TS9024: Declaration emit for this expression requires adding a type reference directive to './types' with --isolatedDeclarations.
22+
-subfolder/index.ts(2,1): error TS9025: Declaration emit for this expression requires adding a path reference directive to '../types.d.ts' with --isolatedDeclarations.
2323
subfolder/index.ts(3,1): error TS2343: This syntax requires an imported helper named '__importStar' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
2424

2525

@@ -33,7 +33,7 @@
3333
~~~~~~~~~~~~~~~~~~~
3434
!!! error TS2343: This syntax requires an imported helper named '__exportStar' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
3535
- ~~~~~~~~~~~~~~~~~~~
36-
-!!! error TS9024: Declaration emit for this expression requires adding a type reference directive to './types' with --isolatedDeclarations.
36+
-!!! error TS9025: Declaration emit for this expression requires adding a path reference directive to '../types.d.ts' with --isolatedDeclarations.
3737
export * as fs from "fs";
3838
~~~~~~~~~~~~~~~~~~~~~~~~~
3939
!!! error TS2343: This syntax requires an imported helper named '__importStar' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.

tests/baselines/reference/isolated-declarations/original/diff/nodeModulesAllowJsImportHelpersCollisions2(module=nodenext).d.ts.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
The file is in the program because:
2020
Root file specified for compilation
2121
subfolder/index.ts(2,1): error TS2343: This syntax requires an imported helper named '__exportStar' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
22-
-subfolder/index.ts(2,1): error TS9024: Declaration emit for this expression requires adding a type reference directive to './types' with --isolatedDeclarations.
22+
-subfolder/index.ts(2,1): error TS9025: Declaration emit for this expression requires adding a path reference directive to '../types.d.ts' with --isolatedDeclarations.
2323
subfolder/index.ts(3,1): error TS2343: This syntax requires an imported helper named '__importStar' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
2424

2525

@@ -33,7 +33,7 @@
3333
~~~~~~~~~~~~~~~~~~~
3434
!!! error TS2343: This syntax requires an imported helper named '__exportStar' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
3535
- ~~~~~~~~~~~~~~~~~~~
36-
-!!! error TS9024: Declaration emit for this expression requires adding a type reference directive to './types' with --isolatedDeclarations.
36+
-!!! error TS9025: Declaration emit for this expression requires adding a path reference directive to '../types.d.ts' with --isolatedDeclarations.
3737
export * as fs from "fs";
3838
~~~~~~~~~~~~~~~~~~~~~~~~~
3939
!!! error TS2343: This syntax requires an imported helper named '__importStar' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.

0 commit comments

Comments
 (0)