Skip to content

Commit

Permalink
Get Widened Type
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Ozga committed Feb 16, 2017
1 parent 533262c commit 2187e67
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Expand Up @@ -83,6 +83,7 @@ namespace ts {
getSignaturesOfType,
getIndexTypeOfType,
getBaseTypes,
getWidenedType,
getTypeFromTypeNode,
getParameterType: getTypeAtPosition,
getReturnTypeOfSignature,
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -3307,6 +3307,14 @@
"category": "Message",
"code": 90015
},
"Add declaration for missing property '{0}'": {
"category": "Message",
"code": 90016
},
"Add index accessor for missing property '{0}'": {
"category": "Message",
"code": 90017
},
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
"category": "Error",
"code": 8017
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Expand Up @@ -2382,6 +2382,7 @@
getSignaturesOfType(type: Type, kind: SignatureKind): Signature[];
getIndexTypeOfType(type: Type, kind: IndexKind): Type;
getBaseTypes(type: InterfaceType): BaseType[];
getWidenedType(type: Type): Type;
getReturnTypeOfSignature(signature: Signature): Type;
/**
* Gets the type of a parameter at a given position in a signature.
Expand Down
54 changes: 31 additions & 23 deletions src/services/codefixes/fixAddMissingMember.ts
Expand Up @@ -12,47 +12,61 @@ namespace ts.codefix {
// This is the identifier in the case of a class declaration
// or the class keyword token in the case of a class expression.
const token = getTokenAtPosition(sourceFile, start);
const checker = context.program.getTypeChecker();

if(!(token.parent && token.parent.kind === SyntaxKind.PropertyAccessExpression)) {
const classDeclaration = getContainingClass(token);
if (!classDeclaration) {
return undefined;
}

if((token.parent as PropertyAccessExpression).expression.kind !== SyntaxKind.ThisKeyword) {
const startPos = classDeclaration.members.pos;

if (!(token.parent && token.parent.kind === SyntaxKind.PropertyAccessExpression)) {
return undefined;
}
1 + 1;

if ((token.parent as PropertyAccessExpression).expression.kind !== SyntaxKind.ThisKeyword) {
return undefined;
}

// if function call, synthesize function declaration
if(token.parent.parent.kind == SyntaxKind.CallExpression) {

}

let typeString: string = 'any';

// if binary expression, try to infer type for LHS, else use any
if(token.parent.parent.kind === SyntaxKind.BinaryExpression)
{
if (token.parent.parent.kind === SyntaxKind.BinaryExpression) {
const binaryExpression = token.parent.parent as BinaryExpression;
binaryExpression.operatorToken;

const type = checker.getTypeAtLocation(binaryExpression.right);

const checker = context.program.getTypeChecker();
const type = checker.getWidenedType(checker.getTypeAtLocation(binaryExpression.right));
typeString = checker.typeToString(type);
}

const classDeclaration = getContainingClass(token);
const startPos = classDeclaration.members.pos;
return [{
description: getLocaleSpecificMessage(Diagnostics.Implement_inherited_abstract_class),
description: formatStringFromArgs(getLocaleSpecificMessage(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: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_accessor_for_missing_property_0), [token.getText()]),
changes: [{
fileName: sourceFile.fileName,
textChanges: [{
span: { start: startPos, length: 0 },
newText: `[name: string]: ${typeString};`
}]
}]
}];
}




// x needs to be a `this` construct. ie
// this.<thing>.
// Want to infer type of x when possible. ie:
// * assignment,
// * function call argument: foo<T>(this.x) where foo(x: SomeType<T>)
Expand All @@ -65,10 +79,4 @@ namespace ts.codefix {
// inferred type might be error. then add any.
// either make indexable of the inferred type
// add named member of the inferred type.
}

// // class C {
// // constructor() {
// // this.x = 1;
// // }
// // }
}

0 comments on commit 2187e67

Please sign in to comment.