Remove Type Assignability QuickFix

This commit is contained in:
Arthur Ozga 2016-10-25 13:58:32 -07:00
parent bf3e487d28
commit 6b4f6b5864
18 changed files with 194 additions and 84 deletions

135
scripts/buildProtocol.js Normal file
View File

@ -0,0 +1,135 @@
/// <reference types="node"/>
"use strict";
var ts = require("../lib/typescript");
var path = require("path");
function endsWith(s, suffix) {
return s.lastIndexOf(suffix, s.length - suffix.length) !== -1;
}
var DeclarationsWalker = (function () {
function DeclarationsWalker(typeChecker, protocolFile) {
this.typeChecker = typeChecker;
this.protocolFile = protocolFile;
this.visitedTypes = [];
this.text = "";
}
DeclarationsWalker.getExtraDeclarations = function (typeChecker, protocolFile) {
var text = "declare namespace ts.server.protocol {\n";
var walker = new DeclarationsWalker(typeChecker, protocolFile);
walker.visitTypeNodes(protocolFile);
return walker.text
? "declare namespace ts.server.protocol {\n" + walker.text + "}"
: "";
};
DeclarationsWalker.prototype.processType = function (type) {
if (this.visitedTypes.indexOf(type) >= 0) {
return;
}
this.visitedTypes.push(type);
var s = type.aliasSymbol || type.getSymbol();
if (!s) {
return;
}
if (s.name === "Array") {
// we should process type argument instead
return this.processType(type.typeArguments[0]);
}
else {
for (var _i = 0, _a = s.getDeclarations(); _i < _a.length; _i++) {
var decl = _a[_i];
var sourceFile = decl.getSourceFile();
if (sourceFile === this.protocolFile || path.basename(sourceFile.fileName) === "lib.d.ts") {
return;
}
// splice declaration in final d.ts file
var text = decl.getFullText();
this.text += text + "\n";
// recursively pull all dependencies into result dts file
this.visitTypeNodes(decl);
}
}
};
DeclarationsWalker.prototype.visitTypeNodes = function (node) {
var _this = this;
if (node.parent) {
switch (node.parent.kind) {
case ts.SyntaxKind.VariableDeclaration:
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.PropertySignature:
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.IndexSignature:
if ((node.parent.type) === node) {
var type = this.typeChecker.getTypeAtLocation(node);
if (type && !(type.flags & ts.TypeFlags.TypeParameter)) {
this.processType(type);
}
}
break;
}
}
ts.forEachChild(node, function (n) { return _this.visitTypeNodes(n); });
};
return DeclarationsWalker;
}());
function generateProtocolFile(protocolTs, typeScriptServicesDts) {
var options = { target: ts.ScriptTarget.ES5, declaration: true, noResolve: true, types: [], stripInternal: true };
/**
* 1st pass - generate a program from protocol.ts and typescriptservices.d.ts and emit core version of protocol.d.ts with all internal members stripped
* @return text of protocol.d.t.s
*/
function getInitialDtsFileForProtocol() {
var program = ts.createProgram([protocolTs, typeScriptServicesDts], options);
var protocolDts;
program.emit(program.getSourceFile(protocolTs), function (file, content) {
if (endsWith(file, ".d.ts")) {
protocolDts = content;
}
});
if (protocolDts === undefined) {
throw new Error("Declaration file for protocol.ts is not generated");
}
return protocolDts;
}
var protocolFileName = "protocol.d.ts";
/**
* Second pass - generate a program from protocol.d.ts and typescriptservices.d.ts, then augment core protocol.d.ts with extra types from typescriptservices.d.ts
*/
function getProgramWithProtocolText(protocolDts, includeTypeScriptServices) {
var host = ts.createCompilerHost(options);
var originalGetSourceFile = host.getSourceFile;
host.getSourceFile = function (fileName) {
if (fileName === protocolFileName) {
return ts.createSourceFile(fileName, protocolDts, options.target);
}
return originalGetSourceFile.apply(host, [fileName]);
};
var rootFiles = includeTypeScriptServices ? [protocolFileName, typeScriptServicesDts] : [protocolFileName];
return ts.createProgram(rootFiles, options, host);
}
var protocolDts = getInitialDtsFileForProtocol();
var program = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ true);
var protocolFile = program.getSourceFile("protocol.d.ts");
var extraDeclarations = DeclarationsWalker.getExtraDeclarations(program.getTypeChecker(), protocolFile);
if (extraDeclarations) {
protocolDts += extraDeclarations;
}
// do sanity check and try to compile generated text as standalone program
var sanityCheckProgram = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ false);
var diagnostics = program.getSyntacticDiagnostics().concat(program.getSemanticDiagnostics(), program.getGlobalDiagnostics());
if (diagnostics.length) {
var flattenedDiagnostics = diagnostics.map(function (d) { return ts.flattenDiagnosticMessageText(d.messageText, "\n"); }).join("\n");
throw new Error("Unexpected errors during sanity check: " + flattenedDiagnostics);
}
return protocolDts;
}
if (process.argv.length < 5) {
console.log("Expected 3 arguments: path to 'protocol.ts', path to 'typescriptservices.d.ts' and path to output file");
process.exit(1);
}
var protocolTs = process.argv[2];
var typeScriptServicesDts = process.argv[3];
var outputFile = process.argv[4];
var generatedProtocolDts = generateProtocolFile(protocolTs, typeScriptServicesDts);
ts.sys.writeFile(outputFile, generatedProtocolDts);
//# sourceMappingURL=file:///C:/repo/TypeScript/scripts/buildProtocol.js.map

View File

@ -1,46 +1,5 @@
/* @internal */
namespace ts.codefix {
registerCodeFix({
errorCodes: [Diagnostics.Type_0_is_not_assignable_to_type_1.code],
getCodeActions: (context: CodeFixContext) => {
const sourceFile = context.sourceFile;
const start = context.span.start;
const token = getTokenAtPosition(sourceFile, start);
const checker = context.program.getTypeChecker();
let textChanges: TextChange[] = [];
if (token.kind === SyntaxKind.Identifier && token.parent.kind === SyntaxKind.VariableDeclaration) {
const variableDeclaration = <VariableDeclaration>token.parent;
const membersAndStartPosObject = getMembersAndStartPosFromReference(variableDeclaration);
const variableMembers = membersAndStartPosObject.members;
const trackingAddedMembers: string[] = [];
const startPos: number = membersAndStartPosObject.startPos;
if (variableDeclaration.type.kind === SyntaxKind.TypeReference) {
textChanges = textChanges.concat(getChanges(variableDeclaration.type, variableMembers, startPos, checker, /*reference*/ true, trackingAddedMembers, context.newLineCharacter));
}
else if (variableDeclaration.type.kind === SyntaxKind.UnionType) {
const types = (<UnionTypeNode>variableDeclaration.type).types;
ts.forEach(types, t => {
textChanges = textChanges.concat(getChanges(t, variableMembers, startPos, checker, /*reference*/ true, trackingAddedMembers, context.newLineCharacter));
});
}
}
if (textChanges.length > 0) {
return [{
description: getLocaleSpecificMessage(Diagnostics.Implement_interface_on_reference),
changes: [{
fileName: sourceFile.fileName,
textChanges: textChanges
}]
}];
}
return undefined;
}
});
registerCodeFix({
errorCodes: [Diagnostics.Class_0_incorrectly_implements_interface_1.code],
getCodeActions: (context: CodeFixContext) => {

View File

@ -10,7 +10,8 @@
//// |]f2() {}
//// }
verify.codeFixAtPosition(`f1(){
throw new Error('Method not Implemented');
},
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`f1(){
// throw new Error('Method not Implemented');
// },
// `);

View File

@ -8,8 +8,9 @@
////
//// |]}
verify.codeFixAtPosition(`
f1(){
throw new Error('Method not Implemented');
}
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`
// f1(){
// throw new Error('Method not Implemented');
// }
// `);

View File

@ -8,5 +8,6 @@
//// var x: I1 ={[|
//// |]}
verify.codeFixAtPosition(`x : ""
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : ""
// `);

View File

@ -7,5 +7,6 @@
//// var x: I1 ={[|
//// |]}
verify.codeFixAtPosition(`x : 0
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : 0
// `);

View File

@ -8,5 +8,6 @@
////
//// |]}
verify.codeFixAtPosition(`x : false
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : false
// `);

View File

@ -9,5 +9,6 @@
//// |]f1(){}
//// }
verify.codeFixAtPosition(`x : "",
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : "",
// `);

View File

@ -10,5 +10,6 @@
//// |]f1(){}
//// }
verify.codeFixAtPosition(`x : 0,
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : 0,
// `);

View File

@ -10,5 +10,6 @@
//// |]f1(){}
//// }
verify.codeFixAtPosition(`x : false,
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : false,
// `);

View File

@ -8,5 +8,6 @@
//// var x: I1 ={[|
//// |]}
verify.codeFixAtPosition(`x : null
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : null
// `);

View File

@ -10,5 +10,6 @@
//// |]f1(){}
//// }
verify.codeFixAtPosition(`x : null,
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : null,
// `);

View File

@ -8,5 +8,6 @@
//// var x: I1 ={[|
//// |]}
verify.codeFixAtPosition(`x : null
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : null
// `);

View File

@ -10,5 +10,6 @@
//// |]f1(){}
//// }
verify.codeFixAtPosition(`x : null,
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : null,
// `);

View File

@ -10,5 +10,6 @@
//// var x: I1 ={[|
//// |]}
verify.codeFixAtPosition(`x : null
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : null
// `);

View File

@ -12,5 +12,6 @@
//// |]f1(){}
//// }
verify.codeFixAtPosition(`x : null,
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`x : null,
// `);

View File

@ -12,10 +12,11 @@
////
//// |]}
verify.codeFixAtPosition(`f1(){
throw new Error('Method not Implemented');
}
f2(){
throw new Error('Method not Implemented');
}
`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`f1(){
// throw new Error('Method not Implemented');
// }
// f2(){
// throw new Error('Method not Implemented');
// }
// `);

View File

@ -6,6 +6,7 @@
////
//// var x: C2 = {[| |]}
verify.codeFixAtPosition(`f1<T extends number>(){
throw new Error('Method not Implemented');
}`);
verify.not.codeFixAvailable();
// verify.codeFixAtPosition(`f1<T extends number>(){
// throw new Error('Method not Implemented');
// }`);