From 1a3d44bbc2c606a5083647a15963d85e20761d2b Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Wed, 18 Dec 2019 18:48:08 +1030 Subject: [PATCH 1/7] chore: use discriminated unions for member name types property names are either `computed: false` with `key: Identifier | StringLiteral | NumberLiteral`, or they are `computed: true` with `key: Expression`. the previous typings took the simple approach of just having a single type, but it made things a bit more cumbersome. This change creates two types for each, using the `computed` value as the key for a discriminated union. This means that if you check `node.computed === true`, then TS will narrow the `key` type appropriately. I also noticed a minor bug in the `TSEnumMember` handling, as the types didn't previously support the fact that it's syntactically valid to use an expression (note it's semantically invalid - TS error 1164). It's also semantically and syntactically valid to do something like `enum Foo { ['key'] }`, which really should be handled differently to `enum Foo { key }`, even if they mean the same thing. So I also added a `computed` prop to `TSEnumMember`, and gave it the same discriminated union treatment. --- packages/eslint-plugin/src/rules/indent.ts | 2 +- .../src/rules/no-untyped-public-signature.ts | 2 +- .../eslint-plugin/src/rules/prefer-for-of.ts | 3 +- packages/eslint-plugin/src/util/misc.ts | 16 +- .../basics/export-named-enum-computed.src.ts | 4 + packages/typescript-estree/src/convert.ts | 1 + .../src/semantic-or-syntactic-errors.ts | 76 ++-- .../src/ts-estree/ts-estree.ts | 339 +++++++++++---- .../semantic-diagnostics-enabled.ts.snap | 9 + .../lib/__snapshots__/typescript.ts.snap | 387 ++++++++++++++++++ 10 files changed, 710 insertions(+), 129 deletions(-) create mode 100644 packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed.src.ts diff --git a/packages/eslint-plugin/src/rules/indent.ts b/packages/eslint-plugin/src/rules/indent.ts index 46ac924f4c74..9023939ac629 100644 --- a/packages/eslint-plugin/src/rules/indent.ts +++ b/packages/eslint-plugin/src/rules/indent.ts @@ -392,7 +392,7 @@ export default util.createRule({ computed: false, method: false, shorthand: false, - }, + } as any, ], // location data diff --git a/packages/eslint-plugin/src/rules/no-untyped-public-signature.ts b/packages/eslint-plugin/src/rules/no-untyped-public-signature.ts index b72e43e4cd04..88ccd3e4a953 100644 --- a/packages/eslint-plugin/src/rules/no-untyped-public-signature.ts +++ b/packages/eslint-plugin/src/rules/no-untyped-public-signature.ts @@ -63,7 +63,7 @@ export default util.createRule({ ) { return ignoredMethods.has(node.key.quasis[0].value.raw); } - if (node.key.type === AST_NODE_TYPES.Identifier && !node.computed) { + if (!node.computed && node.key.type === AST_NODE_TYPES.Identifier) { return ignoredMethods.has(node.key.name); } diff --git a/packages/eslint-plugin/src/rules/prefer-for-of.ts b/packages/eslint-plugin/src/rules/prefer-for-of.ts index 80180415ace3..40fac431664d 100644 --- a/packages/eslint-plugin/src/rules/prefer-for-of.ts +++ b/packages/eslint-plugin/src/rules/prefer-for-of.ts @@ -149,9 +149,8 @@ export default util.createRule({ // ({ foo: a[i] }) = { foo: 0 } if ( parent.type === AST_NODE_TYPES.Property && - parent.parent !== undefined && - parent.parent.type === AST_NODE_TYPES.ObjectExpression && parent.value === node && + parent.parent?.type === AST_NODE_TYPES.ObjectExpression && isAssignee(parent.parent) ) { return true; diff --git a/packages/eslint-plugin/src/util/misc.ts b/packages/eslint-plugin/src/util/misc.ts index 74bc37fabbb6..bb84801b1cd8 100644 --- a/packages/eslint-plugin/src/util/misc.ts +++ b/packages/eslint-plugin/src/util/misc.ts @@ -107,7 +107,7 @@ function getNameFromMember( | TSESTree.TSPropertySignature, sourceCode: TSESLint.SourceCode, ): string { - if (isLiteralOrIdentifier(member.key)) { + if (!member.computed) { if (member.key.type === AST_NODE_TYPES.Identifier) { return member.key.name; } @@ -117,19 +117,6 @@ function getNameFromMember( 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 isLiteralOrIdentifier( - node: TSESTree.Expression, -): node is TSESTree.Literal | TSESTree.Identifier { - return ( - node.type === AST_NODE_TYPES.Literal || - node.type === AST_NODE_TYPES.Identifier - ); -} - type ExcludeKeys< TObj extends Record, TKeys extends keyof TObj @@ -148,7 +135,6 @@ export { InferMessageIdsTypeFromRule, InferOptionsTypeFromRule, isDefinitionFile, - isLiteralOrIdentifier, RequireKeys, upperCaseFirst, }; diff --git a/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed.src.ts b/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed.src.ts new file mode 100644 index 000000000000..2ca22c7ddea6 --- /dev/null +++ b/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed.src.ts @@ -0,0 +1,4 @@ +export enum Foo { + ['baz'], + [1], +} diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index a48427d9e9ef..e269847bf3fe 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -2485,6 +2485,7 @@ export class Converter { const result = this.createNode(node, { type: AST_NODE_TYPES.TSEnumMember, id: this.convertChild(node.name), + computed: node.name.kind === ts.SyntaxKind.ComputedPropertyName, }); if (node.initializer) { result.initializer = this.convertChild(node.initializer); diff --git a/packages/typescript-estree/src/semantic-or-syntactic-errors.ts b/packages/typescript-estree/src/semantic-or-syntactic-errors.ts index a8ac8728798a..d2b61efc856d 100644 --- a/packages/typescript-estree/src/semantic-or-syntactic-errors.ts +++ b/packages/typescript-estree/src/semantic-or-syntactic-errors.ts @@ -56,43 +56,45 @@ function whitelistSupportedDiagnostics( ): readonly (ts.DiagnosticWithLocation | ts.Diagnostic)[] { return diagnostics.filter(diagnostic => { switch (diagnostic.code) { - case 1013: // ts 3.2 "A rest parameter or binding pattern may not have a trailing comma." - case 1014: // ts 3.2 "A rest parameter must be last in a parameter list." - case 1044: // ts 3.2 "'{0}' modifier cannot appear on a module or namespace element." - case 1045: // ts 3.2 "A '{0}' modifier cannot be used with an interface declaration." - case 1048: // ts 3.2 "A rest parameter cannot have an initializer." - case 1049: // ts 3.2 "A 'set' accessor must have exactly one parameter." - case 1070: // ts 3.2 "'{0}' modifier cannot appear on a type member." - case 1071: // ts 3.2 "'{0}' modifier cannot appear on an index signature." - case 1085: // ts 3.2 "Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '{0}'." - case 1090: // ts 3.2 "'{0}' modifier cannot appear on a parameter." - case 1096: // ts 3.2 "An index signature must have exactly one parameter." - case 1097: // ts 3.2 "'{0}' list cannot be empty." - case 1098: // ts 3.3 "Type parameter list cannot be empty." - case 1099: // ts 3.3 "Type argument list cannot be empty." - case 1117: // ts 3.2 "An object literal cannot have multiple properties with the same name in strict mode." - case 1121: // ts 3.2 "Octal literals are not allowed in strict mode." - case 1123: // ts 3.2: "Variable declaration list cannot be empty." - case 1141: // ts 3.2 "String literal expected." - case 1162: // ts 3.2 "An object member cannot be declared optional." - case 1172: // ts 3.2 "'extends' clause already seen." - case 1173: // ts 3.2 "'extends' clause must precede 'implements' clause." - case 1175: // ts 3.2 "'implements' clause already seen." - case 1176: // ts 3.2 "Interface declaration cannot have 'implements' clause." - case 1190: // ts 3.2 "The variable declaration of a 'for...of' statement cannot have an initializer." - case 1200: // ts 3.2 "Line terminator not permitted before arrow." - case 1206: // ts 3.2 "Decorators are not valid here." - case 1211: // ts 3.2 "A class declaration without the 'default' modifier must have a name." - case 1242: // ts 3.2 "'abstract' modifier can only appear on a class, method, or property declaration." - case 1246: // ts 3.2 "An interface property cannot have an initializer." - case 1255: // ts 3.2 "A definite assignment assertion '!' is not permitted in this context." - case 1308: // ts 3.2 "'await' expression is only allowed within an async function." - case 2364: // ts 3.2 "The left-hand side of an assignment expression must be a variable or a property access." - case 2369: // ts 3.2 "A parameter property is only allowed in a constructor implementation." - case 2462: // ts 3.2 "A rest element must be last in a destructuring pattern." - case 8017: // ts 3.2 "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." - case 17012: // ts 3.2 "'{0}' is not a valid meta-property for keyword '{1}'. Did you mean '{2}'?" - case 17013: // ts 3.2 "Meta-property '{0}' is only allowed in the body of a function declaration, function expression, or constructor." + case 1013: // "A rest parameter or binding pattern may not have a trailing comma." + case 1014: // "A rest parameter must be last in a parameter list." + case 1044: // "'{0}' modifier cannot appear on a module or namespace element." + case 1045: // "A '{0}' modifier cannot be used with an interface declaration." + case 1048: // "A rest parameter cannot have an initializer." + case 1049: // "A 'set' accessor must have exactly one parameter." + case 1070: // "'{0}' modifier cannot appear on a type member." + case 1071: // "'{0}' modifier cannot appear on an index signature." + case 1085: // "Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '{0}'." + case 1090: // "'{0}' modifier cannot appear on a parameter." + case 1096: // "An index signature must have exactly one parameter." + case 1097: // "'{0}' list cannot be empty." + case 1098: // "Type parameter list cannot be empty." + case 1099: // "Type argument list cannot be empty." + case 1117: // "An object literal cannot have multiple properties with the same name in strict mode." + case 1121: // "Octal literals are not allowed in strict mode." + case 1123: // "Variable declaration list cannot be empty." + case 1141: // "String literal expected." + case 1162: // "An object member cannot be declared optional." + case 1164: // "Computed property names are not allowed in enums." + case 1172: // "'extends' clause already seen." + case 1173: // "'extends' clause must precede 'implements' clause." + case 1175: // "'implements' clause already seen." + case 1176: // "Interface declaration cannot have 'implements' clause." + case 1190: // "The variable declaration of a 'for...of' statement cannot have an initializer." + case 1200: // "Line terminator not permitted before arrow." + case 1206: // "Decorators are not valid here." + case 1211: // "A class declaration without the 'default' modifier must have a name." + case 1242: // "'abstract' modifier can only appear on a class, method, or property declaration." + case 1246: // "An interface property cannot have an initializer." + case 1255: // "A definite assignment assertion '!' is not permitted in this context." + case 1308: // "'await' expression is only allowed within an async function." + case 2364: // "The left-hand side of an assignment expression must be a variable or a property access." + case 2369: // "A parameter property is only allowed in a constructor implementation." + case 2452: // "An enum member cannot have a numeric name." + case 2462: // "A rest element must be last in a destructuring pattern." + case 8017: // "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." + case 17012: // "'{0}' is not a valid meta-property for keyword '{1}'. Did you mean '{2}'?" + case 17013: // "Meta-property '{0}' is only allowed in the body of a function declaration, function expression, or constructor." return true; } return false; diff --git a/packages/typescript-estree/src/ts-estree/ts-estree.ts b/packages/typescript-estree/src/ts-estree/ts-estree.ts index 438730ab8fcf..150fa652833e 100644 --- a/packages/typescript-estree/src/ts-estree/ts-estree.ts +++ b/packages/typescript-estree/src/ts-estree/ts-estree.ts @@ -254,6 +254,9 @@ export type ClassElement = | TSAbstractMethodDefinition | TSEmptyBodyFunctionExpression | TSIndexSignature; +export type ClassProperty = + | ClassPropertyComputedName + | ClassPropertyNonComputedName; export type DeclarationStatement = | ClassDeclaration | ClassExpression @@ -331,7 +334,19 @@ export type LeftHandSideExpression = | TaggedTemplateExpression | TSNonNullExpression | TSAsExpression; +export type Literal = + | BooleanLiteral + | NumberLiteral + | NullLiteral + | RegExpLiteral + | StringLiteral; export type LiteralExpression = BigIntLiteral | Literal | TemplateLiteral; +export type MemberExpression = + | MemberExpressionComputedName + | MemberExpressionNonComputedName; +export type MethodDefinition = + | MethodDefinitionComputedName + | MethodDefinitionNonComputedName; export type Modifier = | TSAbstractKeyword | TSAsyncKeyword @@ -347,6 +362,9 @@ export type ObjectLiteralElementLike = | Property | SpreadElement | TSAbstractMethodDefinition; +export type OptionalMemberExpression = + | OptionalMemberExpressionComputedName + | OptionalMemberExpressionNonComputedName; export type Parameter = | AssignmentPattern | RestElement @@ -380,7 +398,13 @@ export type PrimaryExpression = | TemplateLiteral | ThisExpression | TSNullKeyword; -export type PropertyName = Expression; +export type Property = PropertyComputedName | PropertyNonComputedName; +export type PropertyName = PropertyNameComputed | PropertyNameNonComputed; +export type PropertyNameComputed = Expression; +export type PropertyNameNonComputed = + | Identifier + | StringLiteral + | NumberLiteral; export type Statement = | BlockStatement | BreakStatement @@ -400,6 +424,27 @@ export type Statement = | TryStatement | VariableDeclaration | WithStatement; +export type TSAbstractClassProperty = + | TSAbstractClassPropertyComputedName + | TSAbstractClassPropertyNonComputedName; +export type TSAbstractMethodDefinition = + | TSAbstractMethodDefinitionComputedName + | TSAbstractMethodDefinitionNonComputedName; +export type TSMethodSignature = + | TSMethodSignatureComputedName + | TSMethodSignatureNonComputedName; +export type TSPropertySignature = + | TSPropertySignatureComputedName + | TSPropertySignatureNonComputedName; +export type TSEnumMember = + | TSEnumMemberComputedName + | TSEnumMemberNonComputedName; +export type TSUnaryExpression = + | AwaitExpression + | LeftHandSideExpression + | TSTypeAssertion + | UnaryExpression + | UpdateExpression; export type TypeElement = | TSCallSignatureDeclaration | TSConstructSignatureDeclaration @@ -443,12 +488,6 @@ export type TypeNode = | TSUnionType | TSUnknownKeyword | TSVoidKeyword; -export type TSUnaryExpression = - | AwaitExpression - | LeftHandSideExpression - | TSTypeAssertion - | UnaryExpression - | UpdateExpression; /////////////// // Base, common types @@ -461,6 +500,13 @@ interface BinaryExpressionBase extends BaseNode { right: Expression; } +interface CallExpressionBase extends BaseNode { + callee: LeftHandSideExpression; + arguments: Expression[]; + typeParameters?: TSTypeParameterInstantiation; + optional: boolean; +} + interface ClassDeclarationBase extends BaseNode { typeParameters?: TSTypeParameterDeclaration; superTypeParameters?: TSTypeParameterInstantiation; @@ -473,8 +519,9 @@ interface ClassDeclarationBase extends BaseNode { decorators?: Decorator[]; } +/** this should not be directly used - instead use ClassPropertyComputedNameBase or ClassPropertyNonComputedNameBase */ interface ClassPropertyBase extends BaseNode { - key: PropertyName; + key: PropertyNameComputed | PropertyNameComputed; value: Expression | null; computed: boolean; static: boolean; @@ -487,6 +534,16 @@ interface ClassPropertyBase extends BaseNode { typeAnnotation?: TSTypeAnnotation; } +interface ClassPropertyComputedNameBase extends ClassPropertyBase { + key: PropertyNameComputed; + computed: true; +} + +interface ClassPropertyNonComputedNameBase extends ClassPropertyBase { + key: PropertyNameNonComputed; + computed: false; +} + interface FunctionDeclarationBase extends BaseNode { id: Identifier | null; generator: boolean; @@ -514,8 +571,27 @@ interface LiteralBase extends BaseNode { }; } +/** this should not be directly used - instead use MemberExpressionComputedNameBase or MemberExpressionNonComputedNameBase */ +interface MemberExpressionBase extends BaseNode { + object: LeftHandSideExpression; + property: Expression | Identifier; + computed: boolean; + optional: boolean; +} + +interface MemberExpressionComputedNameBase extends MemberExpressionBase { + property: Expression; + computed: true; +} + +interface MemberExpressionNonComputedNameBase extends MemberExpressionBase { + property: Identifier; + computed: false; +} + +/** this should not be directly used - instead use MethodDefinitionComputedNameBase or MethodDefinitionNonComputedNameBase */ interface MethodDefinitionBase extends BaseNode { - key: PropertyName; + key: PropertyNameComputed | PropertyNameComputed; value: FunctionExpression | TSEmptyBodyFunctionExpression; computed: boolean; static: boolean; @@ -525,11 +601,68 @@ interface MethodDefinitionBase extends BaseNode { typeParameters?: TSTypeParameterDeclaration; } +interface MethodDefinitionComputedNameBase extends MethodDefinitionBase { + key: PropertyNameComputed; + computed: true; +} + +interface MethodDefinitionNonComputedNameBase extends MethodDefinitionBase { + key: PropertyNameNonComputed; + computed: false; +} + +interface PropertyBase extends BaseNode { + type: AST_NODE_TYPES.Property; + key: PropertyNameComputed | PropertyNameNonComputed; + value: Expression | AssignmentPattern | BindingName; + computed: boolean; + method: boolean; + shorthand: boolean; + kind: 'init' | 'get' | 'set'; +} + +interface TSEnumMemberBase extends BaseNode { + type: AST_NODE_TYPES.TSEnumMember; + id: + | PropertyNameNonComputed + // this should only happen in semantically invalid code (errors 1164 and 2452) + | PropertyNameComputed; + initializer?: Expression; + computed?: boolean; +} + interface TSHeritageBase extends BaseNode { expression: Expression; typeParameters?: TSTypeParameterInstantiation; } +interface TSMethodSignatureBase extends BaseNode { + type: AST_NODE_TYPES.TSMethodSignature; + key: PropertyNameComputed | PropertyNameNonComputed; + computed: boolean; + params: Parameter[]; + optional?: boolean; + returnType?: TSTypeAnnotation; + readonly?: boolean; + typeParameters?: TSTypeParameterDeclaration; + accessibility?: Accessibility; + export?: boolean; + static?: boolean; +} + +interface TSPropertySignatureBase extends BaseNode { + type: AST_NODE_TYPES.TSPropertySignature; + key: PropertyNameComputed | PropertyNameNonComputed; + optional?: boolean; + computed: boolean; + typeAnnotation?: TSTypeAnnotation; + initializer?: Expression; + readonly?: boolean; + static?: boolean; + export?: boolean; + accessibility?: Accessibility; +} + interface UnaryExpressionBase extends BaseNode { operator: string; prefix: boolean; @@ -597,25 +730,20 @@ export interface BlockStatement extends BaseNode { body: Statement[]; } +export interface BooleanLiteral extends LiteralBase { + type: AST_NODE_TYPES.Literal; + value: boolean; +} + export interface BreakStatement extends BaseNode { type: AST_NODE_TYPES.BreakStatement; label: Identifier | null; } -interface CallExpressionBase extends BaseNode { - callee: LeftHandSideExpression; - arguments: Expression[]; - typeParameters?: TSTypeParameterInstantiation; - optional: boolean; -} export interface CallExpression extends CallExpressionBase { type: AST_NODE_TYPES.CallExpression; optional: false; } -export interface OptionalCallExpression extends CallExpressionBase { - type: AST_NODE_TYPES.OptionalCallExpression; - optional: boolean; -} export interface CatchClause extends BaseNode { type: AST_NODE_TYPES.CatchClause; @@ -636,7 +764,13 @@ export interface ClassExpression extends ClassDeclarationBase { type: AST_NODE_TYPES.ClassExpression; } -export interface ClassProperty extends ClassPropertyBase { +export interface ClassPropertyComputedName + extends ClassPropertyComputedNameBase { + type: AST_NODE_TYPES.ClassProperty; +} + +export interface ClassPropertyNonComputedName + extends ClassPropertyNonComputedNameBase { type: AST_NODE_TYPES.ClassProperty; } @@ -856,27 +990,20 @@ export interface LabeledStatement extends BaseNode { body: Statement; } -export interface Literal extends LiteralBase { - type: AST_NODE_TYPES.Literal; -} - export interface LogicalExpression extends BinaryExpressionBase { type: AST_NODE_TYPES.LogicalExpression; } -interface MemberExpressionBase extends BaseNode { - object: LeftHandSideExpression; - property: Expression | Identifier; - computed: boolean; - optional: boolean; -} -export interface MemberExpression extends MemberExpressionBase { +export interface MemberExpressionComputedName + extends MemberExpressionComputedNameBase { type: AST_NODE_TYPES.MemberExpression; optional: false; } -export interface OptionalMemberExpression extends MemberExpressionBase { - type: AST_NODE_TYPES.OptionalMemberExpression; - optional: boolean; + +export interface MemberExpressionNonComputedName + extends MemberExpressionNonComputedNameBase { + type: AST_NODE_TYPES.MemberExpression; + optional: false; } export interface MetaProperty extends BaseNode { @@ -885,7 +1012,13 @@ export interface MetaProperty extends BaseNode { property: Identifier; } -export interface MethodDefinition extends MethodDefinitionBase { +export interface MethodDefinitionComputedName + extends MethodDefinitionComputedNameBase { + type: AST_NODE_TYPES.MethodDefinition; +} + +export interface MethodDefinitionNonComputedName + extends MethodDefinitionNonComputedNameBase { type: AST_NODE_TYPES.MethodDefinition; } @@ -896,6 +1029,16 @@ export interface NewExpression extends BaseNode { typeParameters?: TSTypeParameterInstantiation; } +export interface NumberLiteral extends LiteralBase { + type: AST_NODE_TYPES.Literal; + value: number; +} + +export interface NullLiteral extends LiteralBase { + type: AST_NODE_TYPES.Literal; + value: null; +} + export interface ObjectExpression extends BaseNode { type: AST_NODE_TYPES.ObjectExpression; properties: ObjectLiteralElementLike[]; @@ -909,6 +1052,23 @@ export interface ObjectPattern extends BaseNode { decorators?: Decorator[]; } +export interface OptionalCallExpression extends CallExpressionBase { + type: AST_NODE_TYPES.OptionalCallExpression; + optional: boolean; +} + +export interface OptionalMemberExpressionComputedName + extends MemberExpressionComputedNameBase { + type: AST_NODE_TYPES.OptionalMemberExpression; + optional: boolean; +} + +export interface OptionalMemberExpressionNonComputedName + extends MemberExpressionNonComputedNameBase { + type: AST_NODE_TYPES.OptionalMemberExpression; + optional: boolean; +} + export interface Program extends BaseNode { type: AST_NODE_TYPES.Program; body: Statement[]; @@ -917,14 +1077,19 @@ export interface Program extends BaseNode { tokens?: Token[]; } -export interface Property extends BaseNode { - type: AST_NODE_TYPES.Property; - key: PropertyName; - value: Expression | AssignmentPattern | BindingName; - computed: boolean; - method: boolean; - shorthand: boolean; - kind: 'init' | 'get' | 'set'; +export interface PropertyComputedName extends PropertyBase { + key: PropertyNameComputed; + computed: true; +} + +export interface PropertyNonComputedName extends PropertyBase { + key: PropertyNameNonComputed; + computed: false; +} + +export interface RegExpLiteral extends LiteralBase { + type: AST_NODE_TYPES.Literal; + value: RegExp; } export interface RestElement extends BaseNode { @@ -948,7 +1113,12 @@ export interface SequenceExpression extends BaseNode { export interface SpreadElement extends BaseNode { type: AST_NODE_TYPES.SpreadElement; - argument: BindingName | Expression | PropertyName; + argument: Expression; +} + +export interface StringLiteral extends LiteralBase { + type: AST_NODE_TYPES.Literal; + value: string; } export interface Super extends BaseNode { @@ -1005,7 +1175,13 @@ export interface TryStatement extends BaseNode { finalizer: BlockStatement; } -export interface TSAbstractClassProperty extends ClassPropertyBase { +export interface TSAbstractClassPropertyComputedName + extends ClassPropertyComputedNameBase { + type: AST_NODE_TYPES.TSAbstractClassProperty; +} + +export interface TSAbstractClassPropertyNonComputedName + extends ClassPropertyNonComputedNameBase { type: AST_NODE_TYPES.TSAbstractClassProperty; } @@ -1013,7 +1189,13 @@ export interface TSAbstractKeyword extends BaseNode { type: AST_NODE_TYPES.TSAbstractKeyword; } -export interface TSAbstractMethodDefinition extends MethodDefinitionBase { +export interface TSAbstractMethodDefinitionComputedName + extends MethodDefinitionComputedNameBase { + type: AST_NODE_TYPES.TSAbstractMethodDefinition; +} + +export interface TSAbstractMethodDefinitionNonComputedName + extends MethodDefinitionNonComputedNameBase { type: AST_NODE_TYPES.TSAbstractMethodDefinition; } @@ -1091,10 +1273,25 @@ export interface TSEnumDeclaration extends BaseNode { decorators?: Decorator[]; } -export interface TSEnumMember extends BaseNode { - type: AST_NODE_TYPES.TSEnumMember; - id: PropertyName; - initializer?: Expression; +/** + * this should only really happen in semantically invalid code (errors 1164 and 2452) + * + * VALID: + * enum Foo { ['a'] } + * + * INVALID: + * const x = 'a'; + * enum Foo { [x] } + * enum Bar { ['a' + 'b'] } + */ +export interface TSEnumMemberComputedName extends TSEnumMemberBase { + id: PropertyNameComputed; + computed: true; +} + +export interface TSEnumMemberNonComputedName extends TSEnumMemberBase { + id: PropertyNameNonComputed; + computed: false; } export interface TSExportAssignment extends BaseNode { @@ -1190,18 +1387,15 @@ export interface TSMappedType extends BaseNode { typeAnnotation?: TypeNode; } -export interface TSMethodSignature extends BaseNode { - type: AST_NODE_TYPES.TSMethodSignature; - computed: boolean; - key: PropertyName; - params: Parameter[]; - optional?: boolean; - returnType?: TSTypeAnnotation; - readonly?: boolean; - typeParameters?: TSTypeParameterDeclaration; - accessibility?: Accessibility; - export?: boolean; - static?: boolean; +export interface TSMethodSignatureComputedName extends TSMethodSignatureBase { + key: PropertyNameComputed; + computed: true; +} + +export interface TSMethodSignatureNonComputedName + extends TSMethodSignatureBase { + key: PropertyNameNonComputed; + computed: false; } export interface TSModuleBlock extends BaseNode { @@ -1264,17 +1458,16 @@ export interface TSParenthesizedType extends BaseNode { typeAnnotation: TypeNode; } -export interface TSPropertySignature extends BaseNode { - type: AST_NODE_TYPES.TSPropertySignature; - optional?: boolean; - computed: boolean; - key: PropertyName; - typeAnnotation?: TSTypeAnnotation; - initializer?: Expression; - readonly?: boolean; - static?: boolean; - export?: boolean; - accessibility?: Accessibility; +export interface TSPropertySignatureComputedName + extends TSPropertySignatureBase { + key: PropertyNameComputed; + computed: true; +} + +export interface TSPropertySignatureNonComputedName + extends TSPropertySignatureBase { + key: PropertyNameNonComputed; + computed: false; } export interface TSPublicKeyword extends BaseNode { diff --git a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap index 7454835c3ae1..0e1b547be63b 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap @@ -1831,6 +1831,15 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-named-enum.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; +exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-named-enum-computed.src 1`] = ` +Object { + "column": 4, + "index": 35, + "lineNumber": 3, + "message": "An enum member cannot have a numeric name.", +} +`; + exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-type-alias-declaration.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-type-class-declaration.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; diff --git a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap index 14b0a341b7a9..640529f08951 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap @@ -45600,6 +45600,393 @@ Object { } `; +exports[`typescript fixtures/basics/export-named-enum-computed.src 1`] = ` +Object { + "body": Array [ + Object { + "declaration": Object { + "id": Object { + "loc": Object { + "end": Object { + "column": 15, + "line": 1, + }, + "start": Object { + "column": 12, + "line": 1, + }, + }, + "name": "Foo", + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + }, + "loc": Object { + "end": Object { + "column": 1, + "line": 4, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "members": Array [ + Object { + "id": Object { + "loc": Object { + "end": Object { + "column": 10, + "line": 2, + }, + "start": Object { + "column": 5, + "line": 2, + }, + }, + "range": Array [ + 23, + 28, + ], + "raw": "'baz'", + "type": "Literal", + "value": "baz", + }, + "loc": Object { + "end": Object { + "column": 11, + "line": 2, + }, + "start": Object { + "column": 4, + "line": 2, + }, + }, + "range": Array [ + 22, + 29, + ], + "type": "TSEnumMember", + }, + Object { + "id": Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 3, + }, + "start": Object { + "column": 5, + "line": 3, + }, + }, + "range": Array [ + 36, + 37, + ], + "raw": "1", + "type": "Literal", + "value": 1, + }, + "loc": Object { + "end": Object { + "column": 7, + "line": 3, + }, + "start": Object { + "column": 4, + "line": 3, + }, + }, + "range": Array [ + 35, + 38, + ], + "type": "TSEnumMember", + }, + ], + "range": Array [ + 7, + 41, + ], + "type": "TSEnumDeclaration", + }, + "loc": Object { + "end": Object { + "column": 1, + "line": 4, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 41, + ], + "source": null, + "specifiers": Array [], + "type": "ExportNamedDeclaration", + }, + ], + "loc": Object { + "end": Object { + "column": 0, + "line": 5, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 42, + ], + "sourceType": "module", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 6, + ], + "type": "Keyword", + "value": "export", + }, + Object { + "loc": Object { + "end": Object { + "column": 11, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 11, + ], + "type": "Keyword", + "value": "enum", + }, + Object { + "loc": Object { + "end": Object { + "column": 15, + "line": 1, + }, + "start": Object { + "column": 12, + "line": 1, + }, + }, + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + "value": "Foo", + }, + Object { + "loc": Object { + "end": Object { + "column": 17, + "line": 1, + }, + "start": Object { + "column": 16, + "line": 1, + }, + }, + "range": Array [ + 16, + 17, + ], + "type": "Punctuator", + "value": "{", + }, + Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 2, + }, + "start": Object { + "column": 4, + "line": 2, + }, + }, + "range": Array [ + 22, + 23, + ], + "type": "Punctuator", + "value": "[", + }, + Object { + "loc": Object { + "end": Object { + "column": 10, + "line": 2, + }, + "start": Object { + "column": 5, + "line": 2, + }, + }, + "range": Array [ + 23, + 28, + ], + "type": "String", + "value": "'baz'", + }, + Object { + "loc": Object { + "end": Object { + "column": 11, + "line": 2, + }, + "start": Object { + "column": 10, + "line": 2, + }, + }, + "range": Array [ + 28, + 29, + ], + "type": "Punctuator", + "value": "]", + }, + Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 11, + "line": 2, + }, + }, + "range": Array [ + 29, + 30, + ], + "type": "Punctuator", + "value": ",", + }, + Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 3, + }, + "start": Object { + "column": 4, + "line": 3, + }, + }, + "range": Array [ + 35, + 36, + ], + "type": "Punctuator", + "value": "[", + }, + Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 3, + }, + "start": Object { + "column": 5, + "line": 3, + }, + }, + "range": Array [ + 36, + 37, + ], + "type": "Numeric", + "value": "1", + }, + Object { + "loc": Object { + "end": Object { + "column": 7, + "line": 3, + }, + "start": Object { + "column": 6, + "line": 3, + }, + }, + "range": Array [ + 37, + 38, + ], + "type": "Punctuator", + "value": "]", + }, + Object { + "loc": Object { + "end": Object { + "column": 8, + "line": 3, + }, + "start": Object { + "column": 7, + "line": 3, + }, + }, + "range": Array [ + 38, + 39, + ], + "type": "Punctuator", + "value": ",", + }, + Object { + "loc": Object { + "end": Object { + "column": 1, + "line": 4, + }, + "start": Object { + "column": 0, + "line": 4, + }, + }, + "range": Array [ + 40, + 41, + ], + "type": "Punctuator", + "value": "}", + }, + ], + "type": "Program", +} +`; + exports[`typescript fixtures/basics/export-type-alias-declaration.src 1`] = ` Object { "body": Array [ From 0da7542fc80ae761378b9009fe5a4e07ebcce1b8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Wed, 18 Dec 2019 19:07:48 +1030 Subject: [PATCH 2/7] test: fix alignment tests --- .../tests/ast-alignment/fixtures-to-test.ts | 9 +++++++++ .../tests/lib/__snapshots__/typescript.ts.snap | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts index cf18c1244e77..3897e63df635 100644 --- a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts +++ b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts @@ -616,6 +616,11 @@ tester.addFixturePatternConfig('typescript/basics', { 'class-with-readonly-property', 'object-with-escaped-properties', 'type-reference-comments', + // babel doesn't allow computed enum members + 'const-enum', + 'export-declare-const-named-enum', + 'export-declare-named-enum', + 'export-named-enum', ], ignoreSourceType: [ /** @@ -759,6 +764,10 @@ tester.addFixturePatternConfig('typescript/types', { tester.addFixturePatternConfig('typescript/declare', { fileType: 'ts', + ignore: [ + // babel doesn't allow computed enum members + 'enum', + ], }); tester.addFixturePatternConfig('typescript/namespaces-and-modules', { diff --git a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap index 640529f08951..84fb0d58772c 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap @@ -37483,6 +37483,7 @@ Object { }, "members": Array [ Object { + "computed": false, "id": Object { "loc": Object { "end": Object { @@ -37537,6 +37538,7 @@ Object { "type": "TSEnumMember", }, Object { + "computed": false, "id": Object { "loc": Object { "end": Object { @@ -43242,6 +43244,7 @@ Object { }, "members": Array [ Object { + "computed": false, "id": Object { "loc": Object { "end": Object { @@ -43296,6 +43299,7 @@ Object { "type": "TSEnumMember", }, Object { + "computed": false, "id": Object { "loc": Object { "end": Object { @@ -43629,6 +43633,7 @@ Object { }, "members": Array [ Object { + "computed": false, "id": Object { "loc": Object { "end": Object { @@ -43683,6 +43688,7 @@ Object { "type": "TSEnumMember", }, Object { + "computed": false, "id": Object { "loc": Object { "end": Object { @@ -45285,6 +45291,7 @@ Object { }, "members": Array [ Object { + "computed": false, "id": Object { "loc": Object { "end": Object { @@ -45339,6 +45346,7 @@ Object { "type": "TSEnumMember", }, Object { + "computed": false, "id": Object { "loc": Object { "end": Object { @@ -45635,6 +45643,7 @@ Object { }, "members": Array [ Object { + "computed": true, "id": Object { "loc": Object { "end": Object { @@ -45671,6 +45680,7 @@ Object { "type": "TSEnumMember", }, Object { + "computed": true, "id": Object { "loc": Object { "end": Object { @@ -107143,6 +107153,7 @@ Object { }, "members": Array [ Object { + "computed": false, "id": Object { "loc": Object { "end": Object { @@ -107178,6 +107189,7 @@ Object { "type": "TSEnumMember", }, Object { + "computed": false, "id": Object { "loc": Object { "end": Object { From bc6864402ddf87f5c583e639f41dd1cd46e08252 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Wed, 18 Dec 2019 19:28:42 +1030 Subject: [PATCH 3/7] fix: correct logic in getNameFromMember --- packages/eslint-plugin/src/util/misc.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/util/misc.ts b/packages/eslint-plugin/src/util/misc.ts index bb84801b1cd8..10a12a950565 100644 --- a/packages/eslint-plugin/src/util/misc.ts +++ b/packages/eslint-plugin/src/util/misc.ts @@ -107,10 +107,10 @@ function getNameFromMember( | TSESTree.TSPropertySignature, sourceCode: TSESLint.SourceCode, ): string { - if (!member.computed) { - if (member.key.type === AST_NODE_TYPES.Identifier) { - return member.key.name; - } + if (member.key.type === AST_NODE_TYPES.Identifier) { + return member.key.name; + } + if (member.key.type === AST_NODE_TYPES.Literal) { return `${member.key.value}`; } From 3a8d6db243ea051197bba659455f3c8ba99ad7d5 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 19 Dec 2019 11:40:05 +1030 Subject: [PATCH 4/7] test: update parser snaps --- .../lib/__snapshots__/typescript.ts.snap | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/packages/parser/tests/lib/__snapshots__/typescript.ts.snap b/packages/parser/tests/lib/__snapshots__/typescript.ts.snap index 39f3da70df9c..d70e5a4a1751 100644 --- a/packages/parser/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/parser/tests/lib/__snapshots__/typescript.ts.snap @@ -15814,6 +15814,126 @@ Object { } `; +exports[`typescript fixtures/basics/export-named-enum-computed.src 1`] = ` +Object { + "$id": 3, + "block": Object { + "range": Array [ + 0, + 42, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 2, + "block": Object { + "range": Array [ + 0, + 42, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 1, + "block": Object { + "range": Array [ + 7, + 41, + ], + "type": "TSEnumDeclaration", + }, + "childScopes": Array [], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [], + "throughReferences": Array [], + "type": "enum", + "upperScope": Object { + "$ref": 2, + }, + "variableMap": Object {}, + "variableScope": Object { + "$ref": 2, + }, + "variables": Array [], + }, + ], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [], + "throughReferences": Array [], + "type": "module", + "upperScope": Object { + "$ref": 3, + }, + "variableMap": Object { + "Foo": Object { + "$ref": 0, + }, + }, + "variableScope": Object { + "$ref": 2, + }, + "variables": Array [ + Object { + "$id": 0, + "defs": Array [ + Object { + "name": Object { + "name": "Foo", + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + }, + "node": Object { + "range": Array [ + 7, + 41, + ], + "type": "TSEnumDeclaration", + }, + "parent": undefined, + "type": "EnumName", + }, + ], + "eslintUsed": undefined, + "identifiers": Array [ + Object { + "name": "Foo", + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + }, + ], + "name": "Foo", + "references": Array [], + "scope": Object { + "$ref": 2, + }, + }, + ], + }, + ], + "functionExpressionScope": false, + "isStrict": false, + "references": Array [], + "throughReferences": Array [], + "type": "global", + "upperScope": null, + "variableMap": Object {}, + "variableScope": Object { + "$ref": 3, + }, + "variables": Array [], +} +`; + exports[`typescript fixtures/basics/export-type-alias-declaration.src 1`] = ` Object { "$id": 1, From e91a7fe41b03f4427ab63f8c29c323d9d3521aec Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 19 Dec 2019 13:59:38 +1030 Subject: [PATCH 5/7] chore: make EnumMember.computed optional --- packages/typescript-estree/src/convert.ts | 4 +++- packages/typescript-estree/src/ts-estree/ts-estree.ts | 4 ++-- .../tests/ast-alignment/fixtures-to-test.ts | 9 --------- .../tests/lib/__snapshots__/typescript.ts.snap | 10 ---------- 4 files changed, 5 insertions(+), 22 deletions(-) diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index e269847bf3fe..f36203228a22 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -2485,11 +2485,13 @@ export class Converter { const result = this.createNode(node, { type: AST_NODE_TYPES.TSEnumMember, id: this.convertChild(node.name), - computed: node.name.kind === ts.SyntaxKind.ComputedPropertyName, }); if (node.initializer) { result.initializer = this.convertChild(node.initializer); } + if (node.name.kind === ts.SyntaxKind.ComputedPropertyName) { + result.computed = true; + } return result; } diff --git a/packages/typescript-estree/src/ts-estree/ts-estree.ts b/packages/typescript-estree/src/ts-estree/ts-estree.ts index 150fa652833e..153f693fdd66 100644 --- a/packages/typescript-estree/src/ts-estree/ts-estree.ts +++ b/packages/typescript-estree/src/ts-estree/ts-estree.ts @@ -625,7 +625,7 @@ interface TSEnumMemberBase extends BaseNode { type: AST_NODE_TYPES.TSEnumMember; id: | PropertyNameNonComputed - // this should only happen in semantically invalid code (errors 1164 and 2452) + // this should only happen in semantically invalid code (ts error 1164) | PropertyNameComputed; initializer?: Expression; computed?: boolean; @@ -1291,7 +1291,7 @@ export interface TSEnumMemberComputedName extends TSEnumMemberBase { export interface TSEnumMemberNonComputedName extends TSEnumMemberBase { id: PropertyNameNonComputed; - computed: false; + computed?: false; } export interface TSExportAssignment extends BaseNode { diff --git a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts index 3897e63df635..cf18c1244e77 100644 --- a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts +++ b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts @@ -616,11 +616,6 @@ tester.addFixturePatternConfig('typescript/basics', { 'class-with-readonly-property', 'object-with-escaped-properties', 'type-reference-comments', - // babel doesn't allow computed enum members - 'const-enum', - 'export-declare-const-named-enum', - 'export-declare-named-enum', - 'export-named-enum', ], ignoreSourceType: [ /** @@ -764,10 +759,6 @@ tester.addFixturePatternConfig('typescript/types', { tester.addFixturePatternConfig('typescript/declare', { fileType: 'ts', - ignore: [ - // babel doesn't allow computed enum members - 'enum', - ], }); tester.addFixturePatternConfig('typescript/namespaces-and-modules', { diff --git a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap index 84fb0d58772c..57457334b6f1 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap @@ -37483,7 +37483,6 @@ Object { }, "members": Array [ Object { - "computed": false, "id": Object { "loc": Object { "end": Object { @@ -37538,7 +37537,6 @@ Object { "type": "TSEnumMember", }, Object { - "computed": false, "id": Object { "loc": Object { "end": Object { @@ -43244,7 +43242,6 @@ Object { }, "members": Array [ Object { - "computed": false, "id": Object { "loc": Object { "end": Object { @@ -43299,7 +43296,6 @@ Object { "type": "TSEnumMember", }, Object { - "computed": false, "id": Object { "loc": Object { "end": Object { @@ -43633,7 +43629,6 @@ Object { }, "members": Array [ Object { - "computed": false, "id": Object { "loc": Object { "end": Object { @@ -43688,7 +43683,6 @@ Object { "type": "TSEnumMember", }, Object { - "computed": false, "id": Object { "loc": Object { "end": Object { @@ -45291,7 +45285,6 @@ Object { }, "members": Array [ Object { - "computed": false, "id": Object { "loc": Object { "end": Object { @@ -45346,7 +45339,6 @@ Object { "type": "TSEnumMember", }, Object { - "computed": false, "id": Object { "loc": Object { "end": Object { @@ -107153,7 +107145,6 @@ Object { }, "members": Array [ Object { - "computed": false, "id": Object { "loc": Object { "end": Object { @@ -107189,7 +107180,6 @@ Object { "type": "TSEnumMember", }, Object { - "computed": false, "id": Object { "loc": Object { "end": Object { From 167614610321a1616d548ea075efa510d8322dd7 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 19 Dec 2019 14:23:55 +1030 Subject: [PATCH 6/7] test: split computed enum member tests --- .../lib/__snapshots__/typescript.ts.snap | 295 +++++++- .../export-named-enum-computed-number.src.ts | 3 + ... export-named-enum-computed-string.src.ts} | 1 - .../export-named-enum-computed-var-ref.src.ts | 3 + .../tests/ast-alignment/fixtures-to-test.ts | 2 + .../semantic-diagnostics-enabled.ts.snap | 17 +- .../lib/__snapshots__/typescript.ts.snap | 640 +++++++++++++++--- 7 files changed, 857 insertions(+), 104 deletions(-) create mode 100644 packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-number.src.ts rename packages/shared-fixtures/fixtures/typescript/basics/{export-named-enum-computed.src.ts => export-named-enum-computed-string.src.ts} (78%) create mode 100644 packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-var-ref.src.ts diff --git a/packages/parser/tests/lib/__snapshots__/typescript.ts.snap b/packages/parser/tests/lib/__snapshots__/typescript.ts.snap index d70e5a4a1751..b5be8d45758e 100644 --- a/packages/parser/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/parser/tests/lib/__snapshots__/typescript.ts.snap @@ -15814,13 +15814,13 @@ Object { } `; -exports[`typescript fixtures/basics/export-named-enum-computed.src 1`] = ` +exports[`typescript fixtures/basics/export-named-enum-computed-number.src 1`] = ` Object { "$id": 3, "block": Object { "range": Array [ 0, - 42, + 29, ], "type": "Program", }, @@ -15830,7 +15830,7 @@ Object { "block": Object { "range": Array [ 0, - 42, + 29, ], "type": "Program", }, @@ -15840,7 +15840,7 @@ Object { "block": Object { "range": Array [ 7, - 41, + 28, ], "type": "TSEnumDeclaration", }, @@ -15892,7 +15892,7 @@ Object { "node": Object { "range": Array [ 7, - 41, + 28, ], "type": "TSEnumDeclaration", }, @@ -15934,6 +15934,291 @@ Object { } `; +exports[`typescript fixtures/basics/export-named-enum-computed-string.src 1`] = ` +Object { + "$id": 3, + "block": Object { + "range": Array [ + 0, + 33, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 2, + "block": Object { + "range": Array [ + 0, + 33, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 1, + "block": Object { + "range": Array [ + 7, + 32, + ], + "type": "TSEnumDeclaration", + }, + "childScopes": Array [], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [], + "throughReferences": Array [], + "type": "enum", + "upperScope": Object { + "$ref": 2, + }, + "variableMap": Object {}, + "variableScope": Object { + "$ref": 2, + }, + "variables": Array [], + }, + ], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [], + "throughReferences": Array [], + "type": "module", + "upperScope": Object { + "$ref": 3, + }, + "variableMap": Object { + "Foo": Object { + "$ref": 0, + }, + }, + "variableScope": Object { + "$ref": 2, + }, + "variables": Array [ + Object { + "$id": 0, + "defs": Array [ + Object { + "name": Object { + "name": "Foo", + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + }, + "node": Object { + "range": Array [ + 7, + 32, + ], + "type": "TSEnumDeclaration", + }, + "parent": undefined, + "type": "EnumName", + }, + ], + "eslintUsed": undefined, + "identifiers": Array [ + Object { + "name": "Foo", + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + }, + ], + "name": "Foo", + "references": Array [], + "scope": Object { + "$ref": 2, + }, + }, + ], + }, + ], + "functionExpressionScope": false, + "isStrict": false, + "references": Array [], + "throughReferences": Array [], + "type": "global", + "upperScope": null, + "variableMap": Object {}, + "variableScope": Object { + "$ref": 3, + }, + "variables": Array [], +} +`; + +exports[`typescript fixtures/basics/export-named-enum-computed-var-ref.src 1`] = ` +Object { + "$id": 4, + "block": Object { + "range": Array [ + 0, + 29, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 3, + "block": Object { + "range": Array [ + 0, + 29, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 2, + "block": Object { + "range": Array [ + 7, + 28, + ], + "type": "TSEnumDeclaration", + }, + "childScopes": Array [], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [], + "throughReferences": Array [], + "type": "enum", + "upperScope": Object { + "$ref": 3, + }, + "variableMap": Object { + "x": Object { + "$ref": 1, + }, + }, + "variableScope": Object { + "$ref": 3, + }, + "variables": Array [ + Object { + "$id": 1, + "defs": Array [ + Object { + "name": Object { + "name": "x", + "range": Array [ + 23, + 24, + ], + "type": "Identifier", + }, + "node": Object { + "range": Array [ + 22, + 25, + ], + "type": "TSEnumMember", + }, + "parent": undefined, + "type": "EnumMemberName", + }, + ], + "eslintUsed": undefined, + "identifiers": Array [ + Object { + "name": "x", + "range": Array [ + 23, + 24, + ], + "type": "Identifier", + }, + ], + "name": "x", + "references": Array [], + "scope": Object { + "$ref": 2, + }, + }, + ], + }, + ], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [], + "throughReferences": Array [], + "type": "module", + "upperScope": Object { + "$ref": 4, + }, + "variableMap": Object { + "Foo": Object { + "$ref": 0, + }, + }, + "variableScope": Object { + "$ref": 3, + }, + "variables": Array [ + Object { + "$id": 0, + "defs": Array [ + Object { + "name": Object { + "name": "Foo", + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + }, + "node": Object { + "range": Array [ + 7, + 28, + ], + "type": "TSEnumDeclaration", + }, + "parent": undefined, + "type": "EnumName", + }, + ], + "eslintUsed": undefined, + "identifiers": Array [ + Object { + "name": "Foo", + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + }, + ], + "name": "Foo", + "references": Array [], + "scope": Object { + "$ref": 3, + }, + }, + ], + }, + ], + "functionExpressionScope": false, + "isStrict": false, + "references": Array [], + "throughReferences": Array [], + "type": "global", + "upperScope": null, + "variableMap": Object {}, + "variableScope": Object { + "$ref": 4, + }, + "variables": Array [], +} +`; + exports[`typescript fixtures/basics/export-type-alias-declaration.src 1`] = ` Object { "$id": 1, diff --git a/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-number.src.ts b/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-number.src.ts new file mode 100644 index 000000000000..5a89330975c9 --- /dev/null +++ b/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-number.src.ts @@ -0,0 +1,3 @@ +export enum Foo { + [1], +} diff --git a/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed.src.ts b/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-string.src.ts similarity index 78% rename from packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed.src.ts rename to packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-string.src.ts index 2ca22c7ddea6..0cbfb7c4fcde 100644 --- a/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed.src.ts +++ b/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-string.src.ts @@ -1,4 +1,3 @@ export enum Foo { ['baz'], - [1], } diff --git a/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-var-ref.src.ts b/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-var-ref.src.ts new file mode 100644 index 000000000000..4c02bc40a944 --- /dev/null +++ b/packages/shared-fixtures/fixtures/typescript/basics/export-named-enum-computed-var-ref.src.ts @@ -0,0 +1,3 @@ +export enum Foo { + [x], +} diff --git a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts index cf18c1244e77..bf919d2eccac 100644 --- a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts +++ b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts @@ -616,6 +616,8 @@ tester.addFixturePatternConfig('typescript/basics', { 'class-with-readonly-property', 'object-with-escaped-properties', 'type-reference-comments', + // babel hard fails on computed string enum members, but TS doesn't + 'export-named-enum-computed-string', ], ignoreSourceType: [ /** diff --git a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap index 0e1b547be63b..d19f40dee215 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap @@ -1831,15 +1831,26 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-named-enum.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; -exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-named-enum-computed.src 1`] = ` +exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-named-enum-computed-number.src 1`] = ` Object { "column": 4, - "index": 35, - "lineNumber": 3, + "index": 22, + "lineNumber": 2, "message": "An enum member cannot have a numeric name.", } `; +exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-named-enum-computed-string.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; + +exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-named-enum-computed-var-ref.src 1`] = ` +Object { + "column": 4, + "index": 22, + "lineNumber": 2, + "message": "Computed property names are not allowed in enums.", +} +`; + exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-type-alias-declaration.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-type-class-declaration.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; diff --git a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap index 57457334b6f1..71ee0537b876 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap @@ -45600,7 +45600,7 @@ Object { } `; -exports[`typescript fixtures/basics/export-named-enum-computed.src 1`] = ` +exports[`typescript fixtures/basics/export-named-enum-computed-number.src 1`] = ` Object { "body": Array [ Object { @@ -45626,7 +45626,7 @@ Object { "loc": Object { "end": Object { "column": 1, - "line": 4, + "line": 3, }, "start": Object { "column": 7, @@ -45639,7 +45639,7 @@ Object { "id": Object { "loc": Object { "end": Object { - "column": 10, + "column": 6, "line": 2, }, "start": Object { @@ -45649,44 +45649,7 @@ Object { }, "range": Array [ 23, - 28, - ], - "raw": "'baz'", - "type": "Literal", - "value": "baz", - }, - "loc": Object { - "end": Object { - "column": 11, - "line": 2, - }, - "start": Object { - "column": 4, - "line": 2, - }, - }, - "range": Array [ - 22, - 29, - ], - "type": "TSEnumMember", - }, - Object { - "computed": true, - "id": Object { - "loc": Object { - "end": Object { - "column": 6, - "line": 3, - }, - "start": Object { - "column": 5, - "line": 3, - }, - }, - "range": Array [ - 36, - 37, + 24, ], "raw": "1", "type": "Literal", @@ -45695,30 +45658,30 @@ Object { "loc": Object { "end": Object { "column": 7, - "line": 3, + "line": 2, }, "start": Object { "column": 4, - "line": 3, + "line": 2, }, }, "range": Array [ - 35, - 38, + 22, + 25, ], "type": "TSEnumMember", }, ], "range": Array [ 7, - 41, + 28, ], "type": "TSEnumDeclaration", }, "loc": Object { "end": Object { "column": 1, - "line": 4, + "line": 3, }, "start": Object { "column": 0, @@ -45727,7 +45690,7 @@ Object { }, "range": Array [ 0, - 41, + 28, ], "source": null, "specifiers": Array [], @@ -45737,7 +45700,7 @@ Object { "loc": Object { "end": Object { "column": 0, - "line": 5, + "line": 4, }, "start": Object { "column": 0, @@ -45746,7 +45709,7 @@ Object { }, "range": Array [ 0, - 42, + 29, ], "sourceType": "module", "tokens": Array [ @@ -45843,7 +45806,7 @@ Object { Object { "loc": Object { "end": Object { - "column": 10, + "column": 6, "line": 2, }, "start": Object { @@ -45853,25 +45816,25 @@ Object { }, "range": Array [ 23, - 28, + 24, ], - "type": "String", - "value": "'baz'", + "type": "Numeric", + "value": "1", }, Object { "loc": Object { "end": Object { - "column": 11, + "column": 7, "line": 2, }, "start": Object { - "column": 10, + "column": 6, "line": 2, }, }, "range": Array [ - 28, - 29, + 24, + 25, ], "type": "Punctuator", "value": "]", @@ -45879,17 +45842,17 @@ Object { Object { "loc": Object { "end": Object { - "column": 12, + "column": 8, "line": 2, }, "start": Object { - "column": 11, + "column": 7, "line": 2, }, }, "range": Array [ - 29, - 30, + 25, + 26, ], "type": "Punctuator", "value": ",", @@ -45897,89 +45860,576 @@ Object { Object { "loc": Object { "end": Object { - "column": 5, + "column": 1, "line": 3, }, "start": Object { - "column": 4, + "column": 0, "line": 3, }, }, "range": Array [ - 35, - 36, + 27, + 28, ], "type": "Punctuator", - "value": "[", + "value": "}", }, + ], + "type": "Program", +} +`; + +exports[`typescript fixtures/basics/export-named-enum-computed-string.src 1`] = ` +Object { + "body": Array [ Object { + "declaration": Object { + "id": Object { + "loc": Object { + "end": Object { + "column": 15, + "line": 1, + }, + "start": Object { + "column": 12, + "line": 1, + }, + }, + "name": "Foo", + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + }, + "loc": Object { + "end": Object { + "column": 1, + "line": 3, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "members": Array [ + Object { + "computed": true, + "id": Object { + "loc": Object { + "end": Object { + "column": 10, + "line": 2, + }, + "start": Object { + "column": 5, + "line": 2, + }, + }, + "range": Array [ + 23, + 28, + ], + "raw": "'baz'", + "type": "Literal", + "value": "baz", + }, + "loc": Object { + "end": Object { + "column": 11, + "line": 2, + }, + "start": Object { + "column": 4, + "line": 2, + }, + }, + "range": Array [ + 22, + 29, + ], + "type": "TSEnumMember", + }, + ], + "range": Array [ + 7, + 32, + ], + "type": "TSEnumDeclaration", + }, "loc": Object { "end": Object { - "column": 6, + "column": 1, "line": 3, }, "start": Object { - "column": 5, - "line": 3, + "column": 0, + "line": 1, }, }, "range": Array [ - 36, - 37, + 0, + 32, ], - "type": "Numeric", - "value": "1", + "source": null, + "specifiers": Array [], + "type": "ExportNamedDeclaration", + }, + ], + "loc": Object { + "end": Object { + "column": 0, + "line": 4, + }, + "start": Object { + "column": 0, + "line": 1, }, + }, + "range": Array [ + 0, + 33, + ], + "sourceType": "module", + "tokens": Array [ Object { "loc": Object { "end": Object { - "column": 7, - "line": 3, + "column": 6, + "line": 1, }, "start": Object { - "column": 6, - "line": 3, + "column": 0, + "line": 1, }, }, "range": Array [ - 37, - 38, + 0, + 6, ], - "type": "Punctuator", - "value": "]", + "type": "Keyword", + "value": "export", }, Object { "loc": Object { "end": Object { - "column": 8, - "line": 3, + "column": 11, + "line": 1, }, "start": Object { "column": 7, - "line": 3, + "line": 1, }, }, "range": Array [ - 38, - 39, + 7, + 11, ], - "type": "Punctuator", - "value": ",", + "type": "Keyword", + "value": "enum", }, Object { "loc": Object { "end": Object { - "column": 1, - "line": 4, + "column": 15, + "line": 1, }, "start": Object { - "column": 0, - "line": 4, + "column": 12, + "line": 1, }, }, "range": Array [ - 40, - 41, + 12, + 15, + ], + "type": "Identifier", + "value": "Foo", + }, + Object { + "loc": Object { + "end": Object { + "column": 17, + "line": 1, + }, + "start": Object { + "column": 16, + "line": 1, + }, + }, + "range": Array [ + 16, + 17, + ], + "type": "Punctuator", + "value": "{", + }, + Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 2, + }, + "start": Object { + "column": 4, + "line": 2, + }, + }, + "range": Array [ + 22, + 23, + ], + "type": "Punctuator", + "value": "[", + }, + Object { + "loc": Object { + "end": Object { + "column": 10, + "line": 2, + }, + "start": Object { + "column": 5, + "line": 2, + }, + }, + "range": Array [ + 23, + 28, + ], + "type": "String", + "value": "'baz'", + }, + Object { + "loc": Object { + "end": Object { + "column": 11, + "line": 2, + }, + "start": Object { + "column": 10, + "line": 2, + }, + }, + "range": Array [ + 28, + 29, + ], + "type": "Punctuator", + "value": "]", + }, + Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 2, + }, + "start": Object { + "column": 11, + "line": 2, + }, + }, + "range": Array [ + 29, + 30, + ], + "type": "Punctuator", + "value": ",", + }, + Object { + "loc": Object { + "end": Object { + "column": 1, + "line": 3, + }, + "start": Object { + "column": 0, + "line": 3, + }, + }, + "range": Array [ + 31, + 32, + ], + "type": "Punctuator", + "value": "}", + }, + ], + "type": "Program", +} +`; + +exports[`typescript fixtures/basics/export-named-enum-computed-var-ref.src 1`] = ` +Object { + "body": Array [ + Object { + "declaration": Object { + "id": Object { + "loc": Object { + "end": Object { + "column": 15, + "line": 1, + }, + "start": Object { + "column": 12, + "line": 1, + }, + }, + "name": "Foo", + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + }, + "loc": Object { + "end": Object { + "column": 1, + "line": 3, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "members": Array [ + Object { + "computed": true, + "id": Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 2, + }, + "start": Object { + "column": 5, + "line": 2, + }, + }, + "name": "x", + "range": Array [ + 23, + 24, + ], + "type": "Identifier", + }, + "loc": Object { + "end": Object { + "column": 7, + "line": 2, + }, + "start": Object { + "column": 4, + "line": 2, + }, + }, + "range": Array [ + 22, + 25, + ], + "type": "TSEnumMember", + }, + ], + "range": Array [ + 7, + 28, + ], + "type": "TSEnumDeclaration", + }, + "loc": Object { + "end": Object { + "column": 1, + "line": 3, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 28, + ], + "source": null, + "specifiers": Array [], + "type": "ExportNamedDeclaration", + }, + ], + "loc": Object { + "end": Object { + "column": 0, + "line": 4, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 29, + ], + "sourceType": "module", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 6, + ], + "type": "Keyword", + "value": "export", + }, + Object { + "loc": Object { + "end": Object { + "column": 11, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 11, + ], + "type": "Keyword", + "value": "enum", + }, + Object { + "loc": Object { + "end": Object { + "column": 15, + "line": 1, + }, + "start": Object { + "column": 12, + "line": 1, + }, + }, + "range": Array [ + 12, + 15, + ], + "type": "Identifier", + "value": "Foo", + }, + Object { + "loc": Object { + "end": Object { + "column": 17, + "line": 1, + }, + "start": Object { + "column": 16, + "line": 1, + }, + }, + "range": Array [ + 16, + 17, + ], + "type": "Punctuator", + "value": "{", + }, + Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 2, + }, + "start": Object { + "column": 4, + "line": 2, + }, + }, + "range": Array [ + 22, + 23, + ], + "type": "Punctuator", + "value": "[", + }, + Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 2, + }, + "start": Object { + "column": 5, + "line": 2, + }, + }, + "range": Array [ + 23, + 24, + ], + "type": "Identifier", + "value": "x", + }, + Object { + "loc": Object { + "end": Object { + "column": 7, + "line": 2, + }, + "start": Object { + "column": 6, + "line": 2, + }, + }, + "range": Array [ + 24, + 25, + ], + "type": "Punctuator", + "value": "]", + }, + Object { + "loc": Object { + "end": Object { + "column": 8, + "line": 2, + }, + "start": Object { + "column": 7, + "line": 2, + }, + }, + "range": Array [ + 25, + 26, + ], + "type": "Punctuator", + "value": ",", + }, + Object { + "loc": Object { + "end": Object { + "column": 1, + "line": 3, + }, + "start": Object { + "column": 0, + "line": 3, + }, + }, + "range": Array [ + 27, + 28, ], "type": "Punctuator", "value": "}", From e732a127982a00e9cbc00f0ac391452c08b2548e Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 19 Dec 2019 14:28:23 +1030 Subject: [PATCH 7/7] test: add missing test case for err 17013 --- .../lib/__snapshots__/typescript.ts.snap | 156 ++++++++ .../new-target-in-arrow-function-body.src.ts | 1 + .../semantic-diagnostics-enabled.ts.snap | 9 + .../lib/__snapshots__/typescript.ts.snap | 335 ++++++++++++++++++ 4 files changed, 501 insertions(+) create mode 100644 packages/shared-fixtures/fixtures/typescript/basics/new-target-in-arrow-function-body.src.ts diff --git a/packages/parser/tests/lib/__snapshots__/typescript.ts.snap b/packages/parser/tests/lib/__snapshots__/typescript.ts.snap index b5be8d45758e..559d0637d08b 100644 --- a/packages/parser/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/parser/tests/lib/__snapshots__/typescript.ts.snap @@ -21170,6 +21170,162 @@ Object { } `; +exports[`typescript fixtures/basics/new-target-in-arrow-function-body.src 1`] = ` +Object { + "$id": 4, + "block": Object { + "range": Array [ + 0, + 28, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 3, + "block": Object { + "range": Array [ + 0, + 28, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 2, + "block": Object { + "range": Array [ + 10, + 26, + ], + "type": "ArrowFunctionExpression", + }, + "childScopes": Array [], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [], + "throughReferences": Array [], + "type": "function", + "upperScope": Object { + "$ref": 3, + }, + "variableMap": Object {}, + "variableScope": Object { + "$ref": 2, + }, + "variables": Array [], + }, + ], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [ + Object { + "$id": 1, + "from": Object { + "$ref": 3, + }, + "identifier": Object { + "name": "b", + "range": Array [ + 6, + 7, + ], + "type": "Identifier", + }, + "kind": "w", + "resolved": Object { + "$ref": 0, + }, + "writeExpr": Object { + "range": Array [ + 10, + 26, + ], + "type": "ArrowFunctionExpression", + }, + }, + ], + "throughReferences": Array [], + "type": "module", + "upperScope": Object { + "$ref": 4, + }, + "variableMap": Object { + "b": Object { + "$ref": 0, + }, + }, + "variableScope": Object { + "$ref": 3, + }, + "variables": Array [ + Object { + "$id": 0, + "defs": Array [ + Object { + "name": Object { + "name": "b", + "range": Array [ + 6, + 7, + ], + "type": "Identifier", + }, + "node": Object { + "range": Array [ + 6, + 26, + ], + "type": "VariableDeclarator", + }, + "parent": Object { + "range": Array [ + 0, + 27, + ], + "type": "VariableDeclaration", + }, + "type": "Variable", + }, + ], + "eslintUsed": undefined, + "identifiers": Array [ + Object { + "name": "b", + "range": Array [ + 6, + 7, + ], + "type": "Identifier", + }, + ], + "name": "b", + "references": Array [ + Object { + "$ref": 1, + }, + ], + "scope": Object { + "$ref": 3, + }, + }, + ], + }, + ], + "functionExpressionScope": false, + "isStrict": false, + "references": Array [], + "throughReferences": Array [], + "type": "global", + "upperScope": null, + "variableMap": Object {}, + "variableScope": Object { + "$ref": 4, + }, + "variables": Array [], +} +`; + exports[`typescript fixtures/basics/non-null-assertion-operator.src 1`] = ` Object { "$id": 10, diff --git a/packages/shared-fixtures/fixtures/typescript/basics/new-target-in-arrow-function-body.src.ts b/packages/shared-fixtures/fixtures/typescript/basics/new-target-in-arrow-function-body.src.ts new file mode 100644 index 000000000000..d5368dbbe38d --- /dev/null +++ b/packages/shared-fixtures/fixtures/typescript/basics/new-target-in-arrow-function-body.src.ts @@ -0,0 +1 @@ +const b = () => new.target; diff --git a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap index d19f40dee215..90817f0b9614 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap @@ -1928,6 +1928,15 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/never-type-param.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; +exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/new-target-in-arrow-function-body.src 1`] = ` +Object { + "column": 16, + "index": 16, + "lineNumber": 1, + "message": "Meta-property 'new.target' is only allowed in the body of a function declaration, function expression, or constructor.", +} +`; + exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/non-null-assertion-operator.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/null-and-undefined-type-annotations.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; diff --git a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap index 71ee0537b876..e53d5b3e9fb5 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap @@ -67683,6 +67683,341 @@ Object { } `; +exports[`typescript fixtures/basics/new-target-in-arrow-function-body.src 1`] = ` +Object { + "body": Array [ + Object { + "declarations": Array [ + Object { + "id": Object { + "loc": Object { + "end": Object { + "column": 7, + "line": 1, + }, + "start": Object { + "column": 6, + "line": 1, + }, + }, + "name": "b", + "range": Array [ + 6, + 7, + ], + "type": "Identifier", + }, + "init": Object { + "async": false, + "body": Object { + "loc": Object { + "end": Object { + "column": 26, + "line": 1, + }, + "start": Object { + "column": 16, + "line": 1, + }, + }, + "meta": Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 16, + "line": 1, + }, + }, + "name": "new", + "range": Array [ + 16, + 19, + ], + "type": "Identifier", + }, + "property": Object { + "loc": Object { + "end": Object { + "column": 26, + "line": 1, + }, + "start": Object { + "column": 20, + "line": 1, + }, + }, + "name": "target", + "range": Array [ + 20, + 26, + ], + "type": "Identifier", + }, + "range": Array [ + 16, + 26, + ], + "type": "MetaProperty", + }, + "expression": true, + "generator": false, + "id": null, + "loc": Object { + "end": Object { + "column": 26, + "line": 1, + }, + "start": Object { + "column": 10, + "line": 1, + }, + }, + "params": Array [], + "range": Array [ + 10, + 26, + ], + "type": "ArrowFunctionExpression", + }, + "loc": Object { + "end": Object { + "column": 26, + "line": 1, + }, + "start": Object { + "column": 6, + "line": 1, + }, + }, + "range": Array [ + 6, + 26, + ], + "type": "VariableDeclarator", + }, + ], + "kind": "const", + "loc": Object { + "end": Object { + "column": 27, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 27, + ], + "type": "VariableDeclaration", + }, + ], + "loc": Object { + "end": Object { + "column": 0, + "line": 2, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 28, + ], + "sourceType": "script", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 5, + ], + "type": "Keyword", + "value": "const", + }, + Object { + "loc": Object { + "end": Object { + "column": 7, + "line": 1, + }, + "start": Object { + "column": 6, + "line": 1, + }, + }, + "range": Array [ + 6, + 7, + ], + "type": "Identifier", + "value": "b", + }, + Object { + "loc": Object { + "end": Object { + "column": 9, + "line": 1, + }, + "start": Object { + "column": 8, + "line": 1, + }, + }, + "range": Array [ + 8, + 9, + ], + "type": "Punctuator", + "value": "=", + }, + Object { + "loc": Object { + "end": Object { + "column": 11, + "line": 1, + }, + "start": Object { + "column": 10, + "line": 1, + }, + }, + "range": Array [ + 10, + 11, + ], + "type": "Punctuator", + "value": "(", + }, + Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 11, + "line": 1, + }, + }, + "range": Array [ + 11, + 12, + ], + "type": "Punctuator", + "value": ")", + }, + Object { + "loc": Object { + "end": Object { + "column": 15, + "line": 1, + }, + "start": Object { + "column": 13, + "line": 1, + }, + }, + "range": Array [ + 13, + 15, + ], + "type": "Punctuator", + "value": "=>", + }, + Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 16, + "line": 1, + }, + }, + "range": Array [ + 16, + 19, + ], + "type": "Keyword", + "value": "new", + }, + Object { + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 19, + "line": 1, + }, + }, + "range": Array [ + 19, + 20, + ], + "type": "Punctuator", + "value": ".", + }, + Object { + "loc": Object { + "end": Object { + "column": 26, + "line": 1, + }, + "start": Object { + "column": 20, + "line": 1, + }, + }, + "range": Array [ + 20, + 26, + ], + "type": "Identifier", + "value": "target", + }, + Object { + "loc": Object { + "end": Object { + "column": 27, + "line": 1, + }, + "start": Object { + "column": 26, + "line": 1, + }, + }, + "range": Array [ + 26, + 27, + ], + "type": "Punctuator", + "value": ";", + }, + ], + "type": "Program", +} +`; + exports[`typescript fixtures/basics/non-null-assertion-operator.src 1`] = ` Object { "body": Array [ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy