diff --git a/.changeset/common-states-cut.md b/.changeset/common-states-cut.md new file mode 100644 index 0000000..92c1c3d --- /dev/null +++ b/.changeset/common-states-cut.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/acorn-typescript': patch +--- + +fix: parse type assertions diff --git a/CHANGELOG.md b/CHANGELOG.md index 509fb3b..825c32f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,367 +1 @@ # Changelog - -All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. - -### [1.4.13](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.12...v1.4.13) (2024-01-03) - -### โœจ Features - -- ๐Ÿ›support satisfies ([21baa52](https://github.com/TyrealHu/acorn-typescript/commit/21baa522732d93fb010b95622dd30f016f0ce814)) - -### [1.4.12](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.11...v1.4.12) (2023-12-05) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›the scope flag between type value and ident ([da26d27](https://github.com/TyrealHu/acorn-typescript/commit/da26d27a181d18ff615cd078459b0d4847c5faee)) - -### [1.4.11](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.10...v1.4.11) (2023-11-10) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›parse tagged template expression with type ([575ab8c](https://github.com/TyrealHu/acorn-typescript/commit/575ab8cb3e209d45fa09e8dba784b18ec734783f)) - -### [1.4.10](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.9...v1.4.10) (2023-10-20) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›bugs of left relational and right relational ([de5984c](https://github.com/TyrealHu/acorn-typescript/commit/de5984c67eec2e46d199067a6691557cd6923a21)) - -### [1.4.9](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.8...v1.4.9) (2023-10-18) - -### โ™ป๏ธ Code Refactoring - -- ๐ŸŒŸset jsx options to true, while its empty ([77e26cd](https://github.com/TyrealHu/acorn-typescript/commit/77e26cd54947242b6b994cd24e4ff476422edeb9)) - -### [1.4.8](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.7...v1.4.8) (2023-10-09) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ› parse import specifier with keyword ([91a27a9](https://github.com/TyrealHu/acorn-typescript/commit/91a27a90800e37a41a650ba71be6b891128e9f83)) - -### [1.4.7](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.6...v1.4.7) (2023-09-21) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ› JSX token parse in ! > ! case ([a519fab](https://github.com/TyrealHu/acorn-typescript/commit/a519fab69a9d8e16af95f97265d53a6f19cc2f0b)) - -### [1.4.6](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.5...v1.4.6) (2023-09-20) - -### โœจ Features - -- ๐Ÿš€ support import assertions and type check ([e6d286d](https://github.com/TyrealHu/acorn-typescript/commit/e6d286d0f2ba5b63413d6a4b1062f955756dc0a5)) - -### [1.4.5](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.4...v1.4.5) (2023-09-06) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›checkLValInnerPattern in TSParameterProperty ([6de3ebd](https://github.com/TyrealHu/acorn-typescript/commit/6de3ebd0773f8051598fde971b313859564d34c0)) - -### [1.4.4](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.3...v1.4.4) (2023-08-30) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›bugs of TSTypeCastExpression ([22be8a3](https://github.com/TyrealHu/acorn-typescript/commit/22be8a33bf09f49966e33e352d27a9f93fd9ae9e)) - -### [1.4.3](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.2...v1.4.3) (2023-08-25) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ› optional class property & class method generic types & arrow function generic type ([998969a](https://github.com/TyrealHu/acorn-typescript/commit/998969a5f86a0c032e9422f3bb33791d49c7214d)) - -### [1.4.2](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.1...v1.4.2) (2023-08-24) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›modifier params in class method ([5a06b09](https://github.com/TyrealHu/acorn-typescript/commit/5a06b09b488d0c66454f736490949a5b4b3c71c7)) - -### [1.4.1](https://github.com/TyrealHu/acorn-typescript/compare/v1.4.0...v1.4.1) (2023-07-26) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›bugs in parseExprList ([399f38e](https://github.com/TyrealHu/acorn-typescript/commit/399f38e03611710b29ad844c94cb674a54e345a7)) - -## [1.4.0](https://github.com/TyrealHu/acorn-typescript/compare/v1.3.7...v1.4.0) (2023-07-03) - -### โœจ Features - -- ๐Ÿš€rewrite some methonds in acorn 8.9.0 ([8666259](https://github.com/TyrealHu/acorn-typescript/commit/8666259cf6665bb7ac2459089422530641992451)) - -### [1.3.7](https://github.com/TyrealHu/acorn-typescript/compare/v1.3.6...v1.3.7) (2023-06-14) - -### โœจ Features - -- ๐Ÿš€add locations validating in static method ([e707b64](https://github.com/TyrealHu/acorn-typescript/commit/e707b642141bb4ad10572d18c78b4ee2be4d27a5)) - -### [1.3.6](https://github.com/TyrealHu/acorn-typescript/compare/v1.3.5...v1.3.6) (2023-06-08) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›error dependencies in index.d.ts ([0a04e07](https://github.com/TyrealHu/acorn-typescript/commit/0a04e07048070e6a0d97c3c5c85d1a7463bbfc5f)) - -### [1.3.5](https://github.com/TyrealHu/acorn-typescript/compare/v1.3.4...v1.3.5) (2023-06-06) - -### โ™ป๏ธ Code Refactoring - -- position checkLValSimple in function ([038e588](https://github.com/TyrealHu/acorn-typescript/commit/038e588f4ad99ec3fca16e5d7753e8b967de230f)) - -### [1.3.4](https://github.com/TyrealHu/acorn-typescript/compare/v1.3.3...v1.3.4) (2023-06-06) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›duplicate name in dts ([d608373](https://github.com/TyrealHu/acorn-typescript/commit/d60837307df9c3559512f39524d6bd0dd1c48ba4)) - -### [1.3.3](https://github.com/TyrealHu/acorn-typescript/compare/v1.3.2...v1.3.3) (2023-06-02) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›condition expression dont throw error ([1779760](https://github.com/TyrealHu/acorn-typescript/commit/177976095cba46efcfc03659c0be3c857c01bee4)) - -### [1.3.2](https://github.com/TyrealHu/acorn-typescript/compare/v1.3.1...v1.3.2) (2023-06-01) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›arrow function with optinal param ([163f0cf](https://github.com/TyrealHu/acorn-typescript/commit/163f0cf5a2275a22959ba7b94b507a68ade3ec6a)) - -### [1.3.1](https://github.com/TyrealHu/acorn-typescript/compare/v1.3.0...v1.3.1) (2023-05-31) - -### โ™ป๏ธ Code Refactoring - -- allow comma at the end of generics ([3e0bf3d](https://github.com/TyrealHu/acorn-typescript/commit/3e0bf3d60a1ea090288a297e1c562d631177ea53)) -- allow comma at the end of generics ([fa1b6cf](https://github.com/TyrealHu/acorn-typescript/commit/fa1b6cfc826d10325854a50850a3f7329189dbf3)) - -## [1.3.0](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.10...v1.3.0) (2023-05-31) - -### ๐Ÿ› Bug Fixes - -- parse generics with trailing comma ([028d787](https://github.com/TyrealHu/acorn-typescript/commit/028d787dc463a34a821f8fc415b3399abb1f40e9)) - -### [1.2.10](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.9...v1.2.10) (2023-05-29) - -### โ™ป๏ธ Code Refactoring - -- async arrow function with one param ([2cd5a29](https://github.com/TyrealHu/acorn-typescript/commit/2cd5a29c61f5c2248eb84d74d1b84c9752777f97)) - -### ๐Ÿ› Bug Fixes - -- async in for of without decl ([c73f2f0](https://github.com/TyrealHu/acorn-typescript/commit/c73f2f0e9eb0e7762171da97722f87c25408d2da)) - -### [1.2.9](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.8...v1.2.9) (2023-05-29) - -### ๐Ÿ› Bug Fixes - -- eatContextual and reduce keywords ([d280912](https://github.com/TyrealHu/acorn-typescript/commit/d2809122f4d89013c89c00234926dc1f24ede606)) -- syntax error in async arrow function with one param ([d13794e](https://github.com/TyrealHu/acorn-typescript/commit/d13794e168a57d90e96d06c2a69279490636e73c)) -- syntax error in catch redeclared var statement captured ([cd6f0de](https://github.com/TyrealHu/acorn-typescript/commit/cd6f0ded06f22169eb360c9a053a1976ca41fd3a)) -- syntax error in escaped keyword property ([2c11ea9](https://github.com/TyrealHu/acorn-typescript/commit/2c11ea98d5f1aa601ee619a7b5b213c880a98b63)) -- syntax error in identifier named `as` in export and import declarations ([ad797e8](https://github.com/TyrealHu/acorn-typescript/commit/ad797e8a4761e24b25ce2192bf6f333870ea9e1c)) -- to raise error in escaped static method ([0c3f381](https://github.com/TyrealHu/acorn-typescript/commit/0c3f381198a191a9d3b6ecb422f69dc0e81afb77)) -- to raise error in multiple constructors ([20ab1bd](https://github.com/TyrealHu/acorn-typescript/commit/20ab1bdd5c85dc767ef4126a758f921eca60192a)) - -### [1.2.8](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.7...v1.2.8) (2023-05-26) - -### ๐Ÿ› Bug Fixes - -- syntax error in async generator function ([94c3cfa](https://github.com/TyrealHu/acorn-typescript/commit/94c3cfabd54e360f45dc7e78f076d59e8a8646c0)) -- syntax error in export default anonymous class ([af10901](https://github.com/TyrealHu/acorn-typescript/commit/af10901ebd1306289d020c4b5ca4615b23ab01d0)) -- syntax error in for-in without decl ([c544039](https://github.com/TyrealHu/acorn-typescript/commit/c5440398da480de7ef8f421495ed9fb7cf826593)) -- syntax error in static async methods ([eba80f4](https://github.com/TyrealHu/acorn-typescript/commit/eba80f48c5e92c42fe594c9ada7dfc831b0386bc)) -- syntax error in static getter/setter ([e8fb775](https://github.com/TyrealHu/acorn-typescript/commit/e8fb77586897610b7fc893b1eb275c92910c1af2)) -- syntax error when arrow function parameter is assignment pattern ([7f84e5c](https://github.com/TyrealHu/acorn-typescript/commit/7f84e5c29afdf74a0306547072cde2156c02a76d)) - -### [1.2.7](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.6...v1.2.7) (2023-05-25) - -### โ™ป๏ธ Code Refactoring - -- use mjs instead of esm ([8700da9](https://github.com/TyrealHu/acorn-typescript/commit/8700da9e7bda11043a39329a7693a8b85c5eec45)) - -### [1.2.6](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.5...v1.2.6) (2023-05-25) - -### โœจ Features - -- add exports in pkg ([3707c21](https://github.com/TyrealHu/acorn-typescript/commit/3707c21b9366eb7bdbfa4b7529876b8a4bf96109)) - -### [1.2.5](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.4...v1.2.5) (2023-05-25) - -### ๐Ÿ› Bug Fixes - -- parseExpressionAt ([57fe8bb](https://github.com/TyrealHu/acorn-typescript/commit/57fe8bbc1e020774deffe1ff5a12045666cd83f5)) - -### [1.2.4](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.3...v1.2.4) (2023-05-18) - -### ๐Ÿ› Bug Fixes - -- bugs of exports ([ca33992](https://github.com/TyrealHu/acorn-typescript/commit/ca339928e51df18805078801681025b31169982a)) -- support import default in commonjs tsc ([bb69884](https://github.com/TyrealHu/acorn-typescript/commit/bb69884dd47b636096c41a3ffd021dde84703c1f)) - -### [1.2.3](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.2...v1.2.3) (2023-05-18) - -### ๐Ÿ› Bug Fixes - -- support import default in commonjs tsc ([bf219cf](https://github.com/TyrealHu/acorn-typescript/commit/bf219cff0e79632bbfc3835e7b626d4af41b4223)) - -### [1.2.2](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.1...v1.2.2) (2023-05-18) - -### ๐Ÿ› Bug Fixes - -- use export instead of export default ([8984560](https://github.com/TyrealHu/acorn-typescript/commit/89845609d010f1844c0792c3cfa0f88ac72a5023)) - -### [1.2.1](https://github.com/TyrealHu/acorn-typescript/compare/v1.2.0...v1.2.1) (2023-05-18) - -### โœจ Features - -- ๐Ÿš€ support parse accessor modifier ([ac1e54b](https://github.com/TyrealHu/acorn-typescript/commit/ac1e54b48f33699a1f2aac871e659e41c1bcd9ed)) - -## [1.2.0](https://github.com/TyrealHu/acorn-typescript/compare/v1.1.2...v1.2.0) (2023-04-03) - -### โœจ Features - -- ๐Ÿš€ support parse jsx and tsx ([0e5974c](https://github.com/TyrealHu/acorn-typescript/commit/0e5974ce0505cb56422d6d6db1e76652c17415bc)) - -### [1.1.2](https://github.com/TyrealHu/acorn-typescript/compare/v1.1.1...v1.1.2) (2023-03-23) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›the bugs of isContextual ([fcf13f3](https://github.com/TyrealHu/acorn-typescript/commit/fcf13f3634788055d1ef02cfd7aff4c6a7ce7e4d)) - -### [1.1.1](https://github.com/TyrealHu/acorn-typescript/compare/v1.1.0...v1.1.1) (2023-03-23) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ› throw error while parsing for...of ([26495a5](https://github.com/TyrealHu/acorn-typescript/commit/26495a56b346278ea12c6db13fab5f92e0981392)) - -## [1.1.0](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.13...v1.1.0) (2023-03-22) - -### โœจ Features - -- ๐Ÿš€ support parse typescript decorators ([01e0ef8](https://github.com/TyrealHu/acorn-typescript/commit/01e0ef878b34080f6aac867c80e3dcff6b2f79c0)) - -### [1.0.13](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.12...v1.0.13) (2023-03-08) - -### โœจ Features - -- ๐Ÿš€ add acornTypeScript in runtime ([26414f5](https://github.com/TyrealHu/acorn-typescript/commit/26414f5779df124b92e9330b239acc915d970296)) -- ๐Ÿš€ get acornJsx from Parser ([bd3dd82](https://github.com/TyrealHu/acorn-typescript/commit/bd3dd822a95721e7e2eae9d9842c5a14b4ad2242)) - -### [1.0.12](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.11...v1.0.12) (2023-03-06) - -### โœจ Features - -- ๐Ÿš€add acornTypeScript to export some methods and tokenTypes ([a113a0e](https://github.com/TyrealHu/acorn-typescript/commit/a113a0e0573c8e30cf76b259e0f9c6aaf5d0fd41)) - -### [1.0.11](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.10...v1.0.11) (2023-03-02) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ›parse definite assignment ([710773b](https://github.com/TyrealHu/acorn-typescript/commit/710773b4368151329166ce2775a139ddcfbf1aa0)) - -### [1.0.10](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.9...v1.0.10) (2023-03-01) - -### ๐Ÿ› Bug Fixes - -- parse async arrow in subscript ([f9beb65](https://github.com/TyrealHu/acorn-typescript/commit/f9beb650d8ffc6ee6def8b9f76fda0b68ecd213b)) - -### [1.0.9](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.8...v1.0.9) (2023-02-28) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ› return type location and expr and distinguish arrow function parse ([ffbc4ee](https://github.com/TyrealHu/acorn-typescript/commit/ffbc4ee4415b384fc22209c5e406f1d2816a95e6)) - -### โœจ Features - -- ๐Ÿš€ unit test of express arrow function and expression with paren ([e77b2e9](https://github.com/TyrealHu/acorn-typescript/commit/e77b2e960839c8177e3b5d73a23229527e0822b1)) - -### [1.0.8](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.7...v1.0.8) (2023-02-23) - -### โœจ Features - -- ๐Ÿš€add outer kind variable to help parse import or export ([65bd2ad](https://github.com/TyrealHu/acorn-typescript/commit/65bd2ad8190ef11f5454158c10ad101728323fe6)) - -### ๐Ÿ› Bug Fixes - -- ๐Ÿ› check comma while parse binding list & methods abstract ([ccc7866](https://github.com/TyrealHu/acorn-typescript/commit/ccc7866163a718749e577c6c1e4edf6135fd64a9)) -- ๐Ÿ› check the position number while raise error ([55dd763](https://github.com/TyrealHu/acorn-typescript/commit/55dd763a9e9e7a81ede7956d97a84347a30dd605)) - -### [1.0.7](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.6...v1.0.7) (2023-02-20) - -### โœจ Features - -- ๐Ÿš€acorn middleware class and delete ts-ignore ([7d05f36](https://github.com/TyrealHu/acorn-typescript/commit/7d05f36b355da00e309c39c61c62b9b206d7d7da)) - -### [1.0.6](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.5...v1.0.6) (2023-02-15) - -### โœจ Features - -- parser of ClassMethods ([2218d77](https://github.com/TyrealHu/acorn-typescript/commit/2218d775703da57100620073b60bef01822b6040)) - -### [1.0.5](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.4...v1.0.5) (2023-01-12) - -### โœจ Features - -- **tyreal:** only export cjs and esm, minimum the pkg ([1c46756](https://github.com/TyrealHu/acorn-typescript/commit/1c467569523b79ec4b590a8025f989dc966abea8)) - -### [1.0.4](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.3...v1.0.4) (2023-01-11) - -### โœจ Features - -- **tyreal:** package desc and change test file ([5ca6959](https://github.com/TyrealHu/acorn-typescript/commit/5ca6959b626fc71822dd9fe5c8b7db1b69e40da1)) - -### [1.0.3](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.2...v1.0.3) (2023-01-11) - -### โœจ Features - -- add CHANGELOG jump ([ccdaa25](https://github.com/TyrealHu/acorn-typescript/commit/ccdaa2575dbf70c5bdcdb3c523555e34d08faf3c)) - -### [1.0.2](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.1...v1.0.2) (2023-01-11) - -### โœจ Features - -- minimum package ([5efe8d3](https://github.com/TyrealHu/acorn-typescript/commit/5efe8d304e71463b4a8cc3bc0e2f016809a9b01b)) - -### [1.0.1](https://github.com/TyrealHu/acorn-typescript/compare/v1.0.0...v1.0.1) (2023-01-11) - -### โœจ Features - -- minimum package ([7eaa353](https://github.com/TyrealHu/acorn-typescript/commit/7eaa3535295ff446bab3d351cd98b64041926519)) - -## 1.0.0 (2023-01-11) - -### ๐Ÿ› Bug Fixes - -- **tyreal:** reset location ([b74bc04](https://github.com/TyrealHu/acorn-typescript/commit/b74bc044505d26cd5dc064ac295fed420f429a7d)) - -### โœจ Features - -- ๐Ÿš€complete parseMaybeDecoratorArguments and ts class ([b5eb8c8](https://github.com/TyrealHu/acorn-typescript/commit/b5eb8c8bea837151c121a29262af50ec1aefdf03)) -- ๐Ÿš€support ts parse default import ([93de410](https://github.com/TyrealHu/acorn-typescript/commit/93de41060b2485de0e0ee1307153580acfe172e0)) -- add import and export type as ([8a27090](https://github.com/TyrealHu/acorn-typescript/commit/8a270902058dc141c9b03d693bf7b33dfacc51c0)) -- complete parse middleware ([dfb76ab](https://github.com/TyrealHu/acorn-typescript/commit/dfb76ab74f77bdfea85fee01a96ab30425f78475)) -- origin export ([5a4cefa](https://github.com/TyrealHu/acorn-typescript/commit/5a4cefaf9d8035b27fa36bdb314bc95de7671797)) -- parseArrayLiker ([c40fb84](https://github.com/TyrealHu/acorn-typescript/commit/c40fb84b8249c57911b1d03710e835d159fa6258)) -- parseClassId ([76f1459](https://github.com/TyrealHu/acorn-typescript/commit/76f1459957863cbd2be81cd73effdd520b0eda5a)) -- parseImport ([d2eab00](https://github.com/TyrealHu/acorn-typescript/commit/d2eab00b295e5de6e1f56e485c11f91477f5b713)) -- parseNew ([9f55c7d](https://github.com/TyrealHu/acorn-typescript/commit/9f55c7dabd934e752e595b6005b93f3136c61884)) -- parseSubscript ([1d55b72](https://github.com/TyrealHu/acorn-typescript/commit/1d55b723189d9fcb3ec67d06dab5520d6c254ff8)) -- parseVarId ([7c21967](https://github.com/TyrealHu/acorn-typescript/commit/7c219670737a5b7bac6da930a01f2329100115c0)) -- reportReservedArrowTypeParam, next parseMaybeAssign ([037b675](https://github.com/TyrealHu/acorn-typescript/commit/037b675a0710179ddc36f62e196e80f36a8b6747)) -- support declare and export/import type ([a745431](https://github.com/TyrealHu/acorn-typescript/commit/a745431105703669541818e659b5092d9c375123)) -- **tyreal:** 'let contextual is false ([bf9e593](https://github.com/TyrealHu/acorn-typescript/commit/bf9e5935fa27a0e2693aa3ef1ad87f29e815ccbc)) -- **tyreal:** add jest and bundler ([d0212b7](https://github.com/TyrealHu/acorn-typescript/commit/d0212b7dca75a45f8566caf3c0b963013673d888)) -- **tyreal:** add ts jest ([96459f0](https://github.com/TyrealHu/acorn-typescript/commit/96459f09e94921aab7275407a21e98d07854ef10)) -- **tyreal:** class method, and normal property ([fbf22ec](https://github.com/TyrealHu/acorn-typescript/commit/fbf22eccc9c691f58d461c960d1aa0ee4085f6f1)) -- **tyreal:** class modifiers ([23510de](https://github.com/TyrealHu/acorn-typescript/commit/23510deb7d6e7afb95956b3cd72a788758847f9f)) -- **tyreal:** class support abstract and add unit test ([5b7b9b5](https://github.com/TyrealHu/acorn-typescript/commit/5b7b9b535f85652b1d6c621d5f356d67529fb63a)) -- **tyreal:** export test ([9889601](https://github.com/TyrealHu/acorn-typescript/commit/98896013ec50d5a4c0883eb75074426857bddb7b)) -- **tyreal:** finish import normal test ([146e0d6](https://github.com/TyrealHu/acorn-typescript/commit/146e0d644896b96dafd71e1f81801aa3941b1b8e)) -- **tyreal:** finish no parameters function test ([647032e](https://github.com/TyrealHu/acorn-typescript/commit/647032e27cfa7950421e051cc23671fe3716529d)) -- **tyreal:** finish the development of export unit test ([e71b85e](https://github.com/TyrealHu/acorn-typescript/commit/e71b85ef6f77e8c0ed041ab408b4fbe2f60dbbb8)) -- **tyreal:** function with paramaters ([59814c5](https://github.com/TyrealHu/acorn-typescript/commit/59814c55feee873eb842904c302405b6325a0b8f)) -- **tyreal:** generate snapshot ([abe8198](https://github.com/TyrealHu/acorn-typescript/commit/abe81989013a89bb457940461a29baf31796bbab)) -- **tyrealhu:** WIP parseClassElement ([369c2c7](https://github.com/TyrealHu/acorn-typescript/commit/369c2c70a36972fdeab977a01110ccc43b50e3fb)) -- **tyreal:** import type ut ([5cf8686](https://github.com/TyrealHu/acorn-typescript/commit/5cf86868593858508a069b63a6a2dcf40070fefa)) -- **tyreal:** interface and type unit test ([b010bca](https://github.com/TyrealHu/acorn-typescript/commit/b010bcabc1a249f81f2503092e1fd41e487b734f)) -- **tyreal:** remove ignore file ([a3ea153](https://github.com/TyrealHu/acorn-typescript/commit/a3ea153e812d41f8e3db2ca813da90be20668fe0)) -- **tyreal:** snapshot map structure ([0ee2778](https://github.com/TyrealHu/acorn-typescript/commit/0ee27781c1b396f0763b27b9c32a923dc7999816)) -- **tyreal:** support class static ([ac450e4](https://github.com/TyrealHu/acorn-typescript/commit/ac450e4654cd2d35ee335875bbba9b9d9971a86f)) -- **tyreal:** type identifier and export type unit test ([6ac96da](https://github.com/TyrealHu/acorn-typescript/commit/6ac96da1c7499d653ad5ab1e7fc0e5e20753d99e)) -- **tyreal:** variables test ([35bf1a9](https://github.com/TyrealHu/acorn-typescript/commit/35bf1a909a98f901f5097414f95f214123c07806)) -- **tyreal:** write one test and fix parameters of typescript parser ([cdbbd6c](https://github.com/TyrealHu/acorn-typescript/commit/cdbbd6c8d7ec8ffbbb162218a3cb013d0abb8001)) diff --git a/__test__/__snapshot__/expression/casts.ts b/__test__/__snapshot__/expression/casts.ts new file mode 100644 index 0000000..c78a60d --- /dev/null +++ b/__test__/__snapshot__/expression/casts.ts @@ -0,0 +1,782 @@ +const TypeCastsSnapshot = { + OneAsNumber: { + type: 'Program', + start: 0, + end: 22, + loc: { + start: { line: 1, column: 0, index: 0 }, + end: { line: 1, column: 22, index: 22 } + }, + body: [ + { + type: 'VariableDeclaration', + start: 0, + end: 22, + loc: { + start: { line: 1, column: 0, index: 0 }, + end: { line: 1, column: 22, index: 22 } + }, + declarations: [ + { + type: 'VariableDeclarator', + start: 4, + end: 22, + loc: { + start: { line: 1, column: 4, index: 4 }, + end: { line: 1, column: 22, index: 22 } + }, + id: { + type: 'Identifier', + start: 4, + end: 8, + loc: { + start: { line: 1, column: 4, index: 4 }, + end: { line: 1, column: 8, index: 8 } + }, + name: 'test' + }, + init: { + type: 'TSAsExpression', + start: 11, + end: 22, + loc: { + start: { line: 1, column: 11, index: 11 }, + end: { line: 1, column: 22, index: 22 } + }, + expression: { + type: 'Literal', + start: 11, + end: 12, + loc: { + start: { line: 1, column: 11, index: 11 }, + end: { line: 1, column: 12, index: 12 } + }, + value: 1, + raw: '1' + }, + typeAnnotation: { + type: 'TSNumberKeyword', + start: 16, + end: 22, + loc: { + start: { line: 1, column: 16, index: 16 }, + end: { line: 1, column: 22, index: 22 } + } + } + } + } + ], + kind: 'let' + } + ], + sourceType: 'module' + }, + OneCastNumber: { + type: 'Program', + start: 0, + end: 20, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 20, + index: 20 + } + }, + body: [ + { + type: 'VariableDeclaration', + start: 0, + end: 20, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 20, + index: 20 + } + }, + declarations: [ + { + type: 'VariableDeclarator', + start: 4, + end: 20, + loc: { + start: { + line: 1, + column: 4, + index: 4 + }, + end: { + line: 1, + column: 20, + index: 20 + } + }, + id: { + type: 'Identifier', + start: 4, + end: 8, + loc: { + start: { + line: 1, + column: 4, + index: 4 + }, + end: { + line: 1, + column: 8, + index: 8 + } + }, + name: 'test' + }, + init: { + type: 'TSTypeAssertion', + start: 11, + end: 20, + loc: { + start: { + line: 1, + column: 11, + index: 11 + }, + end: { + line: 1, + column: 20, + index: 20 + } + }, + typeAnnotation: { + type: 'TSNumberKeyword', + start: 12, + end: 18, + loc: { + start: { + line: 1, + column: 12, + index: 12 + }, + end: { + line: 1, + column: 18, + index: 18 + } + } + }, + expression: { + type: 'Literal', + start: 19, + end: 20, + loc: { + start: { + line: 1, + column: 19, + index: 19 + }, + end: { + line: 1, + column: 20, + index: 20 + } + }, + value: 1, + raw: '1' + } + } + } + ], + kind: 'let' + } + ], + sourceType: 'module' + }, + LabeledOneAsNumber: { + type: 'Program', + start: 0, + end: 21, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 21, + index: 21 + } + }, + body: [ + { + type: 'LabeledStatement', + start: 0, + end: 21, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 21, + index: 21 + } + }, + body: { + type: 'ExpressionStatement', + start: 3, + end: 21, + loc: { + start: { + line: 1, + column: 3, + index: 3 + }, + end: { + line: 1, + column: 21, + index: 21 + } + }, + expression: { + type: 'AssignmentExpression', + start: 3, + end: 21, + loc: { + start: { + line: 1, + column: 3, + index: 3 + }, + end: { + line: 1, + column: 21, + index: 21 + } + }, + operator: '=', + left: { + type: 'Identifier', + start: 3, + end: 7, + loc: { + start: { + line: 1, + column: 3, + index: 3 + }, + end: { + line: 1, + column: 7, + index: 7 + } + }, + name: 'test' + }, + right: { + type: 'TSAsExpression', + start: 10, + end: 21, + loc: { + start: { + line: 1, + column: 10, + index: 10 + }, + end: { + line: 1, + column: 21, + index: 21 + } + }, + expression: { + type: 'Literal', + start: 10, + end: 11, + loc: { + start: { + line: 1, + column: 10, + index: 10 + }, + end: { + line: 1, + column: 11, + index: 11 + } + }, + value: 1, + raw: '1' + }, + typeAnnotation: { + type: 'TSNumberKeyword', + start: 15, + end: 21, + loc: { + start: { + line: 1, + column: 15, + index: 15 + }, + end: { + line: 1, + column: 21, + index: 21 + } + } + } + } + } + }, + label: { + type: 'Identifier', + start: 0, + end: 1, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 1, + index: 1 + } + }, + name: '$' + } + } + ], + sourceType: 'module' + }, + LabeledOneCastNumber: { + type: 'Program', + start: 0, + end: 19, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 19, + index: 19 + } + }, + body: [ + { + type: 'LabeledStatement', + start: 0, + end: 19, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 19, + index: 19 + } + }, + body: { + type: 'ExpressionStatement', + start: 3, + end: 19, + loc: { + start: { + line: 1, + column: 3, + index: 3 + }, + end: { + line: 1, + column: 19, + index: 19 + } + }, + expression: { + type: 'AssignmentExpression', + start: 3, + end: 19, + loc: { + start: { + line: 1, + column: 3, + index: 3 + }, + end: { + line: 1, + column: 19, + index: 19 + } + }, + operator: '=', + left: { + type: 'Identifier', + start: 3, + end: 7, + loc: { + start: { + line: 1, + column: 3, + index: 3 + }, + end: { + line: 1, + column: 7, + index: 7 + } + }, + name: 'test' + }, + right: { + type: 'TSTypeAssertion', + start: 10, + end: 19, + loc: { + start: { + line: 1, + column: 10, + index: 10 + }, + end: { + line: 1, + column: 19, + index: 19 + } + }, + typeAnnotation: { + type: 'TSNumberKeyword', + start: 11, + end: 17, + loc: { + start: { + line: 1, + column: 11, + index: 11 + }, + end: { + line: 1, + column: 17, + index: 17 + } + } + }, + expression: { + type: 'Literal', + start: 18, + end: 19, + loc: { + start: { + line: 1, + column: 18, + index: 18 + }, + end: { + line: 1, + column: 19, + index: 19 + } + }, + value: 1, + raw: '1' + } + } + } + }, + label: { + type: 'Identifier', + start: 0, + end: 1, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 1, + index: 1 + } + }, + name: '$' + } + } + ], + sourceType: 'module' + }, + ExpressionOneAsNumber: { + type: 'Program', + start: 0, + end: 16, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 16, + index: 16 + } + }, + body: [ + { + type: 'ExpressionStatement', + start: 0, + end: 16, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 16, + index: 16 + } + }, + expression: { + type: 'CallExpression', + start: 0, + end: 16, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 16, + index: 16 + } + }, + callee: { + type: 'Identifier', + start: 0, + end: 3, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 3, + index: 3 + } + }, + name: 'foo' + }, + arguments: [ + { + type: 'TSAsExpression', + start: 4, + end: 15, + loc: { + start: { + line: 1, + column: 4, + index: 4 + }, + end: { + line: 1, + column: 15, + index: 15 + } + }, + expression: { + type: 'Literal', + start: 4, + end: 5, + loc: { + start: { + line: 1, + column: 4, + index: 4 + }, + end: { + line: 1, + column: 5, + index: 5 + } + }, + value: 1, + raw: '1' + }, + typeAnnotation: { + type: 'TSNumberKeyword', + start: 9, + end: 15, + loc: { + start: { + line: 1, + column: 9, + index: 9 + }, + end: { + line: 1, + column: 15, + index: 15 + } + } + } + } + ], + optional: false + } + } + ], + sourceType: 'module' + }, + ExpressionOneCastNumber: { + type: 'Program', + start: 0, + end: 14, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 14, + index: 14 + } + }, + body: [ + { + type: 'ExpressionStatement', + start: 0, + end: 14, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 14, + index: 14 + } + }, + expression: { + type: 'CallExpression', + start: 0, + end: 14, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 14, + index: 14 + } + }, + callee: { + type: 'Identifier', + start: 0, + end: 3, + loc: { + start: { + line: 1, + column: 0, + index: 0 + }, + end: { + line: 1, + column: 3, + index: 3 + } + }, + name: 'foo' + }, + arguments: [ + { + type: 'TSTypeAssertion', + start: 4, + end: 13, + loc: { + start: { + line: 1, + column: 4, + index: 4 + }, + end: { + line: 1, + column: 13, + index: 13 + } + }, + typeAnnotation: { + type: 'TSNumberKeyword', + start: 5, + end: 11, + loc: { + start: { + line: 1, + column: 5, + index: 5 + }, + end: { + line: 1, + column: 11, + index: 11 + } + } + }, + expression: { + type: 'Literal', + start: 12, + end: 13, + loc: { + start: { + line: 1, + column: 12, + index: 12 + }, + end: { + line: 1, + column: 13, + index: 13 + } + }, + value: 1, + raw: '1' + } + } + ], + optional: false + } + } + ], + sourceType: 'module' + } +}; + +export default TypeCastsSnapshot; diff --git a/__test__/__snapshot__/expression/variables.ts b/__test__/__snapshot__/expression/variables.ts index 0d1bc94..a874e4b 100644 --- a/__test__/__snapshot__/expression/variables.ts +++ b/__test__/__snapshot__/expression/variables.ts @@ -4812,78 +4812,6 @@ const VariablesTypeSnapshot = { ], sourceType: 'module' }, - OneAsNumber: { - type: 'Program', - start: 0, - end: 22, - loc: { - start: { line: 1, column: 0, index: 0 }, - end: { line: 1, column: 22, index: 22 } - }, - body: [ - { - type: 'VariableDeclaration', - start: 0, - end: 22, - loc: { - start: { line: 1, column: 0, index: 0 }, - end: { line: 1, column: 22, index: 22 } - }, - declarations: [ - { - type: 'VariableDeclarator', - start: 4, - end: 22, - loc: { - start: { line: 1, column: 4, index: 4 }, - end: { line: 1, column: 22, index: 22 } - }, - id: { - type: 'Identifier', - start: 4, - end: 8, - loc: { - start: { line: 1, column: 4, index: 4 }, - end: { line: 1, column: 8, index: 8 } - }, - name: 'test' - }, - init: { - type: 'TSAsExpression', - start: 11, - end: 22, - loc: { - start: { line: 1, column: 11, index: 11 }, - end: { line: 1, column: 22, index: 22 } - }, - expression: { - type: 'Literal', - start: 11, - end: 12, - loc: { - start: { line: 1, column: 11, index: 11 }, - end: { line: 1, column: 12, index: 12 } - }, - value: 1, - raw: '1' - }, - typeAnnotation: { - type: 'TSNumberKeyword', - start: 16, - end: 22, - loc: { - start: { line: 1, column: 16, index: 16 }, - end: { line: 1, column: 22, index: 22 } - } - } - } - } - ], - kind: 'let' - } - ], - sourceType: 'module' - }, ParseGenericsWithoutComma: { type: 'Program', start: 0, diff --git a/__test__/expression/casts.test.ts b/__test__/expression/casts.test.ts new file mode 100644 index 0000000..b316cba --- /dev/null +++ b/__test__/expression/casts.test.ts @@ -0,0 +1,47 @@ +import { describe, it } from 'vitest'; +import TypeCastsSnapshot from '../__snapshot__/expression/casts'; +import { equalNode, generateSource, parseSource } from '../utils'; + +describe('type casts', () => { + describe('in variable declarations', () => { + it('1 as number', () => { + const node = parseSource(generateSource([`let test = 1 as number`])); + + equalNode(node, TypeCastsSnapshot.OneAsNumber); + }); + + it('1', () => { + const node = parseSource(generateSource([`let test = 1`])); + + equalNode(node, TypeCastsSnapshot.OneCastNumber); + }); + }); + + describe('in labeled statements', () => { + it('1 as number', () => { + const node = parseSource(generateSource([`$: test = 1 as number`])); + + equalNode(node, TypeCastsSnapshot.LabeledOneAsNumber); + }); + + it('1', () => { + const node = parseSource(generateSource([`$: test = 1`])); + + equalNode(node, TypeCastsSnapshot.LabeledOneCastNumber); + }); + }); + + describe('in expressions', () => { + it('1 as number', () => { + const node = parseSource(generateSource([`foo(1 as number)`])); + + equalNode(node, TypeCastsSnapshot.ExpressionOneAsNumber); + }); + + it('1', () => { + const node = parseSource(generateSource([`foo(1)`])); + + equalNode(node, TypeCastsSnapshot.ExpressionOneCastNumber); + }); + }); +}); diff --git a/__test__/expression/variables.test.ts b/__test__/expression/variables.test.ts index 38dcdf4..6ec85ab 100644 --- a/__test__/expression/variables.test.ts +++ b/__test__/expression/variables.test.ts @@ -166,12 +166,6 @@ describe('variables declaration', () => { equalNode(node, VariablesTypeSnapshot.ExpressionEqualAsyncArrowFunction); }); - it('1 as number', () => { - const node = parseSource(generateSource([`let test = 1 as number`])); - - equalNode(node, VariablesTypeSnapshot.OneAsNumber); - }); - //console.log(JSON.stringify(node, null, 2)) it('parse generics without comma', () => { const node = parseSource(generateSource([`const a: Foo = 1`])); diff --git a/__test__/jsx/index.test.ts b/__test__/jsx/index.test.ts index a7e3f9a..522487c 100644 --- a/__test__/jsx/index.test.ts +++ b/__test__/jsx/index.test.ts @@ -1,5 +1,5 @@ import { describe, it } from 'vitest'; -import { equalNode, generateSource, parseSource } from '../utils'; +import { equalNode, generateSource, parseJsxSource } from '../utils'; import JSXSnapshot from '../__snapshot__/jsx'; const issue46File = ` @@ -23,31 +23,31 @@ const StyledDiv = styled.div\` describe('jsx', function () { it('simple', () => { - const node = parseSource(generateSource([`
`])); + const node = parseJsxSource(generateSource([`
`])); equalNode(node, JSXSnapshot.Simple); }); it('value with colon', () => { - const node = parseSource(generateSource([` {value} `])); + const node = parseJsxSource(generateSource([` {value} `])); equalNode(node, JSXSnapshot.ValueWithColon); }); it('props with expr', () => { - const node = parseSource(generateSource([``])); + const node = parseJsxSource(generateSource([``])); equalNode(node, JSXSnapshot.PropsWithExpr); }); it('jsx with type generics', () => { - const node = parseSource(generateSource([` />`])); + const node = parseJsxSource(generateSource([` />`])); equalNode(node, JSXSnapshot.JsxWithTypeGenerics); }); it('tsx', () => { - const node = parseSource( + const node = parseJsxSource( generateSource([ 'import * as React from "react";', "import UserInterface from '../UserInterface'", @@ -74,12 +74,11 @@ describe('jsx', function () { ]) ); - console.log(JSON.stringify(node, null, 2)); equalNode(node, JSXSnapshot.Tsx); }); it('issue 29 jsx', () => { - const node = parseSource( + const node = parseJsxSource( generateSource([ 'import React, { forwardRef } from "react";', 'import PropTypes from "prop-types";', @@ -115,13 +114,13 @@ describe('jsx', function () { }); it('issue 46', () => { - const node = parseSource(issue46File); + const node = parseJsxSource(issue46File); equalNode(node, JSXSnapshot.Issue46); }); it('issue 48', () => { - const node = parseSource(issue48File); + const node = parseJsxSource(issue48File); equalNode(node, JSXSnapshot.Issue48); }); diff --git a/__test__/utils.ts b/__test__/utils.ts index 5513228..9af7a34 100644 --- a/__test__/utils.ts +++ b/__test__/utils.ts @@ -1,4 +1,4 @@ -import { expect } from 'vitest'; +import { assert } from 'vitest'; import * as acorn from 'acorn'; import tsPlugin from '../src'; @@ -10,6 +10,12 @@ export const DtsParser = acorn.Parser.extend( }) ); +export const JsxParser = acorn.Parser.extend( + tsPlugin({ + jsx: true + }) +); + export const AllowSatisfiesParser = acorn.Parser.extend( tsPlugin({ allowSatisfies: true @@ -17,7 +23,11 @@ export const AllowSatisfiesParser = acorn.Parser.extend( ); export function equalNode(node, snapshot) { - expect(node).toEqual(snapshot); + assert.deepEqual( + JSON.parse(JSON.stringify(node)), + snapshot, + 'should be' + JSON.stringify(node, null, 2) + ); } export function parseDtsSource(input: string) { @@ -28,6 +38,14 @@ export function parseDtsSource(input: string) { }); } +export function parseJsxSource(input: string) { + return JsxParser.parse(input, { + sourceType: 'module', + ecmaVersion: 'latest', + locations: true + }); +} + export function parseSource(input: string) { return Parser.parse(input, { sourceType: 'module', diff --git a/index.d.ts b/index.d.ts index 3646e52..98e6a88 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,10 +3,13 @@ import { Parser } from 'acorn'; declare function tsPlugin(options?: { dts?: boolean; allowSatisfies?: boolean; - jsx?: { - allowNamespaces?: boolean; - allowNamespacedObjects?: boolean; - }; + /** Whether to use JSX. Defaults to false */ + jsx?: + | boolean + | { + allowNamespaces?: boolean; + allowNamespacedObjects?: boolean; + }; }): (BaseParser: typeof Parser) => typeof Parser; export { tsPlugin, tsPlugin as default }; diff --git a/index.js b/index.js index c197536..162ed07 100644 --- a/index.js +++ b/index.js @@ -1057,7 +1057,7 @@ function keywordTypeFromName(value) { } function tsPlugin(options) { const { dts = false, allowSatisfies = false } = options || {}; - const disallowAmbiguousJSXLike = false; + const disallowAmbiguousJSXLike = !!options?.jsx; return function(Parser) { const _acorn = Parser.acorn || acornNamespace; const acornTypeScript = generateAcornTypeScript(_acorn); @@ -1088,7 +1088,14 @@ function tsPlugin(options) { return -1; } Parser = generateParseDecorators(Parser, acornTypeScript, _acorn); - Parser = generateJsxParser(_acorn, acornTypeScript, Parser, options?.jsx); + if (options?.jsx) { + Parser = generateJsxParser( + _acorn, + acornTypeScript, + Parser, + typeof options.jsx === "boolean" ? {} : options.jsx + ); + } Parser = generateParseImportAssertions(Parser, acornTypeScript, _acorn); class TypeScriptParser extends Parser { constructor(options2, input, startPos) { @@ -1150,7 +1157,11 @@ function tsPlugin(options) { } if (code === 60 && this.exprAllowed && this.input.charCodeAt(this.pos + 1) !== 33) { ++this.pos; - return this.finishToken(tokTypes2.jsxTagStart); + if (options?.jsx) { + return this.finishToken(tokTypes2.jsxTagStart); + } else { + return this.finishToken(tt.relational, "<"); + } } } return super.readToken(code); @@ -2659,18 +2670,32 @@ function tsPlugin(options) { errorTemplate: TypeScriptError.InvalidModifierOnTypeParameter }); } - // tsParseTypeAssertion(): any { - // if (disallowAmbiguousJSXLike) { - // this.raise(this.start, TypeScriptError.ReservedTypeAssertion) - // } - // - // const node = this.startNode() - // const _const = this.tsTryNextParseConstantContext() - // node.typeAnnotation = _const || this.tsNextThenParseType() - // this.expect(tt.relational) - // node.expression = this.parseMaybeUnary() - // return this.finishNode(node, 'TSTypeAssertion') - // } + // Handle type assertions + parseMaybeUnary(refExpressionErrors, sawUnary, incDec, forInit) { + if (!options?.jsx && this.tsMatchLeftRelational()) { + return this.tsParseTypeAssertion(); + } else { + return super.parseMaybeUnary(refExpressionErrors, sawUnary, incDec, forInit); + } + } + tsParseTypeAssertion() { + if (disallowAmbiguousJSXLike) { + this.raise(this.start, TypeScriptError.ReservedTypeAssertion); + } + const result = this.tryParse(() => { + const node = this.startNode(); + const _const = this.tsTryNextParseConstantContext(); + node.typeAnnotation = _const || this.tsNextThenParseType(); + this.expect(tt.relational); + node.expression = this.parseMaybeUnary(); + return this.finishNode(node, "TSTypeAssertion"); + }); + if (result.error) { + return this.tsParseTypeParameters(); + } else { + return result.node; + } + } tsParseTypeArguments() { const node = this.startNode(); node.params = this.tsInType( @@ -3985,7 +4010,7 @@ function tsPlugin(options) { let state; let jsx; let typeCast; - if (this.matchJsx("jsxTagStart") || this.tsMatchLeftRelational()) { + if (options?.jsx && (this.matchJsx("jsxTagStart") || this.tsMatchLeftRelational())) { state = this.cloneCurLookaheadState(); jsx = this.tryParse( () => this.parseMaybeAssignOrigin(forInit, refExpressionErrors, afterLeftParse), diff --git a/src/index.ts b/src/index.ts index 9a9a7a7..1df34b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -146,14 +146,16 @@ function tsPlugin(options?: { // default false // disallowAmbiguousJSXLike?: boolean // default true - jsx?: { - allowNamespaces?: boolean; - allowNamespacedObjects?: boolean; - }; + jsx?: + | boolean + | { + allowNamespaces?: boolean; + allowNamespacedObjects?: boolean; + }; allowSatisfies?: boolean; }) { const { dts = false, allowSatisfies = false } = options || {}; - const disallowAmbiguousJSXLike = false; + const disallowAmbiguousJSXLike = !!options?.jsx; return function (Parser: typeof AcornParseClass) { const _acorn = Parser.acorn || acornNamespace; @@ -192,7 +194,14 @@ function tsPlugin(options?: { Parser = generateParseDecorators(Parser, acornTypeScript, _acorn); // extend jsx - Parser = generateJsxParser(_acorn, acornTypeScript, Parser, options?.jsx); + if (options?.jsx) { + Parser = generateJsxParser( + _acorn, + acornTypeScript, + Parser, + typeof options.jsx === 'boolean' ? {} : options.jsx + ); + } // extend import asset Parser = generateParseImportAssertions(Parser, acornTypeScript, _acorn); @@ -270,7 +279,11 @@ function tsPlugin(options?: { if (code === 60 && this.exprAllowed && this.input.charCodeAt(this.pos + 1) !== 33) { ++this.pos; - return this.finishToken(tokTypes.jsxTagStart); + if (options?.jsx) { + return this.finishToken(tokTypes.jsxTagStart); + } else { + return this.finishToken(tt.relational, '<'); + } } } return super.readToken(code); @@ -2190,18 +2203,40 @@ function tsPlugin(options?: { }); } - // tsParseTypeAssertion(): any { - // if (disallowAmbiguousJSXLike) { - // this.raise(this.start, TypeScriptError.ReservedTypeAssertion) - // } - // - // const node = this.startNode() - // const _const = this.tsTryNextParseConstantContext() - // node.typeAnnotation = _const || this.tsNextThenParseType() - // this.expect(tt.relational) - // node.expression = this.parseMaybeUnary() - // return this.finishNode(node, 'TSTypeAssertion') - // } + // Handle type assertions + parseMaybeUnary( + refExpressionErrors?: any, + sawUnary?: boolean, + incDec?: boolean, + forInit?: boolean + ) { + if (!options?.jsx && this.tsMatchLeftRelational()) { + return this.tsParseTypeAssertion(); + } else { + return super.parseMaybeUnary(refExpressionErrors, sawUnary, incDec, forInit); + } + } + + tsParseTypeAssertion(): any { + if (disallowAmbiguousJSXLike) { + this.raise(this.start, TypeScriptError.ReservedTypeAssertion); + } + + const result = this.tryParse(() => { + const node = this.startNode(); + const _const = this.tsTryNextParseConstantContext(); + node.typeAnnotation = _const || this.tsNextThenParseType(); + this.expect(tt.relational); + node.expression = this.parseMaybeUnary(); + return this.finishNode(node, 'TSTypeAssertion'); + }); + // could also be generics + if (result.error) { + return this.tsParseTypeParameters(); + } else { + return result.node; + } + } tsParseTypeArguments(): any { const node = this.startNode(); @@ -3941,7 +3976,7 @@ function tsPlugin(options?: { let jsx; let typeCast; - if (this.matchJsx('jsxTagStart') || this.tsMatchLeftRelational()) { + if (options?.jsx && (this.matchJsx('jsxTagStart') || this.tsMatchLeftRelational())) { // Prefer to parse JSX if possible. But may be an arrow fn. state = this.cloneCurLookaheadState();