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 a fixer for reference directives. #141

Open
wants to merge 4 commits into
base: isolated-declarations-unity
Choose a base branch
from
Open
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
37 changes: 22 additions & 15 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6818,58 +6818,62 @@
"category": "Error",
"code": 9024
},
"Declaration emit for this parameter requires implicitly adding undefined to it's type. This is not supported with --isolatedDeclarations.": {
"Declaration emit for this expression requires adding a path reference directive to '{0}' with --isolatedDeclarations.": {
"category": "Error",
"code": 9025
},
"Declaration emit for this file requires preserving this import for augmentations. This is not supported with --isolatedDeclarations.": {
"Declaration emit for this parameter requires implicitly adding undefined to it's type. This is not supported with --isolatedDeclarations.": {
"category": "Error",
"code": 9026
},
"Add a type annotation to the variable {0}.": {
"Declaration emit for this file requires preserving this import for augmentations. This is not supported with --isolatedDeclarations.": {
"category": "Error",
"code": 9027
},
"Add a type annotation to the parameter {0}.": {
"Add a type annotation to the variable {0}.": {
"category": "Error",
"code": 9028
},
"Add a type annotation to the property {0}.": {
"Add a type annotation to the parameter {0}.": {
"category": "Error",
"code": 9029
},
"Add a return type to the function expression.": {
"Add a type annotation to the property {0}.": {
"category": "Error",
"code": 9030
},
"Add a return type to the function declaration.": {
"Add a return type to the function expression.": {
"category": "Error",
"code": 9031
},
"Add a return type to the get accessor declaration.": {
"Add a return type to the function declaration.": {
"category": "Error",
"code": 9032
},
"Add a type to parameter of the set accessor declaration.": {
"Add a return type to the get accessor declaration.": {
"category": "Error",
"code": 9033
},
"Add a return type to the method": {
"Add a type to parameter of the set accessor declaration.": {
"category": "Error",
"code": 9034
},
"Add a type assertion to this expression to make type type explicit.": {
"Add a return type to the method": {
"category": "Error",
"code": 9035
},
"Move the expression in default export to a variable and add a type annotation to it.": {
"Add a type assertion to this expression to make type type explicit.": {
"category": "Error",
"code": 9036
},
"Default exports can't be inferred with --isolatedDeclarations.": {
"Move the expression in default export to a variable and add a type annotation to it.": {
"category": "Error",
"code": 9037
},
"Default exports can't be inferred with --isolatedDeclarations.": {
"category": "Error",
"code": 9038
},
"JSX attributes must only be assigned a non-empty 'expression'.": {
"category": "Error",
"code": 17000
Expand Down Expand Up @@ -7223,8 +7227,11 @@
"Annotate types of properties expando function in a namespace": {
"category": "Message",
"code": 90071
},

},
"Add triple slash directives for import resolution": {
"category": "Message",
"code": 90072
},
"Convert function to an ES2015 class": {
"category": "Message",
"code": 95001
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,8 @@ export function transformDeclarations(context: TransformationContext) {
if (!isPresentInSource && isolatedDeclarations) {
context.addDiagnostic(createDiagnosticForNode(
requestingNode,
Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations,
specifier,
Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_path_reference_directive_to_0_with_isolatedDeclarations,
fileName,
));
}
references.push({ pos: -1, end: -1, fileName });
Expand Down
3 changes: 3 additions & 0 deletions src/harness/isolatedDeclarationFixer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as vfs from "./_namespaces/vfs";

export const isolatedDeclarationsErrors = new Set([
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations,
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_path_reference_directive_to_0_with_isolatedDeclarations,
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,
ts.Diagnostics.Declaration_emit_for_this_parameter_requires_implicitly_adding_undefined_to_it_s_type_This_is_not_supported_with_isolatedDeclarations,
ts.Diagnostics.Function_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations,
Expand Down Expand Up @@ -98,6 +99,8 @@ export function fixProjectInternal(

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

// Some fixes need to be applied individually such as fixing `export =`
diagnostics = getIsolatedDeclarationsErrors(file.fileName);
Expand Down
1 change: 1 addition & 0 deletions src/services/_namespaces/ts.codefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export * from "../codefixes/fixUnreachableCode";
export * from "../codefixes/fixUnusedLabel";
export * from "../codefixes/fixJSDocTypes";
export * from "../codefixes/fixMissingCallParentheses";
export * from "../codefixes/fixMissingReferenceDirectivesForIsolatedDeclarations";
export * from "../codefixes/fixMissingTypeAnnotationOnExports";
export * from "../codefixes/fixAwaitInSyncFunction";
export * from "../codefixes/fixPropertyOverrideAccessor";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {CodeFixContextBase, Diagnostics, DiagnosticWithLocation, textChanges} from "../_namespaces/ts";
import {codeFixAll, createCodeFixAction, registerCodeFix} from "../_namespaces/ts.codefix";
import {ChangeTracker} from "../textChanges";


const fixId = "fixMissingReferenceDirectivesForIsolatedDeclarations";
const errorCodes = [
Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations,
Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_path_reference_directive_to_0_with_isolatedDeclarations
].map((diag) => diag.code);

registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions(context) {
const {span, sourceFile, program} = context;
const diags = program.getDeclarationDiagnostics(sourceFile);
const diagOnNode = diags.find((diag) => diag.start === span.start);
if (diagOnNode) {
const changes = textChanges.ChangeTracker.with(context, (changeTracker) => fixDiag(context, diagOnNode, changeTracker));
return [
createCodeFixAction(
fixId,
changes,
Diagnostics.Add_triple_slash_directives_for_import_resolution,
fixId,
// TODO: ADD
Diagnostics.Add_triple_slash_directives_for_import_resolution)];
}
},
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
fixDiag(context, diag, changes)
}),
});

function fixDiag(context: CodeFixContextBase, diag: DiagnosticWithLocation, changeTracker: ChangeTracker) {
const messageText = diag.messageText as string;
const first = messageText.indexOf("'");
const last = messageText.lastIndexOf("'");
const tripleSlashType = diag.code === Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations.code ? 'types' : 'path'
changeTracker.insertText(context.sourceFile, 0, `/// <reference ${tripleSlashType}='${messageText.substring(first + 1, last)}' />\n`);
}
7 changes: 2 additions & 5 deletions src/testRunner/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ class IsolatedDeclarationTest extends CompilerTestBase {
ts.Diagnostics.Enum_member_initializers_must_be_computable_without_references_to_external_symbols_with_isolatedDeclarations,
ts.Diagnostics.Extends_clause_can_t_contain_an_expression_with_isolatedDeclarations,
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations,
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_path_reference_directive_to_0_with_isolatedDeclarations,
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,
ts.Diagnostics.Objects_that_contain_shorthand_properties_can_t_be_inferred_with_isolatedDeclarations,
ts.Diagnostics.Objects_that_contain_spread_assignments_can_t_be_inferred_with_isolatedDeclarations,
Expand Down Expand Up @@ -732,22 +733,18 @@ export class FixedIsolatedDeclarationTest extends IsolatedDeclarationTest {
fixerOptions.isolatedDeclarations = true;
const fixResults = fixTestFiles(env.fileSystem, env.programFileNames, fixerOptions);

const hasReferenceDirectiveErrors = fixResults.success && fixResults.unfixedDiagnostics.some(d => FixedIsolatedDeclarationTest.referenceDirectiveErrors.has(d.code));
for (const file of env.allFiles) {
const content = env.fileSystem.readFileSync(file.unitName, "utf-8");
file.content = content;
}

if (!fixResults.success || hasReferenceDirectiveErrors) {
if (!fixResults.success) {
return undefined;
}
env.fileSystem.makeReadonly();
env.fileSystem = env.fileSystem.shadow();
return env;
}
private static referenceDirectiveErrors = new Set([
ts.Diagnostics.Declaration_emit_for_this_expression_requires_adding_a_type_reference_directive_to_0_with_isolatedDeclarations.code,
]);

protected override get baselinePath() {
return "isolated-declarations/auto-fixed";
Expand Down
14 changes: 7 additions & 7 deletions tests/baselines/reference/computedPropertiesNarrowed.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ computedPropertiesNarrowed.ts(47,5): error TS9014: Computed properties must be n
[x]: 1 // error narrow type !== declared type
~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:4:12: Add a type annotation to the variable o.
!!! related TS9028 computedPropertiesNarrowed.ts:4:12: Add a type annotation to the variable o.
}


Expand All @@ -31,24 +31,24 @@ computedPropertiesNarrowed.ts(47,5): error TS9014: Computed properties must be n
export let o32 = { [1-1]: 1 } // error number
~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:18:12: Add a type annotation to the variable o32.
!!! related TS9028 computedPropertiesNarrowed.ts:18:12: Add a type annotation to the variable o32.

let u = Symbol();
~
!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:20:5: Add a type annotation to the variable u.
!!! related TS9028 computedPropertiesNarrowed.ts:20:5: Add a type annotation to the variable u.
export let o4 = {
[u]: 1 // Should error, nut a unique symbol
~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:21:12: Add a type annotation to the variable o4.
!!! related TS9028 computedPropertiesNarrowed.ts:21:12: Add a type annotation to the variable o4.
}

export let o5 ={
[Symbol()]: 1 // Should error
~~~~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:25:12: Add a type annotation to the variable o5.
!!! related TS9028 computedPropertiesNarrowed.ts:25:12: Add a type annotation to the variable o5.
}

const uu: unique symbol = Symbol();
Expand All @@ -62,7 +62,7 @@ computedPropertiesNarrowed.ts(47,5): error TS9014: Computed properties must be n
[foo()]: 1 // Should error
~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:36:12: Add a type annotation to the variable o7.
!!! related TS9028 computedPropertiesNarrowed.ts:36:12: Add a type annotation to the variable o7.
};

let E = { A: 1 } as const
Expand All @@ -75,6 +75,6 @@ computedPropertiesNarrowed.ts(47,5): error TS9014: Computed properties must be n
[ns().v]: 1
~~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:46:14: Add a type annotation to the variable o9.
!!! related TS9028 computedPropertiesNarrowed.ts:46:14: Add a type annotation to the variable o9.
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
!!! error TS5088: The inferred type of 'foo' references a type with a cyclic structure which cannot be trivially serialized. A type annotation is necessary.
+ ~~~
+!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
+!!! related TS9031 arrayFakeFlatNoCrashInferenceDeclarations.ts:13:10: Add a return type to the function declaration.
+!!! related TS9032 arrayFakeFlatNoCrashInferenceDeclarations.ts:13:10: Add a return type to the function declaration.
return flat(arr, depth);
}
\ No newline at end of file
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
!!! error TS2742: The inferred type of 'x' cannot be named without a reference to 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
+ ~
+!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
+!!! related TS9027 r/entry.ts:3:14: Add a type annotation to the variable x.
+!!! related TS9028 r/entry.ts:3:14: Add a type annotation to the variable x.
export const y: RootProps = bar();

\ No newline at end of file
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
+ export const a = getA();
+ ~
+!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
+!!! related TS9027 /p1/index.ts:4:14: Add a type annotation to the variable a.
+!!! related TS9028 /p1/index.ts:4:14: Add a type annotation to the variable a.
+==== /p2/index.d.ts (0 errors) ====
+ export const a: import("typescript-fsa").A;
+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
+ export const a = getA();
+ ~
+!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
+!!! related TS9027 /p1/index.ts:4:14: Add a type annotation to the variable a.
+!!! related TS9028 /p1/index.ts:4:14: Add a type annotation to the variable a.
+==== /p2/index.d.ts (0 errors) ====
+ export const a: import("typescript-fsa").A;
+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// [Errors] ////

index.ts(7,1): error TS2742: The inferred type of 'default' cannot be named without a reference to 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
+index.ts(7,16): error TS9037: Default exports can't be inferred with --isolatedDeclarations.
+index.ts(7,16): error TS9038: Default exports can't be inferred with --isolatedDeclarations.


==== node_modules/styled-components/node_modules/hoist-non-react-statics/index.d.ts (0 errors) ====
Expand Down Expand Up @@ -48,7 +48,7 @@
~~~
!!! error TS2742: The inferred type of 'default' cannot be named without a reference to 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
+ ~~
+!!! error TS9037: Default exports can't be inferred with --isolatedDeclarations.
+!!! related TS9036 index.ts:7:1: Move the expression in default export to a variable and add a type annotation to it.
+!!! error TS9038: Default exports can't be inferred with --isolatedDeclarations.
+!!! related TS9037 index.ts:7:1: Move the expression in default export to a variable and add a type annotation to it.

\ No newline at end of file
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
+ export const ADMIN = MetadataAccessor.create<boolean>('1');
+ ~~~~~
+!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
+!!! related TS9027 monorepo/pkg3/src/keys.ts:3:14: Add a type annotation to the variable ADMIN.
+!!! related TS9028 monorepo/pkg3/src/keys.ts:3:14: Add a type annotation to the variable ADMIN.
+==== monorepo/pkg1/dist/index.d.ts (0 errors) ====
+ export * from './types';
+==== monorepo/pkg1/dist/types.d.ts (0 errors) ====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
+ export const ADMIN = MetadataAccessor.create<boolean>('1');
+ ~~~~~
+!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
+!!! related TS9027 monorepo/pkg3/src/keys.ts:3:14: Add a type annotation to the variable ADMIN.
+!!! related TS9028 monorepo/pkg3/src/keys.ts:3:14: Add a type annotation to the variable ADMIN.
+==== monorepo/pkg1/dist/index.d.ts (0 errors) ====
+ export * from './types';
+==== monorepo/pkg1/dist/types.d.ts (0 errors) ====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
!!! error TS2742: The inferred type of 'ADMIN' cannot be named without a reference to '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
+ ~~~~~
+!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
+!!! related TS9027 monorepo/pkg3/src/keys.ts:3:14: Add a type annotation to the variable ADMIN.
+!!! related TS9028 monorepo/pkg3/src/keys.ts:3:14: Add a type annotation to the variable ADMIN.
==== monorepo/pkg1/dist/index.d.ts (0 errors) ====
export * from './types';
==== monorepo/pkg1/dist/types.d.ts (0 errors) ====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
+ export const useCsvParser = () => {
+ ~~~~~~~
+!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
+!!! related TS9027 /p1/index.ts:7:14: Add a type annotation to the variable useCsvParser.
+!!! related TS9030 /p1/index.ts:7:29: Add a return type to the function expression.
+!!! related TS9028 /p1/index.ts:7:14: Add a type annotation to the variable useCsvParser.
+!!! related TS9031 /p1/index.ts:7:29: Add a return type to the function expression.
+ const parserRef = useRef<typeof import("csv-parse")>(null);
+ return parserRef;
+ };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
!!! error TS7006: Parameter 'value' implicitly has an 'any' type.
+ ~~~~~
+!!! error TS9009: At least one accessor must have an explicit return type annotation with --isolatedDeclarations.
+!!! related TS9033 isolatedDeclarationErrorsClasses.ts:48:9: Add a type to parameter of the set accessor declaration.
+!!! related TS9034 isolatedDeclarationErrorsClasses.ts:48:9: Add a type to parameter of the set accessor declaration.

[("A" + "B") as "AB"] = 1;
~~~~~~~~~~~~~~~~~~~~~
Expand Down
Loading