Skip to content

no-duplicate-spead-property: filter by contextual type #559

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
@@ -1,6 +1,7 @@
export {};

declare function get<T>(): T;
declare function take<T>(p: T): void;

declare class WithMethods {
foo(): void;
Expand All @@ -14,7 +15,7 @@ const foo = 'foo';
x: 1,
~ [error no-duplicate-spread-property: Property 'x' is overridden later.]
...{x: 2, y: 2},
~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All properties of this object are overridden later.]
~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
y: 1,
...{x: 3},
});
Expand Down Expand Up @@ -47,7 +48,7 @@ const foo = 'foo';
[get<'foo'>()]: 1,
~~~~~~~~~~~~~~ [error no-duplicate-spread-property: Property '[get<'foo'>()]' is overridden later.]
...{[get<'foo'>()]: 2},
~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All properties of this object are overridden later.]
~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
...{[foo]: 3},
});

Expand Down Expand Up @@ -105,15 +106,15 @@ const foo = 'foo';

({
...get<WithMethods>(),
~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All properties of this object are overridden later.]
~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
foo() {},
bar: () => {},
baz: get<() => void>(),
});

({
...get<{foo: number, bar: number, baz: number}>(),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All properties of this object are overridden later.]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
foo() {},
bar: () => {},
baz: get<() => void>(),
Expand Down Expand Up @@ -256,7 +257,7 @@ var v: any;
get foo() { return 1 },
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
},
~~~~~ [error no-duplicate-spread-property: All properties of this object are overridden later.]
~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
get foo() { return 2; },
set foo(v: number) {},
});
Expand All @@ -279,7 +280,7 @@ var v: any;

({
...get<{bar: number} & Record<'foo' | 'baz', number>>(),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All properties of this object are overridden later.]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
foo: 1,
bar: 1,
baz: 1,
Expand All @@ -296,6 +297,18 @@ var v: any;
bar: 1,
});

take<{a: string}>({...get<{a: string, b: string}>(), a: ''});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object that are relevant for the target type are overridden later.]
take<Record<string, string>>({...get<{a: string, b: string}>(), a: ''});
take<{a: string} | {b: string}>({...get<{a: string, b: string}>(), a: ''});
take<{a: string} | {b: string}>({...get<{a: string, b: string}>(), a: '', b: ''});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
take<{a: string, b: string}>({...get<{a: string, b: string}>(), a: '', b: ''});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
take<{a: string}>({...get<{c: string, b: string}>(), a: ''});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: Object doesn't contribute spreadable properties relevant for the target type.]
take<{a: string}>({...get<Record<string, string>>(), a: ''});

function test<T, U extends T, V extends any, W extends object>(t: T, u: U, v: V, w: W) {
({foo: 1, ...t, ...u, ...v});
({valueOf: null, toString: null, ...w}); // make sure we don't use the apparent type
Expand All @@ -316,6 +329,11 @@ function test2<T extends Record<'foo', number>, U extends T, V extends T & Recor
({foo: 1, bar: 1, ...v});
~~~ [error no-duplicate-spread-property: Property 'foo' is overridden later.]
~~~ [error no-duplicate-spread-property: Property 'bar' is overridden later.]

take<T>({...get<{bar: string}>(), ...u});
~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: Object doesn't contribute spreadable properties relevant for the target type.]
take<T>({...get<{foo: number, bar: string}>(), ...u});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object that are relevant for the target type are overridden later.]
}

function test3<T>(t: T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const literal = {a: 1, b: 1};

({
...emptyLiteral,
~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All properties of this object are overridden later.]
~~~~~~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
a: 1,
b: 1,
});
Expand All @@ -22,7 +22,7 @@ export const literal = {a: 1, b: 1};

({
...literal,
~~~~~~~~~~ [error no-duplicate-spread-property: All properties of this object are overridden later.]
~~~~~~~~~~ [error no-duplicate-spread-property: All spreadable properties of this object are overridden later.]
a: 1,
b: 1,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {};

declare function get<T>(): T;
declare function take<T>(p: T): void;

declare class WithMethods {
foo(): void;
Expand Down Expand Up @@ -264,6 +265,14 @@ var v: any;
bar: 1,
});

take<{a: string}>({...get<{a: string, b: string}>(), a: ''});
take<Record<string, string>>({...get<{a: string, b: string}>(), a: ''});
take<{a: string} | {b: string}>({...get<{a: string, b: string}>(), a: ''});
take<{a: string} | {b: string}>({...get<{a: string, b: string}>(), a: '', b: ''});
take<{a: string, b: string}>({...get<{a: string, b: string}>(), a: '', b: ''});
take<{a: string}>({...get<{c: string, b: string}>(), a: ''});
take<{a: string}>({...get<Record<string, string>>(), a: ''});

function test<T, U extends T, V extends any, W extends object>(t: T, u: U, v: V, w: W) {
({foo: 1, ...t, ...u, ...v});
({valueOf: null, toString: null, ...w}); // make sure we don't use the apparent type
Expand All @@ -278,6 +287,9 @@ function test2<T extends Record<'foo', number>, U extends T, V extends T & Recor
({...t, ...u, foo: 1});
({foo: 1, bar: 1, ...get<T & {bar: number}>()});
({foo: 1, bar: 1, ...v});

take<T>({...get<{bar: string}>(), ...u});
take<T>({...get<{foo: number, bar: string}>(), ...u});
}

function test3<T>(t: T) {
Expand Down
81 changes: 72 additions & 9 deletions packages/mimir/src/rules/no-duplicate-spread-property.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { TypedRule, excludeDeclarationFiles, requiresCompilerOption } from '@fimbul/ymir';
import * as ts from 'typescript';
import { isReassignmentTarget, isObjectType, isClassLikeDeclaration, getPropertyName, isIntersectionType, isUnionType } from 'tsutils';
import {
isReassignmentTarget,
isObjectType,
isClassLikeDeclaration,
getPropertyName,
isIntersectionType,
isUnionType,
isUnionOrIntersectionType,
} from 'tsutils';
import { lateBoundPropertyNames } from '../utils';

interface PropertyInfo {
known: boolean;
names: ts.__String[];
assignedNames: ts.__String[];
contextFiltered: boolean;
}

const emptyPropertyInfo: PropertyInfo = {
known: false,
names: [],
assignedNames: [],
contextFiltered: false,
};

@excludeDeclarationFiles
Expand All @@ -39,13 +49,27 @@ export class Rule extends TypedRule {
const property = properties[i];
const info = this.getPropertyInfo(property);
const isAccessor = property.kind === ts.SyntaxKind.GetAccessor || property.kind === ts.SyntaxKind.SetAccessor;
if (info.known && info.names.every((name) => isAccessor ? propertiesSeen.get(name) === false : propertiesSeen.has(name))) {
if (property.kind === ts.SyntaxKind.SpreadAssignment) {
this.addFindingAtNode(property, 'All properties of this object are overridden later.');
} else {
this.addFindingAtNode(property.name, `Property '${property.name.getText(this.sourceFile)}' is overridden later.`);
if (isAccessor)
continue; // avoid overriding the isAccessor state
if (info.known) {
if (info.names.length === 0) {
this.addFindingAtNode(
property,
info.contextFiltered
? "Object doesn't contribute spreadable properties relevant for the target type."
: "Object doesn't contain any properties to spread.",
);
} else if (info.names.every((name) => isAccessor ? propertiesSeen.get(name) === false : propertiesSeen.has(name))) {
if (property.kind === ts.SyntaxKind.SpreadAssignment) {
this.addFindingAtNode(
property,
`All spreadable properties of this object${
info.contextFiltered ? ' that are relevant for the target type' : ''
} are overridden later.`,
);
} else {
this.addFindingAtNode(property.name, `Property '${property.name.getText(this.sourceFile)}' is overridden later.`);
if (isAccessor)
continue; // avoid overriding the isAccessor state
}
}
}
for (const name of info.assignedNames)
Expand All @@ -62,6 +86,7 @@ export class Rule extends TypedRule {
known: true,
names: [property.name.escapedText],
assignedNames: [property.name.escapedText],
contextFiltered: false,
};
default: {
const staticName = getPropertyName(property.name);
Expand All @@ -71,6 +96,7 @@ export class Rule extends TypedRule {
known: true,
names: [escapedName],
assignedNames: [escapedName],
contextFiltered: false,
};
}
const lateBound = lateBoundPropertyNames((<ts.ComputedPropertyName>property.name).expression, this.checker);
Expand All @@ -81,16 +107,50 @@ export class Rule extends TypedRule {
names,
known: true,
assignedNames: names.length !== 1 ? [] : names, // if the computed name is a union, it's not sure which will be assigned
contextFiltered: false,
};
}
}
}

private getPropertyInfoFromSpread(node: ts.Expression): PropertyInfo {
return getPropertyInfoFromType(this.checker.getTypeAtLocation(node)!);
const spreadProperties = getPropertyInfoFromType(this.checker.getTypeAtLocation(node)!);
if (!spreadProperties.known)
return spreadProperties;
const contextualType = this.checker.getContextualType(node);
Copy link
Member Author

Choose a reason for hiding this comment

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

this should be done once for the object literal and not once per SpreadAssignment

if (contextualType === undefined)
return spreadProperties;
const contextProperties = getContextualProperties(contextualType);
if (!contextProperties.known)
return spreadProperties;
const relevantProperties = spreadProperties.names.filter((prop) => contextProperties.names.includes(prop));
if (relevantProperties.length === spreadProperties.names.length)
return spreadProperties;
return {
...spreadProperties,
names: relevantProperties,
contextFiltered: true,
};
}
}

function getContextualProperties(type: ts.Type): PropertyInfo {
if (isUnionOrIntersectionType(type))
return type.types.map(getContextualProperties).reduce(intersectPropertyInfo);
if (type.flags & ts.TypeFlags.Instantiable) {
const constraint = type.getConstraint();
return constraint === undefined ? emptyPropertyInfo : getContextualProperties(constraint);
}
if (!isObjectType(type))
return emptyPropertyInfo;
return {
known: type.getStringIndexType() === undefined && type.getNumberIndexType() === undefined,
names: type.getProperties().map((prop) => prop.escapedName),
assignedNames: [],
contextFiltered: false,
};
}

function getPropertyInfoFromType(type: ts.Type): PropertyInfo {
if (isUnionType(type))
return type.types.map(getPropertyInfoFromType).reduce(unionPropertyInfo);
Expand All @@ -108,6 +168,7 @@ function getPropertyInfoFromType(type: ts.Type): PropertyInfo {
known: type.getStringIndexType() === undefined && type.getNumberIndexType() === undefined,
names: [],
assignedNames: [],
contextFiltered: false,
};
for (const prop of type.getProperties()) {
if (!isSpreadableProperty(prop))
Expand All @@ -131,6 +192,7 @@ function unionPropertyInfo(a: PropertyInfo, b: PropertyInfo): PropertyInfo {
known: a.known && b.known,
names: [...a.names, ...b.names],
assignedNames: a.assignedNames.filter((name) => b.assignedNames.includes(name)),
contextFiltered: false,
};
}

Expand All @@ -139,5 +201,6 @@ function intersectPropertyInfo(a: PropertyInfo, b: PropertyInfo): PropertyInfo {
known: a.known && b.known,
names: [...a.names, ...b.names],
assignedNames: [...a.assignedNames, ...b.assignedNames],
contextFiltered: false,
};
}
12 changes: 12 additions & 0 deletions packages/mimir/test/no-duplicate-spread-property/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {};

declare function get<T>(): T;
declare function take<T>(p: T): void;

declare class WithMethods {
foo(): void;
Expand Down Expand Up @@ -264,6 +265,14 @@ var v: any;
bar: 1,
});

take<{a: string}>({...get<{a: string, b: string}>(), a: ''});
take<Record<string, string>>({...get<{a: string, b: string}>(), a: ''});
take<{a: string} | {b: string}>({...get<{a: string, b: string}>(), a: ''});
take<{a: string} | {b: string}>({...get<{a: string, b: string}>(), a: '', b: ''});
take<{a: string, b: string}>({...get<{a: string, b: string}>(), a: '', b: ''});
take<{a: string}>({...get<{c: string, b: string}>(), a: ''});
take<{a: string}>({...get<Record<string, string>>(), a: ''});

function test<T, U extends T, V extends any, W extends object>(t: T, u: U, v: V, w: W) {
({foo: 1, ...t, ...u, ...v});
({valueOf: null, toString: null, ...w}); // make sure we don't use the apparent type
Expand All @@ -278,6 +287,9 @@ function test2<T extends Record<'foo', number>, U extends T, V extends T & Recor
({...t, ...u, foo: 1});
({foo: 1, bar: 1, ...get<T & {bar: number}>()});
({foo: 1, bar: 1, ...v});

take<T>({...get<{bar: string}>(), ...u});
take<T>({...get<{foo: number, bar: string}>(), ...u});
}

function test3<T>(t: T) {
Expand Down