-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathno-duplicate-spread-property.ts
137 lines (128 loc) · 5.57 KB
/
no-duplicate-spread-property.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { TypedRule, excludeDeclarationFiles, requiresCompilerOption } from '@fimbul/ymir';
import * as ts from 'typescript';
import { isReassignmentTarget, isObjectType, unionTypeParts, isClassLikeDeclaration, getPropertyName, isIntersectionType } from 'tsutils';
import { lateBoundPropertyNames } from '../utils';
interface PropertyInfo {
known: boolean;
names: ts.__String[];
assignedNames: ts.__String[];
}
const emptyPropertyInfo: PropertyInfo = {
known: false,
names: [],
assignedNames: [],
};
@excludeDeclarationFiles
@requiresCompilerOption('strictNullChecks')
export class Rule extends TypedRule {
public apply() {
const checkedObjects = new Set<number>();
for (const node of this.context.getFlatAst()) {
if (
node.kind === ts.SyntaxKind.SpreadAssignment &&
!checkedObjects.has(node.parent!.pos) &&
!isReassignmentTarget(<ts.ObjectLiteralExpression>node.parent)
) {
checkedObjects.add(node.parent!.pos);
this.checkObject(<ts.ObjectLiteralExpression>node.parent);
}
}
}
private checkObject({properties}: ts.ObjectLiteralExpression) {
/** key: propertyName, value: isAccessor */
const propertiesSeen = new Map<ts.__String, boolean>();
for (let i = properties.length - 1; i >= 0; --i) {
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.addFailureAtNode(property, 'All properties of this object are overridden later.');
} else {
this.addFailureAtNode(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)
propertiesSeen.set(name, isAccessor);
}
}
private getPropertyInfo(property: ts.ObjectLiteralElementLike): PropertyInfo {
switch (property.kind) {
case ts.SyntaxKind.SpreadAssignment:
return this.getPropertyInfoFromSpread(property.expression);
case ts.SyntaxKind.ShorthandPropertyAssignment:
return {
known: true,
names: [property.name.escapedText],
assignedNames: [property.name.escapedText],
};
default: {
const staticName = getPropertyName(property.name);
if (staticName !== undefined) {
const escapedName = ts.escapeLeadingUnderscores(staticName);
return {
known: true,
names: [escapedName],
assignedNames: [escapedName],
};
}
const lateBound = lateBoundPropertyNames((<ts.ComputedPropertyName>property.name).expression, this.checker);
if (!lateBound.known)
return emptyPropertyInfo;
const names = lateBound.properties.map((p) => p.symbolName);
return {
names,
known: true,
assignedNames: names.length !== 1 ? [] : names, // if the computed name is a union, it's not sure which will be assigned
};
}
}
}
private getPropertyInfoFromSpread(node: ts.Expression): PropertyInfo {
const type = this.checker.getTypeAtLocation(node)!;
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 = {
known: (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0 ||
type.getStringIndexType() === undefined && type.getNumberIndexType() === undefined,
names: [],
assignedNames: [],
};
for (const prop of type.getProperties()) {
if (!isSpreadableProperty(prop))
continue;
if ((prop.flags & ts.SymbolFlags.Optional) === 0)
result.assignedNames.push(prop.escapedName);
result.names.push(prop.escapedName);
}
return result;
}
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 false;
return true;
}
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],
};
}