Skip to content

Commit

Permalink
no-duplicate-spread-property: exclude class Getters and Setters (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored Aug 10, 2018
1 parent 92e7ca5 commit b6194b3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
12 changes: 6 additions & 6 deletions packages/mimir/src/rules/no-duplicate-spread-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,20 @@ 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);
result.names.push(prop.escapedName);
}
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 {
Expand Down
34 changes: 34 additions & 0 deletions packages/mimir/test/no-duplicate-spread-property/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});

0 comments on commit b6194b3

Please sign in to comment.