diff --git a/lib/lib.d.ts b/lib/lib.d.ts index a65a0100e6c..1c0d6477a4e 100644 --- a/lib/lib.d.ts +++ b/lib/lib.d.ts @@ -160,7 +160,7 @@ interface ObjectConstructor { * Creates an object that has the specified prototype or that has null prototype. * @param o Object to use as a prototype. May be null. */ - create(o: T | null): T | object; + create(o: object | null): any; /** * Creates an object that has the specified prototype, and that optionally contains specified properties. diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index 496dd2102d5..a473e06ba93 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -160,7 +160,7 @@ interface ObjectConstructor { * Creates an object that has the specified prototype or that has null prototype. * @param o Object to use as a prototype. May be null. */ - create(o: T | null): T | object; + create(o: object | null): any; /** * Creates an object that has the specified prototype, and that optionally contains specified properties. diff --git a/lib/lib.es6.d.ts b/lib/lib.es6.d.ts index 0265e696e0d..f25e509238c 100644 --- a/lib/lib.es6.d.ts +++ b/lib/lib.es6.d.ts @@ -160,7 +160,7 @@ interface ObjectConstructor { * Creates an object that has the specified prototype or that has null prototype. * @param o Object to use as a prototype. May be null. */ - create(o: T | null): T | object; + create(o: object | null): any; /** * Creates an object that has the specified prototype, and that optionally contains specified properties. diff --git a/lib/tsc.js b/lib/tsc.js index f8b64b8f397..718b717b5b0 100644 --- a/lib/tsc.js +++ b/lib/tsc.js @@ -3347,7 +3347,9 @@ var ts; Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated: { code: 90010, category: ts.DiagnosticCategory.Error, key: "Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated_90010", message: "Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated." }, Import_0_from_1: { code: 90013, category: ts.DiagnosticCategory.Message, key: "Import_0_from_1_90013", message: "Import {0} from {1}" }, Change_0_to_1: { code: 90014, category: ts.DiagnosticCategory.Message, key: "Change_0_to_1_90014", message: "Change {0} to {1}" }, - Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}" }, + Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}." }, + Add_declaration_for_missing_property_0: { code: 90016, category: ts.DiagnosticCategory.Message, key: "Add_declaration_for_missing_property_0_90016", message: "Add declaration for missing property '{0}'." }, + Add_index_signature_for_missing_property_0: { code: 90017, category: ts.DiagnosticCategory.Message, key: "Add_index_signature_for_missing_property_0_90017", message: "Add index signature for missing property '{0}'" }, Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0: { code: 8017, category: ts.DiagnosticCategory.Error, key: "Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0_8017", message: "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." }, Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0: { code: 8018, category: ts.DiagnosticCategory.Error, key: "Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0_8018", message: "Octal literals are not allowed in enums members initializer. Use the syntax '{0}'." }, }; @@ -20115,6 +20117,8 @@ var ts; getSignaturesOfType: getSignaturesOfType, getIndexTypeOfType: getIndexTypeOfType, getBaseTypes: getBaseTypes, + getBaseTypeOfLiteralType: getBaseTypeOfLiteralType, + getWidenedType: getWidenedType, getTypeFromTypeNode: getTypeFromTypeNode, getParameterType: getTypeAtPosition, getReturnTypeOfSignature: getReturnTypeOfSignature, diff --git a/lib/tsserver.js b/lib/tsserver.js index d73cb29b3cf..c9b560dcdfb 100644 --- a/lib/tsserver.js +++ b/lib/tsserver.js @@ -4308,7 +4308,9 @@ var ts; Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated: { code: 90010, category: ts.DiagnosticCategory.Error, key: "Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated_90010", message: "Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated." }, Import_0_from_1: { code: 90013, category: ts.DiagnosticCategory.Message, key: "Import_0_from_1_90013", message: "Import {0} from {1}" }, Change_0_to_1: { code: 90014, category: ts.DiagnosticCategory.Message, key: "Change_0_to_1_90014", message: "Change {0} to {1}" }, - Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}" }, + Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}." }, + Add_declaration_for_missing_property_0: { code: 90016, category: ts.DiagnosticCategory.Message, key: "Add_declaration_for_missing_property_0_90016", message: "Add declaration for missing property '{0}'." }, + Add_index_signature_for_missing_property_0: { code: 90017, category: ts.DiagnosticCategory.Message, key: "Add_index_signature_for_missing_property_0_90017", message: "Add index signature for missing property '{0}'" }, Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0: { code: 8017, category: ts.DiagnosticCategory.Error, key: "Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0_8017", message: "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." }, Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0: { code: 8018, category: ts.DiagnosticCategory.Error, key: "Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0_8018", message: "Octal literals are not allowed in enums members initializer. Use the syntax '{0}'." }, }; @@ -21175,6 +21177,8 @@ var ts; getSignaturesOfType: getSignaturesOfType, getIndexTypeOfType: getIndexTypeOfType, getBaseTypes: getBaseTypes, + getBaseTypeOfLiteralType: getBaseTypeOfLiteralType, + getWidenedType: getWidenedType, getTypeFromTypeNode: getTypeFromTypeNode, getParameterType: getTypeAtPosition, getReturnTypeOfSignature: getReturnTypeOfSignature, @@ -59054,7 +59058,7 @@ var ts; var importDecl = importSpecifier.parent; ts.Debug.assert(importDecl.moduleSpecifier === importSpecifier); var defaultName = importDecl.importClause.name; - var defaultReferencedSymbol = checker.getAliasedSymbol(checker.getSymbolAtLocation(defaultName)); + var defaultReferencedSymbol = defaultName && checker.getAliasedSymbol(checker.getSymbolAtLocation(defaultName)); if (symbol === defaultReferencedSymbol) { return defaultName.text; } @@ -65647,6 +65651,62 @@ var ts; })(codefix = ts.codefix || (ts.codefix = {})); })(ts || (ts = {})); var ts; +(function (ts) { + var codefix; + (function (codefix) { + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.Property_0_does_not_exist_on_type_1.code], + getCodeActions: getActionsForAddMissingMember + }); + function getActionsForAddMissingMember(context) { + var sourceFile = context.sourceFile; + var start = context.span.start; + var token = ts.getTokenAtPosition(sourceFile, start); + if (token.kind != 70) { + return undefined; + } + var classDeclaration = ts.getContainingClass(token); + if (!classDeclaration) { + return undefined; + } + if (!(token.parent && token.parent.kind === 178)) { + return undefined; + } + if (token.parent.expression.kind !== 98) { + return undefined; + } + var typeString = "any"; + if (token.parent.parent.kind === 193) { + var binaryExpression = token.parent.parent; + var checker = context.program.getTypeChecker(); + var widenedType = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(binaryExpression.right))); + typeString = checker.typeToString(widenedType); + } + var startPos = classDeclaration.members.pos; + return [{ + description: ts.formatStringFromArgs(ts.getLocaleSpecificMessage(ts.Diagnostics.Add_declaration_for_missing_property_0), [token.getText()]), + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: startPos, length: 0 }, + newText: token.getFullText(sourceFile) + ": " + typeString + ";" + }] + }] + }, + { + description: ts.formatStringFromArgs(ts.getLocaleSpecificMessage(ts.Diagnostics.Add_index_signature_for_missing_property_0), [token.getText()]), + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: startPos, length: 0 }, + newText: "[name: string]: " + typeString + ";" + }] + }] + }]; + } + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +var ts; (function (ts) { var codefix; (function (codefix) { @@ -66456,7 +66516,7 @@ var ts; } var declaration = declarations[0]; var name = declaration.name ? declaration.name.getText() : undefined; - var visibility = getVisibilityPrefix(ts.getModifierFlags(declaration)); + var visibility = getVisibilityPrefixWithSpace(ts.getModifierFlags(declaration)); switch (declaration.kind) { case 152: case 153: @@ -66473,7 +66533,7 @@ var ts; if (declarations.length === 1) { ts.Debug.assert(signatures.length === 1); var sigString_1 = checker.signatureToString(signatures[0], enclosingDeclaration, 2048, 0); - return "" + visibility + name + sigString_1 + getMethodBodyStub(newlineChar); + return getStubbedMethod(visibility, name, sigString_1, newlineChar); } var result = ""; for (var i = 0; i < signatures.length; i++) { @@ -66489,7 +66549,7 @@ var ts; bodySig = createBodySignatureWithAnyTypes(signatures, enclosingDeclaration, checker); } var sigString = checker.signatureToString(bodySig, enclosingDeclaration, 2048, 0); - result += "" + visibility + name + sigString + getMethodBodyStub(newlineChar); + result += getStubbedMethod(visibility, name, sigString, newlineChar); return result; default: return ""; @@ -66538,10 +66598,15 @@ var ts; return newParameter; } } - function getMethodBodyStub(newLineChar) { - return " {" + newLineChar + "throw new Error('Method not implemented.');" + newLineChar + "}" + newLineChar; + function getStubbedMethod(visibility, name, sigString, newlineChar) { + if (sigString === void 0) { sigString = "()"; } + return "" + visibility + name + sigString + getMethodBodyStub(newlineChar); } - function getVisibilityPrefix(flags) { + codefix.getStubbedMethod = getStubbedMethod; + function getMethodBodyStub(newlineChar) { + return " {" + newlineChar + "throw new Error('Method not implemented.');" + newlineChar + "}" + newlineChar; + } + function getVisibilityPrefixWithSpace(flags) { if (flags & 4) { return "public "; } diff --git a/lib/tsserverlibrary.d.ts b/lib/tsserverlibrary.d.ts index 458801b631c..6ca7fd91751 100644 --- a/lib/tsserverlibrary.d.ts +++ b/lib/tsserverlibrary.d.ts @@ -1564,6 +1564,8 @@ declare namespace ts { getSignaturesOfType(type: Type, kind: SignatureKind): Signature[]; getIndexTypeOfType(type: Type, kind: IndexKind): Type; getBaseTypes(type: InterfaceType): BaseType[]; + getBaseTypeOfLiteralType(type: Type): Type; + getWidenedType(type: Type): Type; getReturnTypeOfSignature(signature: Signature): Type; getNonNullableType(type: Type): Type; getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[]; diff --git a/lib/tsserverlibrary.js b/lib/tsserverlibrary.js index f5502c5dae3..fbb115c4ea2 100644 --- a/lib/tsserverlibrary.js +++ b/lib/tsserverlibrary.js @@ -4308,7 +4308,9 @@ var ts; Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated: { code: 90010, category: ts.DiagnosticCategory.Error, key: "Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated_90010", message: "Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated." }, Import_0_from_1: { code: 90013, category: ts.DiagnosticCategory.Message, key: "Import_0_from_1_90013", message: "Import {0} from {1}" }, Change_0_to_1: { code: 90014, category: ts.DiagnosticCategory.Message, key: "Change_0_to_1_90014", message: "Change {0} to {1}" }, - Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}" }, + Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}." }, + Add_declaration_for_missing_property_0: { code: 90016, category: ts.DiagnosticCategory.Message, key: "Add_declaration_for_missing_property_0_90016", message: "Add declaration for missing property '{0}'." }, + Add_index_signature_for_missing_property_0: { code: 90017, category: ts.DiagnosticCategory.Message, key: "Add_index_signature_for_missing_property_0_90017", message: "Add index signature for missing property '{0}'" }, Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0: { code: 8017, category: ts.DiagnosticCategory.Error, key: "Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0_8017", message: "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." }, Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0: { code: 8018, category: ts.DiagnosticCategory.Error, key: "Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0_8018", message: "Octal literals are not allowed in enums members initializer. Use the syntax '{0}'." }, }; @@ -22308,6 +22310,8 @@ var ts; getSignaturesOfType: getSignaturesOfType, getIndexTypeOfType: getIndexTypeOfType, getBaseTypes: getBaseTypes, + getBaseTypeOfLiteralType: getBaseTypeOfLiteralType, + getWidenedType: getWidenedType, getTypeFromTypeNode: getTypeFromTypeNode, getParameterType: getTypeAtPosition, getReturnTypeOfSignature: getReturnTypeOfSignature, @@ -59054,7 +59058,7 @@ var ts; var importDecl = importSpecifier.parent; ts.Debug.assert(importDecl.moduleSpecifier === importSpecifier); var defaultName = importDecl.importClause.name; - var defaultReferencedSymbol = checker.getAliasedSymbol(checker.getSymbolAtLocation(defaultName)); + var defaultReferencedSymbol = defaultName && checker.getAliasedSymbol(checker.getSymbolAtLocation(defaultName)); if (symbol === defaultReferencedSymbol) { return defaultName.text; } @@ -65647,6 +65651,62 @@ var ts; })(codefix = ts.codefix || (ts.codefix = {})); })(ts || (ts = {})); var ts; +(function (ts) { + var codefix; + (function (codefix) { + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.Property_0_does_not_exist_on_type_1.code], + getCodeActions: getActionsForAddMissingMember + }); + function getActionsForAddMissingMember(context) { + var sourceFile = context.sourceFile; + var start = context.span.start; + var token = ts.getTokenAtPosition(sourceFile, start); + if (token.kind != 70) { + return undefined; + } + var classDeclaration = ts.getContainingClass(token); + if (!classDeclaration) { + return undefined; + } + if (!(token.parent && token.parent.kind === 178)) { + return undefined; + } + if (token.parent.expression.kind !== 98) { + return undefined; + } + var typeString = "any"; + if (token.parent.parent.kind === 193) { + var binaryExpression = token.parent.parent; + var checker = context.program.getTypeChecker(); + var widenedType = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(binaryExpression.right))); + typeString = checker.typeToString(widenedType); + } + var startPos = classDeclaration.members.pos; + return [{ + description: ts.formatStringFromArgs(ts.getLocaleSpecificMessage(ts.Diagnostics.Add_declaration_for_missing_property_0), [token.getText()]), + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: startPos, length: 0 }, + newText: token.getFullText(sourceFile) + ": " + typeString + ";" + }] + }] + }, + { + description: ts.formatStringFromArgs(ts.getLocaleSpecificMessage(ts.Diagnostics.Add_index_signature_for_missing_property_0), [token.getText()]), + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: startPos, length: 0 }, + newText: "[name: string]: " + typeString + ";" + }] + }] + }]; + } + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +var ts; (function (ts) { var codefix; (function (codefix) { @@ -66456,7 +66516,7 @@ var ts; } var declaration = declarations[0]; var name = declaration.name ? declaration.name.getText() : undefined; - var visibility = getVisibilityPrefix(ts.getModifierFlags(declaration)); + var visibility = getVisibilityPrefixWithSpace(ts.getModifierFlags(declaration)); switch (declaration.kind) { case 152: case 153: @@ -66473,7 +66533,7 @@ var ts; if (declarations.length === 1) { ts.Debug.assert(signatures.length === 1); var sigString_1 = checker.signatureToString(signatures[0], enclosingDeclaration, 2048, 0); - return "" + visibility + name + sigString_1 + getMethodBodyStub(newlineChar); + return getStubbedMethod(visibility, name, sigString_1, newlineChar); } var result = ""; for (var i = 0; i < signatures.length; i++) { @@ -66489,7 +66549,7 @@ var ts; bodySig = createBodySignatureWithAnyTypes(signatures, enclosingDeclaration, checker); } var sigString = checker.signatureToString(bodySig, enclosingDeclaration, 2048, 0); - result += "" + visibility + name + sigString + getMethodBodyStub(newlineChar); + result += getStubbedMethod(visibility, name, sigString, newlineChar); return result; default: return ""; @@ -66538,10 +66598,15 @@ var ts; return newParameter; } } - function getMethodBodyStub(newLineChar) { - return " {" + newLineChar + "throw new Error('Method not implemented.');" + newLineChar + "}" + newLineChar; + function getStubbedMethod(visibility, name, sigString, newlineChar) { + if (sigString === void 0) { sigString = "()"; } + return "" + visibility + name + sigString + getMethodBodyStub(newlineChar); } - function getVisibilityPrefix(flags) { + codefix.getStubbedMethod = getStubbedMethod; + function getMethodBodyStub(newlineChar) { + return " {" + newlineChar + "throw new Error('Method not implemented.');" + newlineChar + "}" + newlineChar; + } + function getVisibilityPrefixWithSpace(flags) { if (flags & 4) { return "public "; } diff --git a/lib/typescript.d.ts b/lib/typescript.d.ts index f5e79f233c5..4fb30c62a46 100644 --- a/lib/typescript.d.ts +++ b/lib/typescript.d.ts @@ -1631,6 +1631,8 @@ declare namespace ts { getSignaturesOfType(type: Type, kind: SignatureKind): Signature[]; getIndexTypeOfType(type: Type, kind: IndexKind): Type; getBaseTypes(type: InterfaceType): BaseType[]; + getBaseTypeOfLiteralType(type: Type): Type; + getWidenedType(type: Type): Type; getReturnTypeOfSignature(signature: Signature): Type; getNonNullableType(type: Type): Type; getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[]; diff --git a/lib/typescript.js b/lib/typescript.js index 2f343bae391..146c1df00f5 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -4792,7 +4792,9 @@ var ts; Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated: { code: 90010, category: ts.DiagnosticCategory.Error, key: "Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated_90010", message: "Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated." }, Import_0_from_1: { code: 90013, category: ts.DiagnosticCategory.Message, key: "Import_0_from_1_90013", message: "Import {0} from {1}" }, Change_0_to_1: { code: 90014, category: ts.DiagnosticCategory.Message, key: "Change_0_to_1_90014", message: "Change {0} to {1}" }, - Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}" }, + Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}." }, + Add_declaration_for_missing_property_0: { code: 90016, category: ts.DiagnosticCategory.Message, key: "Add_declaration_for_missing_property_0_90016", message: "Add declaration for missing property '{0}'." }, + Add_index_signature_for_missing_property_0: { code: 90017, category: ts.DiagnosticCategory.Message, key: "Add_index_signature_for_missing_property_0_90017", message: "Add index signature for missing property '{0}'" }, Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0: { code: 8017, category: ts.DiagnosticCategory.Error, key: "Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0_8017", message: "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." }, Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0: { code: 8018, category: ts.DiagnosticCategory.Error, key: "Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0_8018", message: "Octal literals are not allowed in enums members initializer. Use the syntax '{0}'." }, }; @@ -24730,6 +24732,8 @@ var ts; getSignaturesOfType: getSignaturesOfType, getIndexTypeOfType: getIndexTypeOfType, getBaseTypes: getBaseTypes, + getBaseTypeOfLiteralType: getBaseTypeOfLiteralType, + getWidenedType: getWidenedType, getTypeFromTypeNode: getTypeFromTypeNode, getParameterType: getTypeAtPosition, getReturnTypeOfSignature: getReturnTypeOfSignature, @@ -71286,7 +71290,7 @@ var ts; var importDecl = importSpecifier.parent; ts.Debug.assert(importDecl.moduleSpecifier === importSpecifier); var defaultName = importDecl.importClause.name; - var defaultReferencedSymbol = checker.getAliasedSymbol(checker.getSymbolAtLocation(defaultName)); + var defaultReferencedSymbol = defaultName && checker.getAliasedSymbol(checker.getSymbolAtLocation(defaultName)); if (symbol === defaultReferencedSymbol) { return defaultName.text; } @@ -79197,6 +79201,66 @@ var ts; })(ts || (ts = {})); /* @internal */ var ts; +(function (ts) { + var codefix; + (function (codefix) { + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.Property_0_does_not_exist_on_type_1.code], + getCodeActions: getActionsForAddMissingMember + }); + function getActionsForAddMissingMember(context) { + var sourceFile = context.sourceFile; + var start = context.span.start; + // This is the identifier of the missing property. eg: + // this.missing = 1; + // ^^^^^^^ + var token = ts.getTokenAtPosition(sourceFile, start); + if (token.kind != 70 /* Identifier */) { + return undefined; + } + var classDeclaration = ts.getContainingClass(token); + if (!classDeclaration) { + return undefined; + } + if (!(token.parent && token.parent.kind === 178 /* PropertyAccessExpression */)) { + return undefined; + } + if (token.parent.expression.kind !== 98 /* ThisKeyword */) { + return undefined; + } + var typeString = "any"; + if (token.parent.parent.kind === 193 /* BinaryExpression */) { + var binaryExpression = token.parent.parent; + var checker = context.program.getTypeChecker(); + var widenedType = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(binaryExpression.right))); + typeString = checker.typeToString(widenedType); + } + var startPos = classDeclaration.members.pos; + return [{ + description: ts.formatStringFromArgs(ts.getLocaleSpecificMessage(ts.Diagnostics.Add_declaration_for_missing_property_0), [token.getText()]), + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: startPos, length: 0 }, + newText: token.getFullText(sourceFile) + ": " + typeString + ";" + }] + }] + }, + { + description: ts.formatStringFromArgs(ts.getLocaleSpecificMessage(ts.Diagnostics.Add_index_signature_for_missing_property_0), [token.getText()]), + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: startPos, length: 0 }, + newText: "[name: string]: " + typeString + ";" + }] + }] + }]; + } + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +/* @internal */ +var ts; (function (ts) { var codefix; (function (codefix) { @@ -80115,7 +80179,7 @@ var ts; } var declaration = declarations[0]; var name = declaration.name ? declaration.name.getText() : undefined; - var visibility = getVisibilityPrefix(ts.getModifierFlags(declaration)); + var visibility = getVisibilityPrefixWithSpace(ts.getModifierFlags(declaration)); switch (declaration.kind) { case 152 /* GetAccessor */: case 153 /* SetAccessor */: @@ -80139,7 +80203,7 @@ var ts; if (declarations.length === 1) { ts.Debug.assert(signatures.length === 1); var sigString_1 = checker.signatureToString(signatures[0], enclosingDeclaration, 2048 /* SuppressAnyReturnType */, 0 /* Call */); - return "" + visibility + name + sigString_1 + getMethodBodyStub(newlineChar); + return getStubbedMethod(visibility, name, sigString_1, newlineChar); } var result = ""; for (var i = 0; i < signatures.length; i++) { @@ -80157,7 +80221,7 @@ var ts; bodySig = createBodySignatureWithAnyTypes(signatures, enclosingDeclaration, checker); } var sigString = checker.signatureToString(bodySig, enclosingDeclaration, 2048 /* SuppressAnyReturnType */, 0 /* Call */); - result += "" + visibility + name + sigString + getMethodBodyStub(newlineChar); + result += getStubbedMethod(visibility, name, sigString, newlineChar); return result; default: return ""; @@ -80206,10 +80270,15 @@ var ts; return newParameter; } } - function getMethodBodyStub(newLineChar) { - return " {" + newLineChar + "throw new Error('Method not implemented.');" + newLineChar + "}" + newLineChar; + function getStubbedMethod(visibility, name, sigString, newlineChar) { + if (sigString === void 0) { sigString = "()"; } + return "" + visibility + name + sigString + getMethodBodyStub(newlineChar); } - function getVisibilityPrefix(flags) { + codefix.getStubbedMethod = getStubbedMethod; + function getMethodBodyStub(newlineChar) { + return " {" + newlineChar + "throw new Error('Method not implemented.');" + newlineChar + "}" + newlineChar; + } + function getVisibilityPrefixWithSpace(flags) { if (flags & 4 /* Public */) { return "public "; } @@ -80222,6 +80291,7 @@ var ts; })(codefix = ts.codefix || (ts.codefix = {})); })(ts || (ts = {})); /// +/// /// /// /// diff --git a/lib/typescriptServices.d.ts b/lib/typescriptServices.d.ts index 05b2400dc19..956c52ddc12 100644 --- a/lib/typescriptServices.d.ts +++ b/lib/typescriptServices.d.ts @@ -1631,6 +1631,8 @@ declare namespace ts { getSignaturesOfType(type: Type, kind: SignatureKind): Signature[]; getIndexTypeOfType(type: Type, kind: IndexKind): Type; getBaseTypes(type: InterfaceType): BaseType[]; + getBaseTypeOfLiteralType(type: Type): Type; + getWidenedType(type: Type): Type; getReturnTypeOfSignature(signature: Signature): Type; getNonNullableType(type: Type): Type; getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[]; diff --git a/lib/typescriptServices.js b/lib/typescriptServices.js index 2f343bae391..146c1df00f5 100644 --- a/lib/typescriptServices.js +++ b/lib/typescriptServices.js @@ -4792,7 +4792,9 @@ var ts; Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated: { code: 90010, category: ts.DiagnosticCategory.Error, key: "Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated_90010", message: "Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated." }, Import_0_from_1: { code: 90013, category: ts.DiagnosticCategory.Message, key: "Import_0_from_1_90013", message: "Import {0} from {1}" }, Change_0_to_1: { code: 90014, category: ts.DiagnosticCategory.Message, key: "Change_0_to_1_90014", message: "Change {0} to {1}" }, - Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}" }, + Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}." }, + Add_declaration_for_missing_property_0: { code: 90016, category: ts.DiagnosticCategory.Message, key: "Add_declaration_for_missing_property_0_90016", message: "Add declaration for missing property '{0}'." }, + Add_index_signature_for_missing_property_0: { code: 90017, category: ts.DiagnosticCategory.Message, key: "Add_index_signature_for_missing_property_0_90017", message: "Add index signature for missing property '{0}'" }, Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0: { code: 8017, category: ts.DiagnosticCategory.Error, key: "Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0_8017", message: "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." }, Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0: { code: 8018, category: ts.DiagnosticCategory.Error, key: "Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0_8018", message: "Octal literals are not allowed in enums members initializer. Use the syntax '{0}'." }, }; @@ -24730,6 +24732,8 @@ var ts; getSignaturesOfType: getSignaturesOfType, getIndexTypeOfType: getIndexTypeOfType, getBaseTypes: getBaseTypes, + getBaseTypeOfLiteralType: getBaseTypeOfLiteralType, + getWidenedType: getWidenedType, getTypeFromTypeNode: getTypeFromTypeNode, getParameterType: getTypeAtPosition, getReturnTypeOfSignature: getReturnTypeOfSignature, @@ -71286,7 +71290,7 @@ var ts; var importDecl = importSpecifier.parent; ts.Debug.assert(importDecl.moduleSpecifier === importSpecifier); var defaultName = importDecl.importClause.name; - var defaultReferencedSymbol = checker.getAliasedSymbol(checker.getSymbolAtLocation(defaultName)); + var defaultReferencedSymbol = defaultName && checker.getAliasedSymbol(checker.getSymbolAtLocation(defaultName)); if (symbol === defaultReferencedSymbol) { return defaultName.text; } @@ -79197,6 +79201,66 @@ var ts; })(ts || (ts = {})); /* @internal */ var ts; +(function (ts) { + var codefix; + (function (codefix) { + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.Property_0_does_not_exist_on_type_1.code], + getCodeActions: getActionsForAddMissingMember + }); + function getActionsForAddMissingMember(context) { + var sourceFile = context.sourceFile; + var start = context.span.start; + // This is the identifier of the missing property. eg: + // this.missing = 1; + // ^^^^^^^ + var token = ts.getTokenAtPosition(sourceFile, start); + if (token.kind != 70 /* Identifier */) { + return undefined; + } + var classDeclaration = ts.getContainingClass(token); + if (!classDeclaration) { + return undefined; + } + if (!(token.parent && token.parent.kind === 178 /* PropertyAccessExpression */)) { + return undefined; + } + if (token.parent.expression.kind !== 98 /* ThisKeyword */) { + return undefined; + } + var typeString = "any"; + if (token.parent.parent.kind === 193 /* BinaryExpression */) { + var binaryExpression = token.parent.parent; + var checker = context.program.getTypeChecker(); + var widenedType = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(binaryExpression.right))); + typeString = checker.typeToString(widenedType); + } + var startPos = classDeclaration.members.pos; + return [{ + description: ts.formatStringFromArgs(ts.getLocaleSpecificMessage(ts.Diagnostics.Add_declaration_for_missing_property_0), [token.getText()]), + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: startPos, length: 0 }, + newText: token.getFullText(sourceFile) + ": " + typeString + ";" + }] + }] + }, + { + description: ts.formatStringFromArgs(ts.getLocaleSpecificMessage(ts.Diagnostics.Add_index_signature_for_missing_property_0), [token.getText()]), + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: startPos, length: 0 }, + newText: "[name: string]: " + typeString + ";" + }] + }] + }]; + } + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +/* @internal */ +var ts; (function (ts) { var codefix; (function (codefix) { @@ -80115,7 +80179,7 @@ var ts; } var declaration = declarations[0]; var name = declaration.name ? declaration.name.getText() : undefined; - var visibility = getVisibilityPrefix(ts.getModifierFlags(declaration)); + var visibility = getVisibilityPrefixWithSpace(ts.getModifierFlags(declaration)); switch (declaration.kind) { case 152 /* GetAccessor */: case 153 /* SetAccessor */: @@ -80139,7 +80203,7 @@ var ts; if (declarations.length === 1) { ts.Debug.assert(signatures.length === 1); var sigString_1 = checker.signatureToString(signatures[0], enclosingDeclaration, 2048 /* SuppressAnyReturnType */, 0 /* Call */); - return "" + visibility + name + sigString_1 + getMethodBodyStub(newlineChar); + return getStubbedMethod(visibility, name, sigString_1, newlineChar); } var result = ""; for (var i = 0; i < signatures.length; i++) { @@ -80157,7 +80221,7 @@ var ts; bodySig = createBodySignatureWithAnyTypes(signatures, enclosingDeclaration, checker); } var sigString = checker.signatureToString(bodySig, enclosingDeclaration, 2048 /* SuppressAnyReturnType */, 0 /* Call */); - result += "" + visibility + name + sigString + getMethodBodyStub(newlineChar); + result += getStubbedMethod(visibility, name, sigString, newlineChar); return result; default: return ""; @@ -80206,10 +80270,15 @@ var ts; return newParameter; } } - function getMethodBodyStub(newLineChar) { - return " {" + newLineChar + "throw new Error('Method not implemented.');" + newLineChar + "}" + newLineChar; + function getStubbedMethod(visibility, name, sigString, newlineChar) { + if (sigString === void 0) { sigString = "()"; } + return "" + visibility + name + sigString + getMethodBodyStub(newlineChar); } - function getVisibilityPrefix(flags) { + codefix.getStubbedMethod = getStubbedMethod; + function getMethodBodyStub(newlineChar) { + return " {" + newlineChar + "throw new Error('Method not implemented.');" + newlineChar + "}" + newlineChar; + } + function getVisibilityPrefixWithSpace(flags) { if (flags & 4 /* Public */) { return "public "; } @@ -80222,6 +80291,7 @@ var ts; })(codefix = ts.codefix || (ts.codefix = {})); })(ts || (ts = {})); /// +/// /// /// /// diff --git a/lib/typingsInstaller.js b/lib/typingsInstaller.js index d2415326f86..aecf4b5326a 100644 --- a/lib/typingsInstaller.js +++ b/lib/typingsInstaller.js @@ -3357,7 +3357,9 @@ var ts; Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated: { code: 90010, category: ts.DiagnosticCategory.Error, key: "Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated_90010", message: "Type '{0}' is not assignable to type '{1}'. Two different types with this name exist, but they are unrelated." }, Import_0_from_1: { code: 90013, category: ts.DiagnosticCategory.Message, key: "Import_0_from_1_90013", message: "Import {0} from {1}" }, Change_0_to_1: { code: 90014, category: ts.DiagnosticCategory.Message, key: "Change_0_to_1_90014", message: "Change {0} to {1}" }, - Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}" }, + Add_0_to_existing_import_declaration_from_1: { code: 90015, category: ts.DiagnosticCategory.Message, key: "Add_0_to_existing_import_declaration_from_1_90015", message: "Add {0} to existing import declaration from {1}." }, + Add_declaration_for_missing_property_0: { code: 90016, category: ts.DiagnosticCategory.Message, key: "Add_declaration_for_missing_property_0_90016", message: "Add declaration for missing property '{0}'." }, + Add_index_signature_for_missing_property_0: { code: 90017, category: ts.DiagnosticCategory.Message, key: "Add_index_signature_for_missing_property_0_90017", message: "Add index signature for missing property '{0}'" }, Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0: { code: 8017, category: ts.DiagnosticCategory.Error, key: "Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0_8017", message: "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." }, Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0: { code: 8018, category: ts.DiagnosticCategory.Error, key: "Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0_8018", message: "Octal literals are not allowed in enums members initializer. Use the syntax '{0}'." }, };