Skip to content

Commit

Permalink
feat(typescript-estree): tighten prop name and destructure types (#1346)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher authored Dec 18, 2019
1 parent 9cd5815 commit f335c50
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export default util.createRule({
case AST_NODE_TYPES.FunctionDeclaration:
return member.id && member.id.name;
case AST_NODE_TYPES.TSMethodSignature:
return util.getNameFromPropertyName(member.key);
return util.getNameFromMember(member, sourceCode);
case AST_NODE_TYPES.TSCallSignatureDeclaration:
return 'call';
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return 'new';
case AST_NODE_TYPES.MethodDefinition:
return util.getNameFromClassMember(member, sourceCode);
return util.getNameFromMember(member, sourceCode);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ export default util.createRule<Options, MessageIds>({
break;
}

const methodName = util.getNameFromClassMember(
methodDefinition,
sourceCode,
);
const methodName = util.getNameFromMember(methodDefinition, sourceCode);

if (check === 'off' || ignoredMethodNames.has(methodName)) {
return;
Expand Down Expand Up @@ -163,7 +160,7 @@ export default util.createRule<Options, MessageIds>({
): void {
const nodeType = 'class property';

const propertyName = util.getNameFromPropertyName(classProperty.key);
const propertyName = util.getNameFromMember(classProperty, sourceCode);
if (
propCheck === 'no-public' &&
classProperty.accessibility === 'public'
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/member-naming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default util.createRule<Options, MessageIds>({

validate(
node.key,
util.getNameFromClassMember(node, sourceCode),
util.getNameFromMember(node, sourceCode),
node.accessibility,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ export default util.createRule<Options, MessageIds>({
case AST_NODE_TYPES.TSMethodSignature:
case AST_NODE_TYPES.TSAbstractClassProperty:
case AST_NODE_TYPES.ClassProperty:
return util.getNameFromPropertyName(node.key);
return util.getNameFromMember(node, sourceCode);
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.MethodDefinition:
return node.kind === 'constructor'
? 'constructor'
: util.getNameFromClassMember(node, sourceCode);
: util.getNameFromMember(node, sourceCode);
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return 'new';
case AST_NODE_TYPES.TSIndexSignature:
Expand Down
63 changes: 30 additions & 33 deletions packages/eslint-plugin/src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,6 @@ type InferMessageIdsTypeFromRule<T> = T extends TSESLint.RuleModule<
? TMessageIds
: unknown;

/**
* Gets a string representation of the name of the index signature.
*/
export function getNameFromIndexSignature(
node: TSESTree.TSIndexSignature,
): string {
const propName: TSESTree.PropertyName | undefined = node.parameters.find(
(parameter: TSESTree.Parameter): parameter is TSESTree.Identifier =>
parameter.type === AST_NODE_TYPES.Identifier,
);
return propName ? getNameFromPropertyName(propName) : '(index signature)';
}

/**
* Gets a string name representation of the given PropertyName node
*/
function getNameFromPropertyName(propertyName: TSESTree.PropertyName): string {
if (propertyName.type === AST_NODE_TYPES.Identifier) {
return propertyName.name;
}
return `${propertyName.value}`;
}

/** Return true if both parameters are equal. */
type Equal<T> = (a: T, b: T) => boolean;

Expand Down Expand Up @@ -102,31 +79,51 @@ function findFirstResult<T, U>(
return undefined;
}

/**
* Gets a string representation of the name of the index signature.
*/
export function getNameFromIndexSignature(
node: TSESTree.TSIndexSignature,
): string {
const propName: TSESTree.PropertyName | undefined = node.parameters.find(
(parameter: TSESTree.Parameter): parameter is TSESTree.Identifier =>
parameter.type === AST_NODE_TYPES.Identifier,
);
return propName ? propName.name : '(index signature)';
}

/**
* Gets a string name representation of the name of the given MethodDefinition
* or ClassProperty node, with handling for computed property names.
*/
function getNameFromClassMember(
methodDefinition:
function getNameFromMember(
member:
| TSESTree.MethodDefinition
| TSESTree.TSMethodSignature
| TSESTree.TSAbstractMethodDefinition
| TSESTree.ClassProperty
| TSESTree.TSAbstractMethodDefinition,
| TSESTree.TSAbstractClassProperty
| TSESTree.Property
| TSESTree.TSPropertySignature,
sourceCode: TSESLint.SourceCode,
): string {
if (keyCanBeReadAsPropertyName(methodDefinition.key)) {
return getNameFromPropertyName(methodDefinition.key);
if (isLiteralOrIdentifier(member.key)) {
if (member.key.type === AST_NODE_TYPES.Identifier) {
return member.key.name;
}
return `${member.key.value}`;
}

return sourceCode.text.slice(...methodDefinition.key.range);
return sourceCode.text.slice(...member.key.range);
}

/**
* This covers both actual property names, as well as computed properties that are either
* an identifier or a literal at the top level.
*/
function keyCanBeReadAsPropertyName(
function isLiteralOrIdentifier(
node: TSESTree.Expression,
): node is TSESTree.PropertyName {
): node is TSESTree.Literal | TSESTree.Identifier {
return (
node.type === AST_NODE_TYPES.Literal ||
node.type === AST_NODE_TYPES.Identifier
Expand All @@ -147,11 +144,11 @@ export {
Equal,
ExcludeKeys,
findFirstResult,
getNameFromClassMember,
getNameFromPropertyName,
getNameFromMember,
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
isDefinitionFile,
isLiteralOrIdentifier,
RequireKeys,
upperCaseFirst,
};
18 changes: 12 additions & 6 deletions packages/typescript-estree/src/ts-estree/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ export type Modifier =
export type ObjectLiteralElementLike =
| MethodDefinition
| Property
| RestElement
| SpreadElement
| TSAbstractMethodDefinition;
export type Parameter =
Expand All @@ -355,6 +354,13 @@ export type Parameter =
| ObjectPattern
| Identifier
| TSParameterProperty;
export type DestructuringPattern =
| Identifier
| ObjectPattern
| ArrayPattern
| RestElement
| AssignmentPattern
| MemberExpression;
export type PrimaryExpression =
| ArrayExpression
| ArrayPattern
Expand All @@ -374,7 +380,7 @@ export type PrimaryExpression =
| TemplateLiteral
| ThisExpression
| TSNullKeyword;
export type PropertyName = Identifier | Literal;
export type PropertyName = Expression;
export type Statement =
| BlockStatement
| BreakStatement
Expand Down Expand Up @@ -509,7 +515,7 @@ interface LiteralBase extends BaseNode {
}

interface MethodDefinitionBase extends BaseNode {
key: Expression;
key: PropertyName;
value: FunctionExpression | TSEmptyBodyFunctionExpression;
computed: boolean;
static: boolean;
Expand Down Expand Up @@ -542,7 +548,7 @@ export interface ArrayExpression extends BaseNode {

export interface ArrayPattern extends BaseNode {
type: AST_NODE_TYPES.ArrayPattern;
elements: Expression[];
elements: DestructuringPattern[];
typeAnnotation?: TSTypeAnnotation;
optional?: boolean;
decorators?: Decorator[];
Expand Down Expand Up @@ -897,7 +903,7 @@ export interface ObjectExpression extends BaseNode {

export interface ObjectPattern extends BaseNode {
type: AST_NODE_TYPES.ObjectPattern;
properties: ObjectLiteralElementLike[];
properties: (Property | RestElement)[];
typeAnnotation?: TSTypeAnnotation;
optional?: boolean;
decorators?: Decorator[];
Expand All @@ -923,7 +929,7 @@ export interface Property extends BaseNode {

export interface RestElement extends BaseNode {
type: AST_NODE_TYPES.RestElement;
argument: BindingName | Expression | PropertyName;
argument: DestructuringPattern;
typeAnnotation?: TSTypeAnnotation;
optional?: boolean;
value?: AssignmentPattern;
Expand Down

0 comments on commit f335c50

Please sign in to comment.