From 3631af6486b91bd01d62fc346dd1e8cf39683b77 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 27 Apr 2018 14:54:59 -0700 Subject: [PATCH] Remove readonly from object rest properties (#23746) * Remove readonly from object rest properties Works the same as removing it from object spread properties * Fix bug number in test --- src/compiler/checker.ts | 2 +- .../baselines/reference/objectRestReadonly.js | 36 +++++++++++++++ .../reference/objectRestReadonly.symbols | 40 +++++++++++++++++ .../reference/objectRestReadonly.types | 45 +++++++++++++++++++ .../types/rest/objectRestReadonly.ts | 16 +++++++ 5 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/objectRestReadonly.js create mode 100644 tests/baselines/reference/objectRestReadonly.symbols create mode 100644 tests/baselines/reference/objectRestReadonly.types create mode 100644 tests/cases/conformance/types/rest/objectRestReadonly.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0819ece651e..018539b1b0e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4180,7 +4180,7 @@ namespace ts { const isPrivate = getDeclarationModifierFlagsFromSymbol(prop) & (ModifierFlags.Private | ModifierFlags.Protected); const isSetOnlyAccessor = prop.flags & SymbolFlags.SetAccessor && !(prop.flags & SymbolFlags.GetAccessor); if (!inNamesToRemove && !isPrivate && !isClassMethod(prop) && !isSetOnlyAccessor) { - members.set(prop.escapedName, prop); + members.set(prop.escapedName, getNonReadonlySymbol(prop)); } } const stringIndexInfo = getIndexInfoOfType(source, IndexKind.String); diff --git a/tests/baselines/reference/objectRestReadonly.js b/tests/baselines/reference/objectRestReadonly.js new file mode 100644 index 00000000000..a940dbf52cc --- /dev/null +++ b/tests/baselines/reference/objectRestReadonly.js @@ -0,0 +1,36 @@ +//// [objectRestReadonly.ts] +// #23734 +type ObjType = { + foo: string + baz: string + quux: string +} + +const obj: Readonly = { + foo: 'bar', + baz: 'qux', + quux: 'quuz', +} + +const { foo, ...rest } = obj + +delete rest.baz + + +//// [objectRestReadonly.js] +var __rest = (this && this.__rest) || function (s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) + t[p[i]] = s[p[i]]; + return t; +}; +var obj = { + foo: 'bar', + baz: 'qux', + quux: 'quuz' +}; +var foo = obj.foo, rest = __rest(obj, ["foo"]); +delete rest.baz; diff --git a/tests/baselines/reference/objectRestReadonly.symbols b/tests/baselines/reference/objectRestReadonly.symbols new file mode 100644 index 00000000000..91c95f915e1 --- /dev/null +++ b/tests/baselines/reference/objectRestReadonly.symbols @@ -0,0 +1,40 @@ +=== tests/cases/conformance/types/rest/objectRestReadonly.ts === +// #23734 +type ObjType = { +>ObjType : Symbol(ObjType, Decl(objectRestReadonly.ts, 0, 0)) + + foo: string +>foo : Symbol(foo, Decl(objectRestReadonly.ts, 1, 16)) + + baz: string +>baz : Symbol(baz, Decl(objectRestReadonly.ts, 2, 13)) + + quux: string +>quux : Symbol(quux, Decl(objectRestReadonly.ts, 3, 13)) +} + +const obj: Readonly = { +>obj : Symbol(obj, Decl(objectRestReadonly.ts, 7, 5)) +>Readonly : Symbol(Readonly, Decl(lib.d.ts, --, --)) +>ObjType : Symbol(ObjType, Decl(objectRestReadonly.ts, 0, 0)) + + foo: 'bar', +>foo : Symbol(foo, Decl(objectRestReadonly.ts, 7, 32)) + + baz: 'qux', +>baz : Symbol(baz, Decl(objectRestReadonly.ts, 8, 13)) + + quux: 'quuz', +>quux : Symbol(quux, Decl(objectRestReadonly.ts, 9, 13)) +} + +const { foo, ...rest } = obj +>foo : Symbol(foo, Decl(objectRestReadonly.ts, 13, 7)) +>rest : Symbol(rest, Decl(objectRestReadonly.ts, 13, 12)) +>obj : Symbol(obj, Decl(objectRestReadonly.ts, 7, 5)) + +delete rest.baz +>rest.baz : Symbol(baz, Decl(objectRestReadonly.ts, 2, 13)) +>rest : Symbol(rest, Decl(objectRestReadonly.ts, 13, 12)) +>baz : Symbol(baz, Decl(objectRestReadonly.ts, 2, 13)) + diff --git a/tests/baselines/reference/objectRestReadonly.types b/tests/baselines/reference/objectRestReadonly.types new file mode 100644 index 00000000000..30fd21e896b --- /dev/null +++ b/tests/baselines/reference/objectRestReadonly.types @@ -0,0 +1,45 @@ +=== tests/cases/conformance/types/rest/objectRestReadonly.ts === +// #23734 +type ObjType = { +>ObjType : ObjType + + foo: string +>foo : string + + baz: string +>baz : string + + quux: string +>quux : string +} + +const obj: Readonly = { +>obj : Readonly +>Readonly : Readonly +>ObjType : ObjType +>{ foo: 'bar', baz: 'qux', quux: 'quuz',} : { foo: string; baz: string; quux: string; } + + foo: 'bar', +>foo : string +>'bar' : "bar" + + baz: 'qux', +>baz : string +>'qux' : "qux" + + quux: 'quuz', +>quux : string +>'quuz' : "quuz" +} + +const { foo, ...rest } = obj +>foo : string +>rest : { baz: string; quux: string; } +>obj : Readonly + +delete rest.baz +>delete rest.baz : boolean +>rest.baz : string +>rest : { baz: string; quux: string; } +>baz : string + diff --git a/tests/cases/conformance/types/rest/objectRestReadonly.ts b/tests/cases/conformance/types/rest/objectRestReadonly.ts new file mode 100644 index 00000000000..be600620348 --- /dev/null +++ b/tests/cases/conformance/types/rest/objectRestReadonly.ts @@ -0,0 +1,16 @@ +// #23734 +type ObjType = { + foo: string + baz: string + quux: string +} + +const obj: Readonly = { + foo: 'bar', + baz: 'qux', + quux: 'quuz', +} + +const { foo, ...rest } = obj + +delete rest.baz