Skip to content
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

Fix 8482: Add Object.values and Object.entries #8538

Merged
merged 9 commits into from
May 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ var es2016LibrarySourceMap = es2016LibrarySource.map(function(source) {
return { target: "lib." + source, sources: ["header.d.ts", source] };
})

var es2017LibrarySource = ["es2017.object.d.ts"];

var es2017LibrarySourceMap = es2017LibrarySource.map(function(source) {
return { target: "lib." + source, sources: ["header.d.ts", source] };
})

var hostsLibrarySources = ["dom.generated.d.ts", "webworker.importscripts.d.ts", "scripthost.d.ts"]

var librarySourceMap = [
Expand All @@ -200,11 +206,12 @@ var librarySourceMap = [
{ target: "lib.es5.d.ts", sources: ["header.d.ts", "es5.d.ts"] },
{ target: "lib.es2015.d.ts", sources: ["header.d.ts", "es2015.d.ts"] },
{ target: "lib.es2016.d.ts", sources: ["header.d.ts", "es2016.d.ts"] },
{ target: "lib.es2017.d.ts", sources: ["header.d.ts", "es2017.d.ts"] },

// JavaScript + all host library
{ target: "lib.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(hostsLibrarySources), },
{ target: "lib.es6.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(es2015LibrarySources, hostsLibrarySources, "dom.iterable.d.ts"), },
].concat(es2015LibrarySourceMap, es2016LibrarySourceMap);
].concat(es2015LibrarySourceMap, es2016LibrarySourceMap, es2017LibrarySourceMap);

var libraryTargets = librarySourceMap.map(function (f) {
return path.join(builtLocalDirectory, f.target);
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ namespace ts {
"es2015": "lib.es2015.d.ts",
"es7": "lib.es2016.d.ts",
"es2016": "lib.es2016.d.ts",
"es2017": "lib.es2017.d.ts",
// Host only
"dom": "lib.dom.d.ts",
"webworker": "lib.webworker.d.ts",
Expand All @@ -389,7 +390,8 @@ namespace ts {
"es2015.reflect": "lib.es2015.reflect.d.ts",
"es2015.symbol": "lib.es2015.symbol.d.ts",
"es2015.symbol.wellknown": "lib.es2015.symbol.wellknown.d.ts",
"es2016.array.include": "lib.es2016.array.include.d.ts"
"es2016.array.include": "lib.es2016.array.include.d.ts",
"es2017.object": "lib.es2017.object.d.ts"
},
},
description: Diagnostics.Specify_library_files_to_be_included_in_the_compilation_Colon
Expand Down
2 changes: 2 additions & 0 deletions src/lib/es2017.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference path="lib.es2016.d.ts" />
/// <reference path="lib.es2017.object.d.ts" />
14 changes: 14 additions & 0 deletions src/lib/es2017.object.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface ObjectConstructor {
/**
* Returns an array of values of the enumerable properties of an object
* @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
*/
values<T>(o: { [s: string]: T }): T[];
values(o: any): any[];
/**
* Returns an array of key/values of the enumerable properties of an object
* @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
*/
entries<T>(o: { [s: string]: T }): [string, T][];
entries(o: any): [string, any][];
}
19 changes: 19 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [useObjectValuesAndEntries1.ts]

var o = { a: 1, b: 2 };

for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);
var entries1 = Object.entries(1); // <-- entries: [string, any][]

//// [useObjectValuesAndEntries1.js]
var o = { a: 1, b: 2 };
for (var _i = 0, _a = Object.values(o); _i < _a.length; _i++) {
var x = _a[_i];
var y = x;
}
var entries = Object.entries(o);
var entries1 = Object.entries(1); // <-- entries: [string, any][]
32 changes: 32 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/conformance/es2017/useObjectValuesAndEntries1.ts ===

var o = { a: 1, b: 2 };
>o : Symbol(o, Decl(useObjectValuesAndEntries1.ts, 1, 3))
>a : Symbol(a, Decl(useObjectValuesAndEntries1.ts, 1, 9))
>b : Symbol(b, Decl(useObjectValuesAndEntries1.ts, 1, 15))

for (var x of Object.values(o)) {
>x : Symbol(x, Decl(useObjectValuesAndEntries1.ts, 3, 8))
>Object.values : Symbol(ObjectConstructor.values, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>values : Symbol(ObjectConstructor.values, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>o : Symbol(o, Decl(useObjectValuesAndEntries1.ts, 1, 3))

let y = x;
>y : Symbol(y, Decl(useObjectValuesAndEntries1.ts, 4, 7))
>x : Symbol(x, Decl(useObjectValuesAndEntries1.ts, 3, 8))
}

var entries = Object.entries(o);
>entries : Symbol(entries, Decl(useObjectValuesAndEntries1.ts, 7, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>o : Symbol(o, Decl(useObjectValuesAndEntries1.ts, 1, 3))

var entries1 = Object.entries(1); // <-- entries: [string, any][]
>entries1 : Symbol(entries1, Decl(useObjectValuesAndEntries1.ts, 8, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))

39 changes: 39 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/conformance/es2017/useObjectValuesAndEntries1.ts ===

var o = { a: 1, b: 2 };
>o : { a: number; b: number; }
>{ a: 1, b: 2 } : { a: number; b: number; }
>a : number
>1 : number
>b : number
>2 : number

for (var x of Object.values(o)) {
>x : number
>Object.values(o) : number[]
>Object.values : { <T>(o: { [s: string]: T; }): T[]; (o: any): any[]; }
>Object : ObjectConstructor
>values : { <T>(o: { [s: string]: T; }): T[]; (o: any): any[]; }
>o : { a: number; b: number; }

let y = x;
>y : number
>x : number
}

var entries = Object.entries(o);
>entries : [string, number][]
>Object.entries(o) : [string, number][]
>Object.entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>o : { a: number; b: number; }

var entries1 = Object.entries(1); // <-- entries: [string, any][]
>entries1 : [string, any][]
>Object.entries(1) : [string, any][]
>Object.entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>1 : number

17 changes: 17 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/conformance/es2017/useObjectValuesAndEntries2.ts(4,22): error TS2339: Property 'values' does not exist on type 'ObjectConstructor'.
tests/cases/conformance/es2017/useObjectValuesAndEntries2.ts(8,22): error TS2339: Property 'entries' does not exist on type 'ObjectConstructor'.


==== tests/cases/conformance/es2017/useObjectValuesAndEntries2.ts (2 errors) ====

var o = { a: 1, b: 2 };

for (var x of Object.values(o)) {
~~~~~~
!!! error TS2339: Property 'values' does not exist on type 'ObjectConstructor'.
let y = x;
}

var entries = Object.entries(o);
~~~~~~~
!!! error TS2339: Property 'entries' does not exist on type 'ObjectConstructor'.
17 changes: 17 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [useObjectValuesAndEntries2.ts]

var o = { a: 1, b: 2 };

for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);

//// [useObjectValuesAndEntries2.js]
var o = { a: 1, b: 2 };
for (var _i = 0, _a = Object.values(o); _i < _a.length; _i++) {
var x = _a[_i];
var y = x;
}
var entries = Object.entries(o);
17 changes: 17 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/conformance/es2017/useObjectValuesAndEntries3.ts(4,22): error TS2339: Property 'values' does not exist on type 'ObjectConstructor'.
tests/cases/conformance/es2017/useObjectValuesAndEntries3.ts(8,22): error TS2339: Property 'entries' does not exist on type 'ObjectConstructor'.


==== tests/cases/conformance/es2017/useObjectValuesAndEntries3.ts (2 errors) ====

var o = { a: 1, b: 2 };

for (var x of Object.values(o)) {
~~~~~~
!!! error TS2339: Property 'values' does not exist on type 'ObjectConstructor'.
let y = x;
}

var entries = Object.entries(o);
~~~~~~~
!!! error TS2339: Property 'entries' does not exist on type 'ObjectConstructor'.
16 changes: 16 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [useObjectValuesAndEntries3.ts]

var o = { a: 1, b: 2 };

for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);

//// [useObjectValuesAndEntries3.js]
var o = { a: 1, b: 2 };
for (var x of Object.values(o)) {
let y = x;
}
var entries = Object.entries(o);
16 changes: 16 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [useObjectValuesAndEntries4.ts]

var o = { a: 1, b: 2 };

for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);

//// [useObjectValuesAndEntries4.js]
var o = { a: 1, b: 2 };
for (var x of Object.values(o)) {
let y = x;
}
var entries = Object.entries(o);
26 changes: 26 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries4.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/conformance/es2017/useObjectValuesAndEntries4.ts ===

var o = { a: 1, b: 2 };
>o : Symbol(o, Decl(useObjectValuesAndEntries4.ts, 1, 3))
>a : Symbol(a, Decl(useObjectValuesAndEntries4.ts, 1, 9))
>b : Symbol(b, Decl(useObjectValuesAndEntries4.ts, 1, 15))

for (var x of Object.values(o)) {
>x : Symbol(x, Decl(useObjectValuesAndEntries4.ts, 3, 8))
>Object.values : Symbol(ObjectConstructor.values, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>values : Symbol(ObjectConstructor.values, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>o : Symbol(o, Decl(useObjectValuesAndEntries4.ts, 1, 3))

let y = x;
>y : Symbol(y, Decl(useObjectValuesAndEntries4.ts, 4, 7))
>x : Symbol(x, Decl(useObjectValuesAndEntries4.ts, 3, 8))
}

var entries = Object.entries(o);
>entries : Symbol(entries, Decl(useObjectValuesAndEntries4.ts, 7, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>o : Symbol(o, Decl(useObjectValuesAndEntries4.ts, 1, 3))

31 changes: 31 additions & 0 deletions tests/baselines/reference/useObjectValuesAndEntries4.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== tests/cases/conformance/es2017/useObjectValuesAndEntries4.ts ===

var o = { a: 1, b: 2 };
>o : { a: number; b: number; }
>{ a: 1, b: 2 } : { a: number; b: number; }
>a : number
>1 : number
>b : number
>2 : number

for (var x of Object.values(o)) {
>x : number
>Object.values(o) : number[]
>Object.values : { <T>(o: { [s: string]: T; }): T[]; (o: any): any[]; }
>Object : ObjectConstructor
>values : { <T>(o: { [s: string]: T; }): T[]; (o: any): any[]; }
>o : { a: number; b: number; }

let y = x;
>y : number
>x : number
}

var entries = Object.entries(o);
>entries : [string, number][]
>Object.entries(o) : [string, number][]
>Object.entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>o : { a: number; b: number; }

11 changes: 11 additions & 0 deletions tests/cases/conformance/es2017/useObjectValuesAndEntries1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @target: es5
// @lib: es5,es2017.object

var o = { a: 1, b: 2 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you have an example of a mixed-type object that uses the any-based overload as well? Something like

var o2 = { a: 'first', b: 2 };
var entries = Object.entries(o); // <-- entries: [string, any][]


for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);
var entries1 = Object.entries(1); // <-- entries: [string, any][]
10 changes: 10 additions & 0 deletions tests/cases/conformance/es2017/useObjectValuesAndEntries2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @target: es5
// @lib: es5

var o = { a: 1, b: 2 };

for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);
9 changes: 9 additions & 0 deletions tests/cases/conformance/es2017/useObjectValuesAndEntries3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @target: es6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// @lib: ess6,es2017.object would make this file compile without errors, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep!


var o = { a: 1, b: 2 };

for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);
10 changes: 10 additions & 0 deletions tests/cases/conformance/es2017/useObjectValuesAndEntries4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @target: es6
// @lib: es2017

var o = { a: 1, b: 2 };

for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);
8 changes: 4 additions & 4 deletions tests/cases/unittests/commandLineParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace ts {
assertParseResult(["--lib", "es5,invalidOption", "0.ts"],
{
errors: [{
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'dom', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include'",
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'dom', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object'",
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,

Expand Down Expand Up @@ -217,7 +217,7 @@ namespace ts {
start: undefined,
length: undefined,
}, {
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'dom', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include'",
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'dom', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object'",
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,

Expand All @@ -237,7 +237,7 @@ namespace ts {
assertParseResult(["--lib", "es5,", "es7", "0.ts"],
{
errors: [{
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'dom', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include'",
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'dom', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object'",
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,

Expand All @@ -257,7 +257,7 @@ namespace ts {
assertParseResult(["--lib", "es5, ", "es7", "0.ts"],
{
errors: [{
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'dom', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include'",
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'dom', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object'",
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,

Expand Down
Loading