-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Object rest #12028
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
Object rest #12028
Changes from 8 commits
a7c1836
e3a08ed
ac20b46
9d4ddb6
7ff8876
7ed5204
14f8b99
c9c5f49
71f3157
cc342d1
4337369
4c365bd
609cd00
9977936
214ce38
0196947
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2982,26 +2982,31 @@ namespace ts { | |
return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false); | ||
} | ||
|
||
function getTextOfPropertyName(name: PropertyName): string { | ||
switch (name.kind) { | ||
case SyntaxKind.Identifier: | ||
return (<Identifier>name).text; | ||
case SyntaxKind.StringLiteral: | ||
case SyntaxKind.NumericLiteral: | ||
return (<LiteralExpression>name).text; | ||
case SyntaxKind.ComputedPropertyName: | ||
if (isStringOrNumericLiteral((<ComputedPropertyName>name).expression.kind)) { | ||
return (<LiteralExpression>(<ComputedPropertyName>name).expression).text; | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
function isComputedNonLiteralName(name: PropertyName): boolean { | ||
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteral((<ComputedPropertyName>name).expression.kind); | ||
} | ||
|
||
function getRestType(source: Type, properties: PropertyName[], symbol: Symbol): Type { | ||
Debug.assert(!!(source.flags & TypeFlags.Object), "Rest types only support object types right now."); | ||
const members = createMap<Symbol>(); | ||
const names = createMap<true>(); | ||
for (const name of properties) { | ||
names[getTextOfPropertyName(name)] = true; | ||
} | ||
for (const prop of getPropertiesOfType(source)) { | ||
const inNamesToRemove = prop.name in names; | ||
const isPrivate = getDeclarationModifierFlagsFromSymbol(prop) & (ModifierFlags.Private | ModifierFlags.Protected); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we removing private? it will exist at runtime, we do not create it as enumerable: false, and it not a parent property. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well.. this is actually different from what we do with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spread also removes privates. It's the right thing to do there, and I think it is here too: class P { private p: number }
let p: P;
var { ...exposed } = p; I don't think |
||
const isMethod = prop.flags & SymbolFlags.Method; | ||
const isSetOnlyAccessor = prop.flags & SymbolFlags.SetAccessor && !(prop.flags & SymbolFlags.GetAccessor); | ||
if (!inNamesToRemove && !isPrivate && !isMethod && !isSetOnlyAccessor) { | ||
members[prop.name] = prop; | ||
} | ||
} | ||
const stringIndexInfo = getIndexInfoOfType(source, IndexKind.String); | ||
const numberIndexInfo = getIndexInfoOfType(source, IndexKind.Number); | ||
return createAnonymousType(symbol, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); | ||
} | ||
|
||
/** Return the inferred type for a binding element */ | ||
function getTypeForBindingElement(declaration: BindingElement): Type { | ||
const pattern = <BindingPattern>declaration.parent; | ||
|
@@ -3022,26 +3027,41 @@ namespace ts { | |
|
||
let type: Type; | ||
if (pattern.kind === SyntaxKind.ObjectBindingPattern) { | ||
// Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) | ||
const name = declaration.propertyName || <Identifier>declaration.name; | ||
if (isComputedNonLiteralName(name)) { | ||
// computed properties with non-literal names are treated as 'any' | ||
return anyType; | ||
} | ||
if (declaration.initializer) { | ||
getContextualType(declaration.initializer); | ||
if (declaration.dotDotDotToken) { | ||
if (!(parentType.flags & TypeFlags.Object)) { | ||
error(declaration, Diagnostics.Rest_types_may_only_be_created_from_object_types); | ||
return unknownType; | ||
} | ||
const literalMembers: PropertyName[] = []; | ||
for (const element of pattern.elements) { | ||
if (element.kind !== SyntaxKind.OmittedExpression && !(element as BindingElement).dotDotDotToken) { | ||
literalMembers.push(element.propertyName || element.name as Identifier); | ||
} | ||
} | ||
type = getRestType(parentType, literalMembers, declaration.symbol); | ||
} | ||
else { | ||
// Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) | ||
const name = declaration.propertyName || <Identifier>declaration.name; | ||
if (isComputedNonLiteralName(name)) { | ||
// computed properties with non-literal names are treated as 'any' | ||
return anyType; | ||
} | ||
if (declaration.initializer) { | ||
getContextualType(declaration.initializer); | ||
} | ||
|
||
// Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, | ||
// or otherwise the type of the string index signature. | ||
const text = getTextOfPropertyName(name); | ||
// Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, | ||
// or otherwise the type of the string index signature. | ||
const text = getTextOfPropertyName(name); | ||
|
||
type = getTypeOfPropertyOfType(parentType, text) || | ||
isNumericLiteralName(text) && getIndexTypeOfType(parentType, IndexKind.Number) || | ||
getIndexTypeOfType(parentType, IndexKind.String); | ||
if (!type) { | ||
error(name, Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), declarationNameToString(name)); | ||
return unknownType; | ||
type = getTypeOfPropertyOfType(parentType, text) || | ||
isNumericLiteralName(text) && getIndexTypeOfType(parentType, IndexKind.Number) || | ||
getIndexTypeOfType(parentType, IndexKind.String); | ||
if (!type) { | ||
error(name, Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), declarationNameToString(name)); | ||
return unknownType; | ||
} | ||
} | ||
} | ||
else { | ||
|
@@ -3249,8 +3269,8 @@ namespace ts { | |
let hasComputedProperties = false; | ||
forEach(pattern.elements, e => { | ||
const name = e.propertyName || <Identifier>e.name; | ||
if (isComputedNonLiteralName(name)) { | ||
// do not include computed properties in the implied type | ||
if (isComputedNonLiteralName(name) || e.dotDotDotToken) { | ||
// do not include computed properties or rests in the implied type | ||
hasComputedProperties = true; | ||
return; | ||
} | ||
|
@@ -13868,7 +13888,7 @@ namespace ts { | |
error(name, Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), declarationNameToString(name)); | ||
} | ||
} | ||
else { | ||
else if (property.kind !== SyntaxKind.SpreadElementExpression) { | ||
error(property, Diagnostics.Property_assignment_expected); | ||
} | ||
} | ||
|
@@ -13914,7 +13934,7 @@ namespace ts { | |
} | ||
else { | ||
if (elementIndex < elements.length - 1) { | ||
error(element, Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); | ||
error(element, Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern); | ||
} | ||
else { | ||
const restExpression = (<SpreadExpression>element).expression; | ||
|
@@ -20814,7 +20834,7 @@ namespace ts { | |
if (node.dotDotDotToken) { | ||
const elements = (<BindingPattern>node.parent).elements; | ||
if (node !== lastOrUndefined(elements)) { | ||
return grammarErrorOnNode(node, Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); | ||
return grammarErrorOnNode(node, Diagnostics.A_rest_element_must_be_last_in_a_destructuring_pattern); | ||
} | ||
|
||
if (node.name.kind === SyntaxKind.ArrayBindingPattern || node.name.kind === SyntaxKind.ObjectBindingPattern) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,15 @@ var __assign = (this && this.__assign) || Object.assign || function(t) { | |
return t; | ||
};`; | ||
|
||
const restHelper = ` | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we will need to add this to https://github.com/Microsoft/tslib There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also you will need a check in checker.ts for __rest and __assign, and add some tests for these. |
||
var t = {}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No blank lines in the helpers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. remove empty line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && !e.indexOf(p)) | ||
t[p] = s[p]; | ||
return t; | ||
};`; | ||
|
||
// emit output for the __decorate helper function | ||
const decorateHelper = ` | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
|
@@ -226,6 +235,7 @@ const _super = (function (geti, seti) { | |
let currentFileIdentifiers: Map<string>; | ||
let extendsEmitted: boolean; | ||
let assignEmitted: boolean; | ||
let restEmitted: boolean; | ||
let decorateEmitted: boolean; | ||
let paramEmitted: boolean; | ||
let awaiterEmitted: boolean; | ||
|
@@ -2210,6 +2220,11 @@ const _super = (function (geti, seti) { | |
assignEmitted = true; | ||
} | ||
|
||
if (!restEmitted && node.flags & NodeFlags.HasRestAttribute) { | ||
writeLines(restHelper); | ||
restEmitted = true; | ||
} | ||
|
||
if (!decorateEmitted && node.flags & NodeFlags.HasDecorators) { | ||
writeLines(decorateHelper); | ||
if (compilerOptions.emitDecoratorMetadata) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure this is right. You might be walking up from the right side of a destructuring assignment, in which case the spread is actually a spread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I improved it to check that the relation between root and parent is that the object literal is the lefthand side.