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

no-duplicate-spread-property: handle intersection types #449

Merged
merged 2 commits into from
Oct 30, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,38 @@ var v: any;
get foo() { return 2; },
set foo(v: number) {},
});

({
foo: 1,
~~~ [error no-duplicate-spread-property: Property 'foo' is overridden later.]
bar: 1,
~~~ [error no-duplicate-spread-property: Property 'bar' is overridden later.]
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
bas: 1,
});

({
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
foo: 1,
bar: 1,
bas: 1,
});

({
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All properties of this object are overridden later.]
foo: 1,
bar: 1,
baz: 1,
});

({
bar: 1,
~~~ [error no-duplicate-spread-property: Property 'bar' is overridden later.]
...get<{bar: number} & Record<string, number>>(),
});

({
...get<{bar: number} & Record<string, number>>(),
bar: 1,
});
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,34 @@ var v: any;
get foo() { return 2; },
set foo(v: number) {},
});

({
foo: 1,
bar: 1,
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
bas: 1,
});

({
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
foo: 1,
bar: 1,
bas: 1,
});

({
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
foo: 1,
bar: 1,
baz: 1,
});

({
bar: 1,
...get<{bar: number} & Record<string, number>>(),
});

({
...get<{bar: number} & Record<string, number>>(),
bar: 1,
});
16 changes: 13 additions & 3 deletions packages/mimir/src/rules/no-duplicate-spread-property.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TypedRule, excludeDeclarationFiles, requiresCompilerOption } from '@fimbul/ymir';
import * as ts from 'typescript';
import { isReassignmentTarget, isObjectType, unionTypeParts, isClassLikeDeclaration, getPropertyName } from 'tsutils';
import { isReassignmentTarget, isObjectType, unionTypeParts, isClassLikeDeclaration, getPropertyName, isIntersectionType } from 'tsutils';
import { lateBoundPropertyNames } from '../utils';

interface PropertyInfo {
Expand Down Expand Up @@ -88,11 +88,13 @@ export class Rule extends TypedRule {

private getPropertyInfoFromSpread(node: ts.Expression): PropertyInfo {
const type = this.checker.getTypeAtLocation(node)!;
return unionTypeParts(type).map(getPropertyInfoFromType).reduce(combinePropertyInfo);
return unionTypeParts(type).map(getPropertyInfoFromType).reduce(unionPropertyInfo);
}
}

function getPropertyInfoFromType(type: ts.Type): PropertyInfo {
if (isIntersectionType(type))
return type.types.map(getPropertyInfoFromType).reduce(intersectPropertyInfo);
if (!isObjectType(type))
return emptyPropertyInfo;
const result: PropertyInfo = {
Expand All @@ -118,10 +120,18 @@ function isSpreadableProperty(prop: ts.Symbol): boolean | undefined {
return true;
}

function combinePropertyInfo(a: PropertyInfo, b: PropertyInfo): PropertyInfo {
function unionPropertyInfo(a: PropertyInfo, b: PropertyInfo): PropertyInfo {
return {
known: a.known && b.known,
names: [...a.names, ...b.names],
assignedNames: a.assignedNames.filter((name) => b.assignedNames.includes(name)),
};
}

function intersectPropertyInfo(a: PropertyInfo, b: PropertyInfo): PropertyInfo {
return {
known: a.known && b.known,
names: [...a.names, ...b.names],
assignedNames: [...a.assignedNames, ...b.assignedNames],
};
}
31 changes: 31 additions & 0 deletions packages/mimir/test/no-duplicate-spread-property/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,34 @@ var v: any;
get foo() { return 2; },
set foo(v: number) {},
});

({
foo: 1,
bar: 1,
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
bas: 1,
});

({
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
foo: 1,
bar: 1,
bas: 1,
});

({
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
foo: 1,
bar: 1,
baz: 1,
});

({
bar: 1,
...get<{bar: number} & Record<string, number>>(),
});

({
...get<{bar: number} & Record<string, number>>(),
bar: 1,
});