diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index 1fce8241a5fe5ac03250c14c72881c93c0f6436c..c1af0bba703088d53378784046cc79f9e4a9fd07 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -831,7 +831,7 @@ ir::Expression *ParserImpl::ParseTsTupleElement(ir::TSTupleKind *kind, bool *see lexer_->NextToken(); // eat '?' isOptional = true; *seenOptional = true; - } else if (*seenOptional) { + } else if (*seenOptional && !isRestType) { ThrowSyntaxError("A required element cannot follow an optional element"); } @@ -864,6 +864,10 @@ ir::Expression *ParserImpl::ParseTsTupleElement(ir::TSTupleKind *kind, bool *see element = AllocNode(std::move(element)); element->SetRange({elementStartPos, lexer_->GetToken().End()}); lexer_->NextToken(); // eat '?' + isOptional = true; + *seenOptional = true; + } else if (*seenOptional && !isRestType) { + ThrowSyntaxError("A required element cannot follow an optional element"); } } return element; @@ -2400,7 +2404,7 @@ void ParserImpl::CheckClassPrivateIdentifier(ClassElmentDescriptor *desc) return; } - if (desc->modifiers != ir::ModifierFlags::NONE) { + if (desc->modifiers & ~ir::ModifierFlags::READONLY) { ThrowSyntaxError("Unexpected modifier on private identifier"); } diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index 6c8250593d955a485fc1c5878d2f04ac0338750b..aea10c255caa603bb9a10b3bfc2c429aa47a9345 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -1949,7 +1949,8 @@ ir::VariableDeclarator *ParserImpl::ParseVariableDeclarator(VariableParsingFlags ThrowSyntaxError("Missing initializer in const declaration"); } - if (!(flags & VariableParsingFlags::IN_FOR) && (init->IsArrayPattern() || init->IsObjectPattern())) { + if (!isDeclare && !(flags & VariableParsingFlags::IN_FOR) && + (init->IsArrayPattern() || init->IsObjectPattern())) { ThrowSyntaxError("Missing initializer in destructuring declaration"); } diff --git a/es2panda/test/parser/ts/test-destructure-declaration-expected.txt b/es2panda/test/parser/ts/test-destructure-declaration-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..f3f5547d009c74d183c3445f2106307543f61bc7 --- /dev/null +++ b/es2panda/test/parser/ts/test-destructure-declaration-expected.txt @@ -0,0 +1,231 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "ArrayPattern", + "elements": [ + { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "kind": "var", + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "ObjectPattern", + "properties": [ + { + "type": "Property", + "method": false, + "shorthand": true, + "computed": false, + "key": { + "type": "Identifier", + "name": "c", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "value": { + "type": "Identifier", + "name": "c", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": true, + "computed": false, + "key": { + "type": "Identifier", + "name": "d", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "value": { + "type": "Identifier", + "name": "d", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 19 + } + } + } + ], + "kind": "var", + "declare": true, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 18, + "column": 20 + } + } +} diff --git a/es2panda/test/parser/ts/test-destructure-declaration.ts b/es2panda/test/parser/ts/test-destructure-declaration.ts new file mode 100644 index 0000000000000000000000000000000000000000..f913fd152e99de5cb4cbb9f6dc736a48cce0aa4e --- /dev/null +++ b/es2panda/test/parser/ts/test-destructure-declaration.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +declare var [a, b]; +declare var {c, d}; \ No newline at end of file diff --git a/es2panda/test/parser/ts/test-private-identifier-expected.txt b/es2panda/test/parser/ts/test-private-identifier-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..7d35e2485bd9cc541f1c904095ff50412a00bdff --- /dev/null +++ b/es2panda/test/parser/ts/test-private-identifier-expected.txt @@ -0,0 +1,338 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "TSPrivateIdentifier", + "key": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 7 + } + } + }, + "value": { + "type": "StringLiteral", + "value": "b", + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 7 + } + } + }, + "value": { + "type": "StringLiteral", + "value": "b", + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "value": { + "type": "StringLiteral", + "value": "a", + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "static": false, + "readonly": true, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "TSPrivateIdentifier", + "key": { + "type": "Identifier", + "name": "c", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 15 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + "value": { + "type": "StringLiteral", + "value": "c", + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + "value": { + "type": "StringLiteral", + "value": "c", + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 22 + } + } + }, + "static": false, + "readonly": true, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 22 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } +} diff --git a/es2panda/test/parser/ts/test-private-identifier.ts b/es2panda/test/parser/ts/test-private-identifier.ts new file mode 100644 index 0000000000000000000000000000000000000000..db2f85b9a4acefb65df34223486005a6d1d4ecd8 --- /dev/null +++ b/es2panda/test/parser/ts/test-private-identifier.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +class A { + #b = 'b'; + readonly a = 'a'; + readonly #c = 'c'; +} \ No newline at end of file diff --git a/es2panda/test/parser/ts/test-tuple-type11-expected.txt b/es2panda/test/parser/ts/test-tuple-type11-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..35f4840365b4da4800bd705dee0c1c0db9f5cdc1 --- /dev/null +++ b/es2panda/test/parser/ts/test-tuple-type11-expected.txt @@ -0,0 +1 @@ +SyntaxError: A required element cannot follow an optional element [test-tuple-type11.ts:18:50] diff --git a/es2panda/test/parser/ts/test-tuple-type11.ts b/es2panda/test/parser/ts/test-tuple-type11.ts new file mode 100644 index 0000000000000000000000000000000000000000..27a1d3676a1be61dc12d30b53bc047a1d0f80da5 --- /dev/null +++ b/es2panda/test/parser/ts/test-tuple-type11.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +type WithOptAndRest1 = [number, number?, ...string[]]; +type WithOptAndRest2 = [number, number?, string[]]; \ No newline at end of file diff --git a/es2panda/test/parser/ts/test-tuple-type6-expected.txt b/es2panda/test/parser/ts/test-tuple-type6-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..02a117c4e6bbded1a36b0094eb17f75d1750d2c6 --- /dev/null +++ b/es2panda/test/parser/ts/test-tuple-type6-expected.txt @@ -0,0 +1,209 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "WithOptAndRest", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "typeAnnotation": { + "type": "TSTupleType", + "elementTypes": [ + { + "type": "TSNamedTupleMember", + "elementType": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 17, + "column": 31 + }, + "end": { + "line": 17, + "column": 37 + } + } + }, + "label": { + "type": "Identifier", + "name": "first", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 37 + } + } + }, + { + "type": "TSNamedTupleMember", + "elementType": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 17, + "column": 48 + }, + "end": { + "line": 17, + "column": 54 + } + } + }, + "label": { + "type": "Identifier", + "name": "second", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 45 + } + } + }, + "optional": true, + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 54 + } + } + }, + { + "type": "TSNamedTupleMember", + "elementType": { + "type": "TSArrayType", + "elementType": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 17, + "column": 65 + }, + "end": { + "line": 17, + "column": 71 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 65 + }, + "end": { + "line": 17, + "column": 73 + } + } + }, + "label": { + "type": "Identifier", + "name": "rest", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 59 + }, + "end": { + "line": 17, + "column": 63 + } + } + }, + "rest": true, + "loc": { + "start": { + "line": 17, + "column": 56 + }, + "end": { + "line": 17, + "column": 73 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 74 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 75 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 17, + "column": 74 + }, + "end": { + "line": 17, + "column": 75 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 17, + "column": 75 + } + } +} diff --git a/es2panda/test/parser/ts/test-tuple-type6.ts b/es2panda/test/parser/ts/test-tuple-type6.ts new file mode 100644 index 0000000000000000000000000000000000000000..a7883e92fce93c30427b6e887a6fa1e2640a4359 --- /dev/null +++ b/es2panda/test/parser/ts/test-tuple-type6.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +type WithOptAndRest = [first: number, second?: number, ...rest: string[]]; \ No newline at end of file diff --git a/es2panda/test/test_tsc_ignore_list.txt b/es2panda/test/test_tsc_ignore_list.txt index 1bebc33d4aaaa59aabfebdc04c688fc9c62d78b1..4aff71e0390efaf87a9c8b605636bdf8c090b9a1 100644 --- a/es2panda/test/test_tsc_ignore_list.txt +++ b/es2panda/test/test_tsc_ignore_list.txt @@ -36,7 +36,6 @@ es2panda/test/TypeScript/tests/cases/compiler/sourceMapValidationDestructuringFo es2panda/test/TypeScript/tests/cases/compiler/sourceMapValidationDestructuringForObjectBindingPatternDefaultValues2.ts es2panda/test/TypeScript/tests/cases/compiler/withStatementInternalComments.ts es2panda/test/TypeScript/tests/cases/conformance/async/es2017/asyncAwait_es2017.ts -es2panda/test/TypeScript/tests/cases/conformance/classes/members/privateNames/privateNameComputedPropertyName1.ts es2panda/test/TypeScript/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments03_ES6.ts es2panda/test/TypeScript/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments04_ES6.ts es2panda/test/TypeScript/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments05_ES6.ts @@ -51,7 +50,6 @@ es2panda/test/TypeScript/tests/cases/conformance/es6/arrowFunction/emitArrowFunc es2panda/test/TypeScript/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments15_ES6.ts es2panda/test/TypeScript/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments16_ES6.ts es2panda/test/TypeScript/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments17_ES6.ts -es2panda/test/TypeScript/tests/cases/conformance/es6/destructuring/declarationInAmbientContext.ts es2panda/test/TypeScript/tests/cases/conformance/es6/for-ofStatements/for-of53.ts es2panda/test/TypeScript/tests/cases/conformance/es6/for-ofStatements/for-of56.ts es2panda/test/TypeScript/tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration11_es6.ts @@ -87,7 +85,6 @@ es2panda/test/TypeScript/tests/cases/conformance/scanner/ecmascript3/scannerES3N es2panda/test/TypeScript/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts es2panda/test/TypeScript/tests/cases/conformance/types/members/objectTypeWithStringNamedNumericProperty.ts es2panda/test/TypeScript/tests/cases/conformance/types/specifyingTypes/typeQueries/typeQueryWithReservedWords.ts -es2panda/test/TypeScript/tests/cases/conformance/types/tuple/named/namedTupleMembers.ts es2panda/test/TypeScript/tests/cases/conformance/types/typeParameters/typeArgumentLists/constraintSatisfactionWithEmptyObject.ts es2panda/test/TypeScript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbols.ts es2panda/test/TypeScript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsDeclarations.ts