From b6194b314c6fa53b39c1654da8d1a7ffbb1bff0b Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Fri, 10 Aug 2018 10:01:37 +0200 Subject: [PATCH] no-duplicate-spread-property: exclude class Getters and Setters (#373) --- .../default/test.ts.lint | 37 +++++++++++++++++++ .../loose/test.ts.lint | 34 +++++++++++++++++ .../src/rules/no-duplicate-spread-property.ts | 12 +++--- .../test/no-duplicate-spread-property/test.ts | 34 +++++++++++++++++ 4 files changed, 111 insertions(+), 6 deletions(-) diff --git a/baselines/packages/mimir/test/no-duplicate-spread-property/default/test.ts.lint b/baselines/packages/mimir/test/no-duplicate-spread-property/default/test.ts.lint index b5e10d0ba..2f44f773f 100644 --- a/baselines/packages/mimir/test/no-duplicate-spread-property/default/test.ts.lint +++ b/baselines/packages/mimir/test/no-duplicate-spread-property/default/test.ts.lint @@ -194,3 +194,40 @@ var v: any; bar: 2, }, }); + +({ + foo: 1, + ...new class { + get foo() { return 1; } + set foo(v) {} + bar = 1; + }, +}); + +({ + foo: 1, + ~~~ [error no-duplicate-spread-property: Property 'foo' is overridden later.] + ...{ + get foo() { return 1; }, + bar: 2, + }, +}); + +({ + foo: 1, + ~~~ [error no-duplicate-spread-property: Property 'foo' is overridden later.] + ...{ + get foo() { return 1; }, + set foo(v: number) {}, + bar: 2, + }, +}); + +({ + foo: 1, + ~~~ [error no-duplicate-spread-property: Property 'foo' is overridden later.] + ...{ + set foo(v: number) {}, + bar: 2, + }, +}); diff --git a/baselines/packages/mimir/test/no-duplicate-spread-property/loose/test.ts.lint b/baselines/packages/mimir/test/no-duplicate-spread-property/loose/test.ts.lint index 71b55ce0c..c25c3b7e0 100644 --- a/baselines/packages/mimir/test/no-duplicate-spread-property/loose/test.ts.lint +++ b/baselines/packages/mimir/test/no-duplicate-spread-property/loose/test.ts.lint @@ -174,3 +174,37 @@ var v: any; bar: 2, }, }); + +({ + foo: 1, + ...new class { + get foo() { return 1; } + set foo(v) {} + bar = 1; + }, +}); + +({ + foo: 1, + ...{ + get foo() { return 1; }, + bar: 2, + }, +}); + +({ + foo: 1, + ...{ + get foo() { return 1; }, + set foo(v: number) {}, + bar: 2, + }, +}); + +({ + foo: 1, + ...{ + set foo(v: number) {}, + bar: 2, + }, +}); diff --git a/packages/mimir/src/rules/no-duplicate-spread-property.ts b/packages/mimir/src/rules/no-duplicate-spread-property.ts index 007a15593..a98608821 100644 --- a/packages/mimir/src/rules/no-duplicate-spread-property.ts +++ b/packages/mimir/src/rules/no-duplicate-spread-property.ts @@ -98,7 +98,7 @@ function getPropertyInfoFromType(type: ts.Type): PropertyInfo { assignedNames: [], }; for (const prop of type.getProperties()) { - if (isClassMethod(prop)) + if (!isSpreadableProperty(prop)) continue; if ((prop.flags & ts.SymbolFlags.Optional) === 0) result.assignedNames.push(prop.escapedName); @@ -106,12 +106,12 @@ function getPropertyInfoFromType(type: ts.Type): PropertyInfo { } return result; } -function isClassMethod(prop: ts.Symbol): boolean | undefined { - if (prop.flags & ts.SymbolFlags.Method && prop.declarations !== undefined) - for (const declaration of prop.declarations) +function isSpreadableProperty(prop: ts.Symbol): boolean | undefined { + if (prop.flags & (ts.SymbolFlags.Method | ts.SymbolFlags.Accessor)) + for (const declaration of prop.declarations!) if (isClassLikeDeclaration(declaration.parent!)) - return true; - return false; + return false; + return true; } function combinePropertyInfo(a: PropertyInfo, b: PropertyInfo): PropertyInfo { diff --git a/packages/mimir/test/no-duplicate-spread-property/test.ts b/packages/mimir/test/no-duplicate-spread-property/test.ts index 71b55ce0c..c25c3b7e0 100644 --- a/packages/mimir/test/no-duplicate-spread-property/test.ts +++ b/packages/mimir/test/no-duplicate-spread-property/test.ts @@ -174,3 +174,37 @@ var v: any; bar: 2, }, }); + +({ + foo: 1, + ...new class { + get foo() { return 1; } + set foo(v) {} + bar = 1; + }, +}); + +({ + foo: 1, + ...{ + get foo() { return 1; }, + bar: 2, + }, +}); + +({ + foo: 1, + ...{ + get foo() { return 1; }, + set foo(v: number) {}, + bar: 2, + }, +}); + +({ + foo: 1, + ...{ + set foo(v: number) {}, + bar: 2, + }, +});