Added fourslash tests for standalone and array initialization cases and started implementing them

This commit is contained in:
Florian Regensburger 2019-07-07 13:56:34 +02:00
parent 573b537015
commit 7d08f172d8
11 changed files with 64 additions and 10 deletions

View File

@ -1,6 +1,6 @@
/* @internal */
namespace ts.codefix {
const fixId = "addMissingConstInForLoop";
const fixId = "addMissingConst";
const errorCodes = [
Diagnostics.Cannot_find_name_0.code,
Diagnostics.No_value_exists_in_scope_for_the_shorthand_property_0_Either_declare_one_or_provide_an_initializer.code
@ -22,12 +22,30 @@ namespace ts.codefix {
});
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number, fixedNodes?: NodeSet<Node>) {
const forInitializer = findAncestor(getTokenAtPosition(sourceFile, pos), node =>
const token = getTokenAtPosition(sourceFile, pos);
const forInitializer = findAncestor(token, node =>
isForInOrOfStatement(node.parent) ? node.parent.initializer === node
: isPossiblyPartOfDestructuring(node) ? false : "quit");
if (!forInitializer) return;
if (!fixedNodes || fixedNodes.tryAdd(forInitializer)) {
changeTracker.insertNodeBefore(sourceFile, forInitializer, createToken(SyntaxKind.ConstKeyword));
if (forInitializer) return applyChange(changeTracker, forInitializer, sourceFile, fixedNodes);
const parent = token.parent;
const standaloneInitializer = isExpressionStatement(parent.parent);
if (standaloneInitializer) return applyChange(changeTracker, parent, sourceFile, fixedNodes);
const arrayLiteralInitializer = isArrayLiteralExpression(token.parent);
if (arrayLiteralInitializer) {
const availableIdentifiers: string[] = []; // TODO: where to get/gather this information from?
const noIdentifiersDeclared = parent.forEachChild(node => availableIdentifiers.indexOf(node.getFullText()) < 0);
if (!noIdentifiersDeclared) return;
return applyChange(changeTracker, parent, sourceFile, fixedNodes);
}
}
function applyChange(changeTracker: textChanges.ChangeTracker, initializer: Node, sourceFile: SourceFile, fixedNodes?: NodeSet<Node>) {
if (!fixedNodes || fixedNodes.tryAdd(initializer)) {
changeTracker.insertNodeBefore(sourceFile, initializer, createToken(SyntaxKind.ConstKeyword));
}
}

View File

@ -45,7 +45,7 @@
"codeFixProvider.ts",
"refactorProvider.ts",
"codefixes/addConvertToUnknownForNonOverlappingTypes.ts",
"codefixes/addMissingConstInForLoop.ts",
"codefixes/addMissingConst.ts",
"codefixes/addMissingInvocationForDecorator.ts",
"codefixes/addNameToNamelessParameter.ts",
"codefixes/annotateWithTypeFromJSDoc.ts",

View File

@ -4,7 +4,7 @@
////for (y in []) {}
verify.codeFixAll({
fixId: "addMissingConstInForLoop",
fixId: "addMissingConst",
fixAllDescription: "Add 'const' to all unresolved variables",
newFileContent:
`for (const x in []) {}

View File

@ -4,7 +4,7 @@
////for ([x] of [[1,2]]) {}
verify.codeFixAll({
fixId: "addMissingConstInForLoop",
fixId: "addMissingConst",
fixAllDescription: "Add 'const' to all unresolved variables",
newFileContent:
`for (const [x, y] of [[1,2]]) {}

View File

@ -4,7 +4,7 @@
////for ({ x } of [{ x: 0 }]) { }
verify.codeFixAll({
fixId: "addMissingConstInForLoop",
fixId: "addMissingConst",
fixAllDescription: "Add 'const' to all unresolved variables",
newFileContent:
`for (const { x, y } of [{ x: 0, y: 1 }]) { }

View File

@ -4,7 +4,7 @@
////for (y of []) {}
verify.codeFixAll({
fixId: "addMissingConstInForLoop",
fixId: "addMissingConst",
fixAllDescription: "Add 'const' to all unresolved variables",
newFileContent:
`for (const x of []) {}

View File

@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />
////[x] = [0];
verify.codeFix({
description: "Add 'const' to unresolved variable",
newFileContent: "const [x] = [0];"
});

View File

@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />
////[x, y] = [0, 1];
verify.codeFixAll({
fixId: "addMissingConst",
fixAllDescription: "Add 'const' to all unresolved variables",
newFileContent: "const [x, y] = [0, 1];"
});

View File

@ -0,0 +1,6 @@
/// <reference path='fourslash.ts' />
////let x: any;
////[x, y] = [0, 1];
verify.not.codeFixAvailable();

View File

@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />
////x = 0;
verify.codeFix({
description: "Add 'const' to unresolved variable",
newFileContent: "const x = 0;"
});

View File

@ -0,0 +1,5 @@
/// <reference path='fourslash.ts' />
////x = 0, y = 0;
verify.not.codeFixAvailable();