diff --git a/src/error.ts b/src/error.ts index f7a99d1..3acf971 100644 --- a/src/error.ts +++ b/src/error.ts @@ -48,7 +48,6 @@ export const TypeScriptError = { 'You can either wrap the instantiation expression in parentheses, or delete the type arguments.', InvalidTupleMemberLabel: 'Tuple members must be labeled with a simple identifier.', MissingInterfaceName: "'interface' declarations must be followed by an identifier.", - MixedLabeledAndUnlabeledElements: 'Tuple members must all have names or all not have names.', NonAbstractClassHasAbstractMethod: 'Abstract methods can only appear within an abstract class.', NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.", diff --git a/src/index.ts b/src/index.ts index 09762fa..53c4f17 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1692,7 +1692,6 @@ export function tsPlugin(options?: { // Validate the elementTypes to ensure that no mandatory elements // follow optional elements let seenOptionalElement = false; - let labeledElements: boolean | null = null; node.elementTypes.forEach((elementNode) => { const { type } = elementNode; @@ -1709,16 +1708,8 @@ export function tsPlugin(options?: { (type === 'TSNamedTupleMember' && elementNode.optional) || type === 'TSOptionalType'; // When checking labels, check the argument of the spread operator - let checkType = type; if (type === 'TSRestType') { elementNode = elementNode.typeAnnotation; - checkType = elementNode.type; - } - - const isLabeled = checkType === 'TSNamedTupleMember'; - labeledElements ??= isLabeled; - if (labeledElements !== isLabeled) { - this.raise(elementNode.start, TypeScriptError.MixedLabeledAndUnlabeledElements); } }); diff --git a/test/tuple_named_and_anonymous_mixed/expected.json b/test/tuple_named_and_anonymous_mixed/expected.json new file mode 100644 index 0000000..43dded6 --- /dev/null +++ b/test/tuple_named_and_anonymous_mixed/expected.json @@ -0,0 +1,158 @@ +{ + "type": "Program", + "start": 0, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 0 + } + }, + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "name": "A" + }, + "typeAnnotation": { + "type": "TSTupleType", + "start": 9, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "elementTypes": [ + { + "type": "TSNamedTupleMember", + "start": 10, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "optional": false, + "label": { + "type": "Identifier", + "start": 10, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "name": "foo" + }, + "elementType": { + "type": "TSStringKeyword", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 21 + } + } + } + }, + { + "type": "TSRestType", + "start": 23, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "typeAnnotation": { + "type": "TSArrayType", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 32 + } + } + } + } + } + ] + } + } + ], + "sourceType": "module" +} diff --git a/test/tuple_named_and_anonymous_mixed/input.ts b/test/tuple_named_and_anonymous_mixed/input.ts new file mode 100644 index 0000000..20be884 --- /dev/null +++ b/test/tuple_named_and_anonymous_mixed/input.ts @@ -0,0 +1 @@ +type A = [foo: string, ...number[]];