From 5cddb4c7345eba7c3ac87288b5130ecd1a94bbf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 23 Sep 2022 16:41:56 +0900 Subject: [PATCH] feat(es/typescript): Support TS 4.9 (#5938) **Description:** This PR adds satisfaction expression to the AST and parser. --- .../typeSatisfaction.1.normal.js | 73 +- .../typeSatisfaction.2.minified.js | 48 - ...Satisfaction_contextualTyping1.1.normal.js | 14 +- ...tisfaction_contextualTyping1.2.minified.js | 6 - ...tisfaction_ensureInterfaceImpl.1.normal.js | 13 +- ...sfaction_ensureInterfaceImpl.2.minified.js | 6 - .../typeSatisfaction_js.1.normal.js | 7 +- .../typeSatisfaction_js.2.minified.js | 6 - ...tion_optionalMemberConformance.1.normal.js | 14 +- ...on_optionalMemberConformance.2.minified.js | 10 +- ...isfaction_propNameConstraining.1.normal.js | 16 +- ...faction_propNameConstraining.2.minified.js | 12 +- ...action_propertyNameFulfillment.1.normal.js | 16 +- ...tion_propertyNameFulfillment.2.minified.js | 12 +- ...tion_propertyValueConformance1.1.normal.js | 21 +- ...on_propertyValueConformance1.2.minified.js | 10 +- ...tion_propertyValueConformance2.1.normal.js | 21 +- ...on_propertyValueConformance2.2.minified.js | 10 +- ...tion_propertyValueConformance3.1.normal.js | 24 +- ...on_propertyValueConformance3.2.minified.js | 23 +- crates/swc_atoms/words.txt | 1542 +++++++++-------- crates/swc_ecma_ast/src/expr.rs | 8 +- crates/swc_ecma_ast/src/lib.rs | 11 +- crates/swc_ecma_ast/src/typescript.rs | 11 + crates/swc_ecma_codegen/src/lib.rs | 5 +- crates/swc_ecma_codegen/src/typescript.rs | 13 + crates/swc_ecma_codegen/src/util.rs | 3 +- crates/swc_ecma_minifier/src/util/size.rs | 1 + crates/swc_ecma_parser/src/macros.rs | 3 + crates/swc_ecma_parser/src/parser/expr.rs | 1 + crates/swc_ecma_parser/src/parser/expr/ops.rs | 17 + crates/swc_ecma_parser/src/parser/util.rs | 3 +- .../tests/tsc/typeSatisfaction.json | 755 +++++--- .../typeSatisfaction_contextualTyping1.json | 327 ++-- .../typeSatisfaction_ensureInterfaceImpl.json | 283 +-- .../tests/tsc/typeSatisfaction_js.json | 36 +- ...atisfaction_optionalMemberConformance.json | 105 +- ...typeSatisfaction_propNameConstraining.json | 224 ++- ...eSatisfaction_propertyNameFulfillment.json | 194 ++- ...atisfaction_propertyValueConformance1.json | 117 +- ...atisfaction_propertyValueConformance2.json | 117 +- ...atisfaction_propertyValueConformance3.json | 582 ++++--- crates/swc_ecma_quote_macros/src/ast/expr.rs | 1 + .../src/ast/typescript.rs | 1 + .../src/es2015/destructuring.rs | 7 +- .../src/strip.rs | 3 +- crates/swc_ecma_utils/src/lib.rs | 6 +- crates/swc_ecma_visit/src/lib.rs | 1 + crates/swc_estree_compat/src/babelify/expr.rs | 4 + postinstall.js | 228 ++- 50 files changed, 2962 insertions(+), 2009 deletions(-) diff --git a/crates/swc/tests/tsc-references/typeSatisfaction.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction.1.normal.js index b50c30f6b5c..6ccf99c0097 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction.1.normal.js @@ -1,49 +1,26 @@ //// [typeSatisfaction.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 11 | const t1 = { a: 1 } satisfies I1; // Ok -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 12 | const t2 = { a: 1, b: 1 } satisfies I1; // Error -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 13 | const t3 = { } satisfies I1; // Error -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 15 | const t4: T1 = { a: "a" } satisfies T1; // Ok -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 16 | const t5 = (m => m.substring(0)) satisfies T2; // Ok -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 18 | const t6 = [1, 2] satisfies [number, number]; -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 23 | let t7 = { a: 'test' } satisfies A; -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 24 | let t8 = { a: 'test', b: 'test' } satisfies A; -//! : ^^^^^^^^^ -//! `---- +var t1 = { + a: 1 +}; // Ok +var t2 = { + a: 1, + b: 1 +}; // Error +var t3 = {}; // Error +var t4 = { + a: "a" +}; // Ok +var t5 = function(m) { + return m.substring(0); +}; // Ok +var t6 = [ + 1, + 2 +]; +var t7 = { + a: "test" +}; +var t8 = { + a: "test", + b: "test" +}; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction.2.minified.js index b50c30f6b5c..b5194cb6997 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction.2.minified.js @@ -1,49 +1 @@ //// [typeSatisfaction.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 11 | const t1 = { a: 1 } satisfies I1; // Ok -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 12 | const t2 = { a: 1, b: 1 } satisfies I1; // Error -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 13 | const t3 = { } satisfies I1; // Error -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 15 | const t4: T1 = { a: "a" } satisfies T1; // Ok -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 16 | const t5 = (m => m.substring(0)) satisfies T2; // Ok -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 18 | const t6 = [1, 2] satisfies [number, number]; -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 23 | let t7 = { a: 'test' } satisfies A; -//! : ^^^^^^^^^ -//! `---- -//! -//! x Expected a semicolon -//! ,---- -//! 24 | let t8 = { a: 'test', b: 'test' } satisfies A; -//! : ^^^^^^^^^ -//! `---- diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_contextualTyping1.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction_contextualTyping1.1.normal.js index b5783193de2..84b8b9a68bb 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_contextualTyping1.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_contextualTyping1.1.normal.js @@ -1,7 +1,9 @@ //// [typeSatisfaction_contextualTyping1.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 6 | } satisfies Predicates; -//! : ^^^^^^^^^ -//! `---- +var p = { + isEven: function(n) { + return n % 2 === 0; + }, + isOdd: function(n) { + return n % 2 === 1; + } +}; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_contextualTyping1.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction_contextualTyping1.2.minified.js index b5783193de2..dbafe0dff51 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_contextualTyping1.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_contextualTyping1.2.minified.js @@ -1,7 +1 @@ //// [typeSatisfaction_contextualTyping1.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 6 | } satisfies Predicates; -//! : ^^^^^^^^^ -//! `---- diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_ensureInterfaceImpl.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction_ensureInterfaceImpl.1.normal.js index cf339b40250..fdb6d3a316f 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_ensureInterfaceImpl.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_ensureInterfaceImpl.1.normal.js @@ -1,7 +1,8 @@ //// [typeSatisfaction_ensureInterfaceImpl.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 11 | } satisfies Movable & Record; -//! : ^^^^^^^^^ -//! `---- +var car = { + start: function start() {}, + move: function move(d) { + // d should be number + }, + stop: function stop() {} +}; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_ensureInterfaceImpl.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction_ensureInterfaceImpl.2.minified.js index cf339b40250..d7159508987 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_ensureInterfaceImpl.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_ensureInterfaceImpl.2.minified.js @@ -1,7 +1 @@ //// [typeSatisfaction_ensureInterfaceImpl.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 11 | } satisfies Movable & Record; -//! : ^^^^^^^^^ -//! `---- diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_js.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction_js.1.normal.js index abf0b521c70..05796f72b54 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_js.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_js.1.normal.js @@ -1,7 +1,2 @@ //// [/src/a.js] -//! -//! x Expected a semicolon -//! ,---- -//! 2 | var v = undefined satisfies 1; -//! : ^^^^^^^^^ -//! `---- +var v = undefined; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_js.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction_js.2.minified.js index abf0b521c70..5f433f72afe 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_js.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_js.2.minified.js @@ -1,7 +1 @@ //// [/src/a.js] -//! -//! x Expected a semicolon -//! ,---- -//! 2 | var v = undefined satisfies 1; -//! : ^^^^^^^^^ -//! `---- diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_optionalMemberConformance.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction_optionalMemberConformance.1.normal.js index a958f9f809d..472a5190738 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_optionalMemberConformance.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_optionalMemberConformance.1.normal.js @@ -1,7 +1,9 @@ //// [typeSatisfaction_optionalMemberConformance.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 3 | const a = { x: 10 } satisfies Partial; -//! : ^^^^^^^^^ -//! `---- +// Undesirable behavior today with type annotation +var a = { + x: 10 +}; +// Should OK +console.log(a.x.toFixed()); +// Should error +var p = a.y; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_optionalMemberConformance.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction_optionalMemberConformance.2.minified.js index a958f9f809d..fc2ad692ed6 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_optionalMemberConformance.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_optionalMemberConformance.2.minified.js @@ -1,7 +1,5 @@ //// [typeSatisfaction_optionalMemberConformance.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 3 | const a = { x: 10 } satisfies Partial; -//! : ^^^^^^^^^ -//! `---- +var a = { + x: 10 +}; +console.log(a.x.toFixed()), a.y; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propNameConstraining.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction_propNameConstraining.1.normal.js index 3e1a0968344..2df22f0a1a9 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propNameConstraining.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propNameConstraining.1.normal.js @@ -1,7 +1,11 @@ //// [typeSatisfaction_propNameConstraining.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 7 | } satisfies Partial>; -//! : ^^^^^^^^^ -//! `---- +var p = { + a: 0, + b: "hello", + x: 8 // Should error, 'x' isn't in 'Keys' +}; +// Should be OK -- retain info that a is number and b is string +var a = p.a.toFixed(); +var b = p.b.substring(1); +// Should error even though 'd' is in 'Keys' +var d = p.d; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propNameConstraining.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction_propNameConstraining.2.minified.js index 3e1a0968344..07ef891322d 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propNameConstraining.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propNameConstraining.2.minified.js @@ -1,7 +1,7 @@ //// [typeSatisfaction_propNameConstraining.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 7 | } satisfies Partial>; -//! : ^^^^^^^^^ -//! `---- +var p = { + a: 0, + b: "hello", + x: 8 +}; +p.a.toFixed(), p.b.substring(1), p.d; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propertyNameFulfillment.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction_propertyNameFulfillment.1.normal.js index 5e4db7eab74..ce7f2fce2f3 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propertyNameFulfillment.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propertyNameFulfillment.1.normal.js @@ -1,7 +1,11 @@ //// [typeSatisfaction_propertyNameFulfillment.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 7 | } satisfies Record; -//! : ^^^^^^^^^ -//! `---- +var p = { + a: 0, + b: "hello", + x: 8 // Should error, 'x' isn't in 'Keys' +}; +// Should be OK -- retain info that a is number and b is string +var a = p.a.toFixed(); +var b = p.b.substring(1); +// Should error even though 'd' is in 'Keys' +var d = p.d; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propertyNameFulfillment.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction_propertyNameFulfillment.2.minified.js index 5e4db7eab74..ef7db6eccf4 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propertyNameFulfillment.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propertyNameFulfillment.2.minified.js @@ -1,7 +1,7 @@ //// [typeSatisfaction_propertyNameFulfillment.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 7 | } satisfies Record; -//! : ^^^^^^^^^ -//! `---- +var p = { + a: 0, + b: "hello", + x: 8 +}; +p.a.toFixed(), p.b.substring(1), p.d; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance1.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance1.1.normal.js index c41e7ec9b81..dbd0c9f649b 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance1.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance1.1.normal.js @@ -1,7 +1,16 @@ //// [typeSatisfaction_propertyValueConformance1.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 24 | } satisfies Facts; -//! : ^^^^^^^^^ -//! `---- +var x = { + m: true +}; +// Should be OK +checkTruths(x); +// Should be OK +checkM(x); +// Should fail under --noPropertyAccessFromIndexSignature +console.log(x.z); +var m = x.m; +// Should be able to detect a failure here +var x2 = { + m: true, + s: "false" +}; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance1.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance1.2.minified.js index c41e7ec9b81..bed96d2f357 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance1.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance1.2.minified.js @@ -1,7 +1,5 @@ //// [typeSatisfaction_propertyValueConformance1.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 24 | } satisfies Facts; -//! : ^^^^^^^^^ -//! `---- +var x = { + m: !0 +}; +checkTruths(x), checkM(x), console.log(x.z), x.m; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance2.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance2.1.normal.js index 3d8b5c970a1..11e90e6aae8 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance2.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance2.1.normal.js @@ -1,7 +1,16 @@ //// [typeSatisfaction_propertyValueConformance2.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 24 | } satisfies Facts; -//! : ^^^^^^^^^ -//! `---- +var x = { + m: true +}; +// Should be OK +checkTruths(x); +// Should be OK +checkM(x); +console.log(x.z); +// Should be OK under --noUncheckedIndexedAccess +var m = x.m; +// Should be able to detect a failure here +var x2 = { + m: true, + s: "false" +}; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance2.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance2.2.minified.js index 3d8b5c970a1..4430b400290 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance2.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance2.2.minified.js @@ -1,7 +1,5 @@ //// [typeSatisfaction_propertyValueConformance2.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 24 | } satisfies Facts; -//! : ^^^^^^^^^ -//! `---- +var x = { + m: !0 +}; +checkTruths(x), checkM(x), console.log(x.z), x.m; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance3.1.normal.js b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance3.1.normal.js index c0297a6708b..c35f6d37beb 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance3.1.normal.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance3.1.normal.js @@ -1,7 +1,19 @@ //// [typeSatisfaction_propertyValueConformance3.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 8 | } satisfies Record; -//! : ^^^^^^^^^ -//! `---- +// All of these should be Colors, but I only use some of them here. +export var Palette = { + white: { + r: 255, + g: 255, + b: 255 + }, + black: { + r: 0, + g: 0, + d: 0 + }, + blue: { + r: 0, + g: 0, + b: 255 + } +}; diff --git a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance3.2.minified.js b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance3.2.minified.js index c0297a6708b..ce9348e28af 100644 --- a/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance3.2.minified.js +++ b/crates/swc/tests/tsc-references/typeSatisfaction_propertyValueConformance3.2.minified.js @@ -1,7 +1,18 @@ //// [typeSatisfaction_propertyValueConformance3.ts] -//! -//! x Expected a semicolon -//! ,---- -//! 8 | } satisfies Record; -//! : ^^^^^^^^^ -//! `---- +export var Palette = { + white: { + r: 255, + g: 255, + b: 255 + }, + black: { + r: 0, + g: 0, + d: 0 + }, + blue: { + r: 0, + g: 0, + b: 255 + } +}; diff --git a/crates/swc_atoms/words.txt b/crates/swc_atoms/words.txt index 774d9bbe198..75d550914a6 100644 --- a/crates/swc_atoms/words.txt +++ b/crates/swc_atoms/words.txt @@ -1,70 +1,25 @@ + * -a -abbr +-moz-any +-webkit-any +-webkit-mask-box-image-repeat +-webkit-mask-repeat AbortController AbortSignal -abstract -accept -acronym -actuate -address -after -altglyph -altGlyph -altglyphdef -altGlyphDef -altglyphitem -altGlyphItem AnalyserNode -and -animate -animatecolor -animateColor -animatemotion -animateMotion -animatetransform -animateTransform Animation -animation -animation-name AnimationEffectReadOnly AnimationEffectTiming AnimationEffectTimingReadOnly AnimationEvent AnimationPlaybackEvent AnimationTimeline -annotation-xml -any -applet -apple-touch-icon -apple-touch-icon-precomposed ApplicationCache ApplicationCacheErrorEvent -application/ecmascript -application/javascript -application/json -application/ld+json -application/xhtml+xml -application/x-javascript -apply -arcrole -area -arguments Array ArrayBuffer -article -as -aside -assert -asserts -async Atomics Attr -attributename -attributeName -attributetype -attributeType -audio Audio AudioBuffer AudioBufferSourceNode @@ -78,116 +33,21 @@ AudioScheduledSourceNode AudioWorkletGlobalScope AudioWorkletNode AudioWorkletProcessor -await -b -background-repeat BarProp -base BaseAudioContext -basefont -basefrequency -baseFrequency -baseprofile -baseProfile BatteryManager -bdi -bdo -before BeforeUnloadEvent -bgsound -big -bigint BigInt BigInt64Array BigUint64Array BiquadFilterNode -blink Blob BlobEvent -block -blocking -blockquote -body -bold -boolean Boolean -border-block-width -border-bottom-left-radius -border-bottom-right-radius -border-end-end-radius -border-end-start-radius -border-image-outset -border-image-repeat -border-image-slice -border-image-width -border-inline-width -border-spacing -border-start-end-radius -border-start-start-radius -border-style -border-top-left-radius -border-top-right-radius -border-width Bottom line -br -break BroadcastChannel BudgetService -button ByteLengthQueuingStrategy -Cache -CacheStorage -calcmode -calcMode -call -canvas -CanvasCaptureMediaStreamTrack -CanvasGradient -CanvasPattern -CanvasRenderingContext2D -caption -case -catch -center -ChannelMergerNode -ChannelSplitterNode -CharacterData -circle -cite -class -ClipboardEvent -clip-path -clippath -clipPath -clippathunits -clipPathUnits -CloseEvent -cm -code -col -colgroup -color -color-profile -command -Comment -CompositionEvent -concat -const -ConstantSourceNode -constructor -content -contenteditable -content-security-policy -continue -ConvolverNode -counter-style -CountQueuingStrategy -createClass -createReactClass -Credential -CredentialsContainer -Crypto -CryptoKey CSS CSSConditionRule CSSFontFaceRule @@ -204,47 +64,28 @@ CSSStyleDeclaration CSSStyleRule CSSStyleSheet CSSSupportsRule -cubic-bezier -cursor +Cache +CacheStorage +CanvasCaptureMediaStreamTrack +CanvasGradient +CanvasPattern +CanvasRenderingContext2D +ChannelMergerNode +ChannelSplitterNode +CharacterData +ClipboardEvent +CloseEvent +Comment +CompositionEvent +ConstantSourceNode +ConvolverNode +CountQueuingStrategy +Credential +CredentialsContainer +Crypto +CryptoKey CustomElementRegistry CustomEvent -data -datalist -DataTransfer -DataTransferItem -DataTransferItemList -DataView -Date -dd -debugger -declare -default -_defineProperty -definitionurl -definitionURL -defs -deg -del -DelayNode -delete -desc -details -DeviceMotionEvent -DeviceOrientationEvent -dfn -dialog -diffuseconstant -diffuseConstant -dir -discard -display -displayName -div -dl -do -Document -DocumentFragment -DocumentType DOMError DOMException DOMImplementation @@ -259,180 +100,60 @@ DOMRectReadOnly DOMStringList DOMStringMap DOMTokenList +DataTransfer +DataTransferItem +DataTransferItemList +DataView +Date +DelayNode +DeviceMotionEvent +DeviceOrientationEvent +Document +DocumentFragment +DocumentType DragEvent -dt DynamicsCompressorNode -edgemode -edgeMode -element Element -ellipse -else -em -embed -encoding -end -enum -env Error ErrorEvent -eval EvalError -even Event EventSource EventTarget Exclude -export -_extends -extends Extract -face -false -feblend -feBlend -fecolormatrix -feColorMatrix -fecomponenttransfer -feComponentTransfer -fecomposite -feComposite -feconvolvematrix -feConvolveMatrix -fediffuselighting -feDiffuseLighting -fedisplacementmap -feDisplacementMap -fedistantlight -feDistantLight -fedropshadow -feDropShadow -feflood -feFlood -fefunca -feFuncA -fefuncb -feFuncB -fefuncg -feFuncG -fefuncr -feFuncR -fegaussianblur -feGaussianBlur -feimage -feImage -femerge -feMerge -femergenode -feMergeNode -femorphology -feMorphology -feoffset -feOffset -fepointlight -fePointLight -fespecularlighting -feSpecularLighting -fespotlight -feSpotLight -fetile -feTile -feturbulence -feTurbulence -fieldset -figcaption -figure -file File FileList FileReader -fill-opacity -filter -filterunits -filterUnits -finally -first-child -first-letter -first-line -first-of-type -flex Float32Array Float64Array -flow -flow-root FocusEvent -font -font-face FontFace -font-face-format -font-face-name FontFaceSetLoadEvent -font-face-src -font-face-uri -font-palette-values -font-weight -footer -for -foreignobject -foreignObject -form FormData -frame -frameset -from -function Function -g GainNode Gamepad GamepadButton GamepadEvent -get -global -glyph -glyphref -glyphRef -grad -gradienttransform -gradientTransform -gradientunits -gradientUnits -grid -h1 -h2 -h3 -h4 -h5 -h6 -HashChangeEvent -head -header -headers -Headers -hgroup -History -hkern -hr -href -html HTMLAllCollection HTMLAnchorElement HTMLAreaElement HTMLAudioElement +HTMLBRElement HTMLBaseElement HTMLBodyElement -HTMLBRElement HTMLButtonElement HTMLCanvasElement HTMLCollection HTMLContentElement +HTMLDListElement HTMLDataElement HTMLDataListElement HTMLDetailsElement HTMLDialogElement HTMLDirectoryElement HTMLDivElement -HTMLDListElement HTMLDocument HTMLElement HTMLEmbedElement @@ -442,16 +163,16 @@ HTMLFormControlsCollection HTMLFormElement HTMLFrameElement HTMLFrameSetElement +HTMLHRElement HTMLHeadElement HTMLHeadingElement -HTMLHRElement HTMLHtmlElement HTMLIFrameElement HTMLImageElement HTMLInputElement +HTMLLIElement HTMLLabelElement HTMLLegendElement -HTMLLIElement HTMLLinkElement HTMLMapElement HTMLMarqueeElement @@ -460,8 +181,8 @@ HTMLMenuElement HTMLMetaElement HTMLMeterElement HTMLModElement -HTMLObjectElement HTMLOListElement +HTMLObjectElement HTMLOptGroupElement HTMLOptionElement HTMLOptionsCollection @@ -493,11 +214,9 @@ HTMLTrackElement HTMLUListElement HTMLUnknownElement HTMLVideoElement -http-equiv -hz -i -icon -id +HashChangeEvent +Headers +History IDBCursor IDBCursorWithValue IDBDatabase @@ -509,121 +228,36 @@ IDBOpenDBRequest IDBRequest IDBTransaction IDBVersionChangeEvent -IdleDeadline -if -iframe IIRFilterNode -image +IdleDeadline Image ImageBitmap ImageBitmapRenderingContext ImageCapture ImageData -imagesizes -imagesrcset -img -implements -import -important -importmap -in -infer Infinity -inherit -initial -inline -input InputEvent -ins -inset -inset-block -inset-inline -instanceof Int16Array Int32Array Int8Array -interface IntersectionObserver IntersectionObserverEntry Intl -intrinsic -is -isindex -itemprop -itemref -itemtype -iterator JSON -jump-end -jump-start -kbd -kernelmatrix -kernelMatrix -kernelunitlength -kernelUnitLength -key KeyboardEvent KeyframeEffect KeyframeEffectReadOnly -keygen -keyof -keypoints -keyPoints -keysplines -keySplines -keytimes -keyTimes -keywords -khz -label -lang -language -last-of-type -layer -legend -length -lengthadjust -lengthAdjust -let -li -limitingconeangle -limitingConeAngle -line -lineargradient -linearGradient -link -listing -list-item Location -main -malignmark -map +MIDIAccess +MIDIConnectionEvent +MIDIInput +MIDIInputMap +MIDIMessageEvent +MIDIOutput +MIDIOutputMap +MIDIPort Map -margin -margin-block -margin-inline -mark -marker -markerheight -markerHeight -markerunits -markerUnits -markerwidth -markerWidth -marquee -mask -mask-border-outset -mask-border-repeat -maskcontentunits -maskContentUnits -mask-repeat -maskunits -maskUnits -matches -math Math -matrix3d -media MediaDeviceInfo MediaDevices MediaElementAudioSourceNode @@ -645,117 +279,42 @@ MediaStreamAudioSourceNode MediaStreamEvent MediaStreamTrack MediaStreamTrackEvent -menu MessageChannel MessageEvent MessagePort -meta -metadata -meter -mglyph -mi -MIDIAccess -MIDIConnectionEvent -MIDIInput -MIDIInputMap -MIDIMessageEvent -MIDIOutput -MIDIOutputMap -MIDIPort MimeType MimeTypeArray -missing-glyph -mm -mn -mo -module MouseEvent --moz-any -mpath -ms -mtext MutationEvent MutationObserver MutationRecord -name -NamedNodeMap -namespace +NODE_ENV NaN -nav +NamedNodeMap NavigationPreloadManager Navigator NetworkInformation -never -new -nobr Node -NODE_ENV NodeFilter NodeIterator NodeList -noembed -noframes -none NonNullable -no-repeat -normal -noscript -not Notification -nth-child -nth-last-child -nth-last-of-type -nth-of-type -null -number Number -numoctaves -numOctaves -object Object -of OfflineAudioCompletionEvent OfflineAudioContext OffscreenCanvas -ol -only -opacity -optgroup -option Option -or OscillatorNode -out -output -overflow -override -overscroll-behavior -p -package -padding -padding-block -padding-inline PageTransitionEvent PannerNode -param -part Partial -path Path2D -pathlength -pathLength -pattern -patterncontentunits -patternContentUnits -patterntransform -patternTransform -patternunits -patternUnits PaymentAddress PaymentRequest PaymentRequestUpdateEvent PaymentResponse -pc Performance PerformanceEntry PerformanceLongTaskTiming @@ -769,31 +328,14 @@ PerformancePaintTiming PerformanceResourceTiming PerformanceTiming PeriodicWave -Permissions PermissionStatus +Permissions PhotoCapabilities Pick -picture -place-content -placeholder -place-items -place-self -plaintext Plugin PluginArray PointerEvent -points -pointsatx -pointsAtX -pointsaty -pointsAtY -pointsatz -pointsAtZ -polygon -polyline PopStateEvent -pre -preload Presentation PresentationAvailability PresentationConnection @@ -802,81 +344,14 @@ PresentationConnectionCloseEvent PresentationConnectionList PresentationReceiver PresentationRequest -preservealpha -preserveAlpha -preserveaspectratio -preserveAspectRatio -primitiveunits -primitiveUnits -private -process ProcessingInstruction -progress ProgressEvent Promise PromiseRejectionEvent -protected Proxy -pt -public PushManager PushSubscription PushSubscriptionOptions -px -q -rad -radialgradient -radialGradient -RadioNodeList -Range -RangeError -rb -rbc -React -ReadableStream -readonly -Readonly -ReadonlyArray -Record -rect -ReferenceError -Reflect -refx -refX -refy -refY -RegExp -rel -RemotePlayback -repeat -repeatcount -repeatCount -repeatdur -repeatDur -Request -require -Required -requiredextensions -requiredExtensions -requiredfeatures -requiredFeatures -ResizeObserver -ResizeObserverEntry -Response -return -ReturnType -revert -revert-layer -role -rotate -rotate3d -rotatex -rotatey -rotatez -round -rp -rt -rtc RTCCertificate RTCDataChannel RTCDataChannelEvent @@ -893,98 +368,29 @@ RTCSctpTransport RTCSessionDescription RTCStatsReport RTCTrackEvent -ruby -run-in -s -samp -sandbox -scale -scale3d -Screen -ScreenOrientation -script -ScriptProcessorNode -scroll-margin -scroll-margin-block -scroll-margin-inline -scroll-padding -scroll-padding-block -scroll-padding-inline -scroll-snap-align -section -SecurityPolicyViolationEvent -select -Selection -ServiceWorker -ServiceWorkerContainer -ServiceWorkerRegistration -set -Set -shadow -ShadowRoot -shape-image-threshold -SharedArrayBuffer -SharedWorker -show -size -sizes -skew -skewx -skewy -slot -small -solidColor -source -SourceBuffer -SourceBufferList -space -spacer -span -specularconstant -specularConstant -specularexponent -specularExponent -speculationrules -SpeechSynthesisEvent -SpeechSynthesisUtterance -spreadmethod -spreadMethod -src -srcdoc -start -startoffset -startOffset -static -StaticRange -stddeviation -stdDeviation -steps -StereoPannerNode -stitchtiles -stitchTiles -stop -Storage -StorageEvent -StorageManager -strike -string -String -stroke-dasharray -stroke-opacity -strong -style -StyleSheet -StyleSheetList -sub -SubtleCrypto -summary -sup -super -surfacescale -surfaceScale -svg +RadioNodeList +Range +RangeError +React +ReadableStream +Readonly +ReadonlyArray +Record +ReferenceError +Reflect +RegExp +RemotePlayback +Request +Required +ResizeObserver +ResizeObserverEntry +Response +ReturnType SVGAElement SVGAngle +SVGAnimateElement +SVGAnimateMotionElement +SVGAnimateTransformElement SVGAnimatedAngle SVGAnimatedBoolean SVGAnimatedEnumeration @@ -997,9 +403,6 @@ SVGAnimatedPreserveAspectRatio SVGAnimatedRect SVGAnimatedString SVGAnimatedTransformList -SVGAnimateElement -SVGAnimateMotionElement -SVGAnimateTransformElement SVGAnimationElement SVGCircleElement SVGClipPathElement @@ -1043,13 +446,13 @@ SVGGraphicsElement SVGImageElement SVGLength SVGLengthList -SVGLinearGradientElement SVGLineElement +SVGLinearGradientElement +SVGMPathElement SVGMarkerElement SVGMaskElement SVGMatrix SVGMetadataElement -SVGMPathElement SVGNumber SVGNumberList SVGPathElement @@ -1062,14 +465,15 @@ SVGPreserveAspectRatio SVGRadialGradientElement SVGRect SVGRectElement +SVGSVGElement SVGScriptElement SVGSetElement SVGStopElement SVGStringList SVGStyleElement -SVGSVGElement SVGSwitchElement SVGSymbolElement +SVGTSpanElement SVGTextContentElement SVGTextElement SVGTextPathElement @@ -1077,110 +481,66 @@ SVGTextPositioningElement SVGTitleElement SVGTransform SVGTransformList -SVGTSpanElement SVGUnitTypes SVGUseElement SVGViewElement -switch -symbol +Screen +ScreenOrientation +ScriptProcessorNode +SecurityPolicyViolationEvent +Selection +ServiceWorker +ServiceWorkerContainer +ServiceWorkerRegistration +Set +ShadowRoot +SharedArrayBuffer +SharedWorker +SourceBuffer +SourceBufferList +SpeechSynthesisEvent +SpeechSynthesisUtterance +StaticRange +StereoPannerNode +Storage +StorageEvent +StorageManager +String +StyleSheet +StyleSheetList +SubtleCrypto Symbol SyntaxError -systemlanguage -systemLanguage -table -tablevalues -tableValues -target -targetx -targetX -targety -targetY TaskAttributionTiming -tbody -td -template -text Text -textarea -text/css TextDecoder -text/ecmascript TextEncoder TextEvent -text/html -text/javascript -text/jscript -textlength -textLength TextMetrics -textpath -textPath TextTrack TextTrackCue TextTrackCueList TextTrackList -tfoot -th -thead -this -throw -time TimeRanges -title -_toConsumableArray -toString Touch TouchEvent TouchList -tr -track TrackEvent -transform TransitionEvent -translate -translate3d -transparent TreeWalker -tref -true -try -tspan -tt -turn -type TypeError -typeof -u UIEvent +URIError +URL +URLSearchParams Uint16Array Uint32Array Uint8Array Uint8ClampedArray -ul -undefined -unique -unknown -unset -URIError -url -URL -URLSearchParams -use -ValidityState -var -video -view -viewbox -viewBox -viewport -viewtarget -viewTarget -VisualViewport -vkern -void VTTCue +ValidityState +VisualViewport WaveShaperNode -wbr WeakMap WeakSet WebAssembly @@ -1201,19 +561,670 @@ WebGLTexture WebGLTransformFeedback WebGLUniformLocation WebGLVertexArrayObject --webkit-any --webkit-mask-box-image-repeat --webkit-mask-repeat WebSocket WheelEvent -where -while Window -with Worker WritableStream -xchannelselector +XMLDocument +XMLHttpRequest +XMLHttpRequestEventTarget +XMLHttpRequestUpload +XMLSerializer +XPathEvaluator +XPathExpression +XPathResult +XSLTProcessor +_defineProperty +_extends +_toConsumableArray +a +abbr +abstract +accept +acronym +actuate +address +after +altGlyph +altGlyphDef +altGlyphItem +altglyph +altglyphdef +altglyphitem +and +animate +animateColor +animateMotion +animateTransform +animatecolor +animatemotion +animatetransform +animation +animation-name +annotation-xml +any +apple-touch-icon +apple-touch-icon-precomposed +applet +application/ecmascript +application/javascript +application/json +application/ld+json +application/x-javascript +application/xhtml+xml +apply +arcrole +area +arguments +article +as +aside +assert +asserts +async +attributeName +attributeType +attributename +attributetype +audio +await +b +background-repeat +base +baseFrequency +baseProfile +basefont +basefrequency +baseprofile +bdi +bdo +before +bgsound +big +bigint +blink +block +blocking +blockquote +body +bold +boolean +border-block-width +border-bottom-left-radius +border-bottom-right-radius +border-end-end-radius +border-end-start-radius +border-image-outset +border-image-repeat +border-image-slice +border-image-width +border-inline-width +border-spacing +border-start-end-radius +border-start-start-radius +border-style +border-top-left-radius +border-top-right-radius +border-width +br +break +button +calcMode +calcmode +call +canvas +caption +case +catch +center +circle +cite +class +clip-path +clipPath +clipPathUnits +clippath +clippathunits +cm +code +col +colgroup +color +color-profile +command +concat +const +constructor +content +content-security-policy +contenteditable +continue +counter-style +createClass +createReactClass +cubic-bezier +cursor +data +datalist +dd +debugger +declare +default +definitionURL +definitionurl +defs +deg +del +delete +desc +details +dfn +dialog +diffuseConstant +diffuseconstant +dir +discard +display +displayName +div +dl +do +dt +edgeMode +edgemode +element +ellipse +else +em +embed +encoding +end +enum +env +eval +even +export +extends +face +false +feBlend +feColorMatrix +feComponentTransfer +feComposite +feConvolveMatrix +feDiffuseLighting +feDisplacementMap +feDistantLight +feDropShadow +feFlood +feFuncA +feFuncB +feFuncG +feFuncR +feGaussianBlur +feImage +feMerge +feMergeNode +feMorphology +feOffset +fePointLight +feSpecularLighting +feSpotLight +feTile +feTurbulence +feblend +fecolormatrix +fecomponenttransfer +fecomposite +feconvolvematrix +fediffuselighting +fedisplacementmap +fedistantlight +fedropshadow +feflood +fefunca +fefuncb +fefuncg +fefuncr +fegaussianblur +feimage +femerge +femergenode +femorphology +feoffset +fepointlight +fespecularlighting +fespotlight +fetile +feturbulence +fieldset +figcaption +figure +file +fill-opacity +filter +filterUnits +filterunits +finally +first-child +first-letter +first-line +first-of-type +flex +flow +flow-root +font +font-face +font-face-format +font-face-name +font-face-src +font-face-uri +font-palette-values +font-weight +footer +for +foreignObject +foreignobject +form +frame +frameset +from +function +g +get +global +glyph +glyphRef +glyphref +grad +gradientTransform +gradientUnits +gradienttransform +gradientunits +grid +h1 +h2 +h3 +h4 +h5 +h6 +head +header +headers +hgroup +hkern +hr +href +html +http-equiv +hz +i +icon +id +if +iframe +image +imagesizes +imagesrcset +img +implements +import +important +importmap +in +infer +inherit +initial +inline +input +ins +inset +inset-block +inset-inline +instanceof +interface +intrinsic +is +isindex +itemprop +itemref +itemtype +iterator +jump-end +jump-start +kbd +kernelMatrix +kernelUnitLength +kernelmatrix +kernelunitlength +key +keyPoints +keySplines +keyTimes +keygen +keyof +keypoints +keysplines +keytimes +keywords +khz +label +lang +language +last-of-type +layer +legend +length +lengthAdjust +lengthadjust +let +li +limitingConeAngle +limitingconeangle +line +linearGradient +lineargradient +link +list-item +listing +main +malignmark +map +margin +margin-block +margin-inline +mark +marker +markerHeight +markerUnits +markerWidth +markerheight +markerunits +markerwidth +marquee +mask +mask-border-outset +mask-border-repeat +mask-repeat +maskContentUnits +maskUnits +maskcontentunits +maskunits +matches +math +matrix3d +media +menu +meta +metadata +meter +mglyph +mi +missing-glyph +mm +mn +mo +module +mpath +ms +mtext +name +namespace +nav +never +new +no-repeat +nobr +noembed +noframes +none +normal +noscript +not +nth-child +nth-last-child +nth-last-of-type +nth-of-type +null +numOctaves +number +numoctaves +object +of +ol +only +opacity +optgroup +option +or +out +output +overflow +override +overscroll-behavior +p +package +padding +padding-block +padding-inline +param +part +path +pathLength +pathlength +pattern +patternContentUnits +patternTransform +patternUnits +patterncontentunits +patterntransform +patternunits +pc +picture +place-content +place-items +place-self +placeholder +plaintext +points +pointsAtX +pointsAtY +pointsAtZ +pointsatx +pointsaty +pointsatz +polygon +polyline +pre +preload +preserveAlpha +preserveAspectRatio +preservealpha +preserveaspectratio +primitiveUnits +primitiveunits +private +process +progress +protected +pt +public +px +q +rad +radialGradient +radialgradient +rb +rbc +readonly +rect +refX +refY +refx +refy +rel +repeat +repeatCount +repeatDur +repeatcount +repeatdur +require +requiredExtensions +requiredFeatures +requiredextensions +requiredfeatures +return +revert +revert-layer +role +rotate +rotate3d +rotatex +rotatey +rotatez +round +rp +rt +rtc +ruby +run-in +s +samp +sandbox +satisfies +scale +scale3d +script +scroll-margin +scroll-margin-block +scroll-margin-inline +scroll-padding +scroll-padding-block +scroll-padding-inline +scroll-snap-align +section +select +set +shadow +shape-image-threshold +show +size +sizes +skew +skewx +skewy +slot +small +solidColor +source +space +spacer +span +specularConstant +specularExponent +specularconstant +specularexponent +speculationrules +spreadMethod +spreadmethod +src +srcdoc +start +startOffset +startoffset +static +stdDeviation +stddeviation +steps +stitchTiles +stitchtiles +stop +strike +string +stroke-dasharray +stroke-opacity +strong +style +sub +summary +sup +super +surfaceScale +surfacescale +svg +switch +symbol +systemLanguage +systemlanguage +table +tableValues +tablevalues +target +targetX +targetY +targetx +targety +tbody +td +template +text +text/css +text/ecmascript +text/html +text/javascript +text/jscript +textLength +textPath +textarea +textlength +textpath +tfoot +th +thead +this +throw +time +title +toString +tr +track +transform +translate +translate3d +transparent +tref +true +try +tspan +tt +turn +type +typeof +u +ul +undefined +unique +unknown +unset +url +use +var +video +view +viewBox +viewTarget +viewbox +viewport +viewtarget +vkern +void +wbr +where +while +with xChannelSelector +xchannelselector xlink xlink:actuate xlink:arcrole @@ -1223,22 +1234,13 @@ xlink:show xlink:title xlink:type xml -XMLDocument -XMLHttpRequest -XMLHttpRequestEventTarget -XMLHttpRequestUpload xml:lang +xml:space xmlns xmlns:xlink -XMLSerializer -xml:space xmp -XPathEvaluator -XPathExpression -XPathResult -XSLTProcessor -ychannelselector yChannelSelector +ychannelselector yield -zoomandpan zoomAndPan +zoomandpan diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index de5f02308a4..f18d46da3ee 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -22,8 +22,8 @@ use crate::{ prop::Prop, stmt::BlockStmt, typescript::{ - TsAsExpr, TsConstAssertion, TsInstantiation, TsNonNullExpr, TsTypeAnn, TsTypeAssertion, - TsTypeParamDecl, TsTypeParamInstantiation, + TsAsExpr, TsConstAssertion, TsInstantiation, TsNonNullExpr, TsSatisfactionExpr, TsTypeAnn, + TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation, }, ComputedPropName, Id, Invalid, }; @@ -157,6 +157,9 @@ pub enum Expr { #[tag("TsInstantiation")] TsInstantiation(TsInstantiation), + #[tag("TsSatisfactionExpr")] + TsSatisfaction(TsSatisfactionExpr), + #[tag("PrivateName")] PrivateName(PrivateName), @@ -257,6 +260,7 @@ impl Clone for Expr { PrivateName(e) => PrivateName(e.clone()), OptChain(e) => OptChain(e.clone()), Invalid(e) => Invalid(e.clone()), + TsSatisfaction(e) => TsSatisfaction(e.clone()), } } } diff --git a/crates/swc_ecma_ast/src/lib.rs b/crates/swc_ecma_ast/src/lib.rs index edd61c4affb..4ed108c1814 100644 --- a/crates/swc_ecma_ast/src/lib.rs +++ b/crates/swc_ecma_ast/src/lib.rs @@ -68,11 +68,12 @@ pub use self::{ TsKeywordType, TsKeywordTypeKind, TsLit, TsLitType, TsMappedType, TsMethodSignature, TsModuleBlock, TsModuleDecl, TsModuleName, TsModuleRef, TsNamespaceBody, TsNamespaceDecl, TsNamespaceExportDecl, TsNonNullExpr, TsOptionalType, TsParamProp, TsParamPropParam, - TsParenthesizedType, TsPropertySignature, TsQualifiedName, TsRestType, TsSetterSignature, - TsThisType, TsThisTypeOrIdent, TsTplLitType, TsTupleElement, TsTupleType, TsType, - TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion, TsTypeElement, TsTypeLit, TsTypeOperator, - TsTypeOperatorOp, TsTypeParam, TsTypeParamDecl, TsTypeParamInstantiation, TsTypePredicate, - TsTypeQuery, TsTypeQueryExpr, TsTypeRef, TsUnionOrIntersectionType, TsUnionType, + TsParenthesizedType, TsPropertySignature, TsQualifiedName, TsRestType, TsSatisfactionExpr, + TsSetterSignature, TsThisType, TsThisTypeOrIdent, TsTplLitType, TsTupleElement, + TsTupleType, TsType, TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion, TsTypeElement, TsTypeLit, + TsTypeOperator, TsTypeOperatorOp, TsTypeParam, TsTypeParamDecl, TsTypeParamInstantiation, + TsTypePredicate, TsTypeQuery, TsTypeQueryExpr, TsTypeRef, TsUnionOrIntersectionType, + TsUnionType, }, }; diff --git a/crates/swc_ecma_ast/src/typescript.rs b/crates/swc_ecma_ast/src/typescript.rs index 07b0fc76941..db9b892b20d 100644 --- a/crates/swc_ecma_ast/src/typescript.rs +++ b/crates/swc_ecma_ast/src/typescript.rs @@ -1055,6 +1055,17 @@ pub struct TsNonNullExpr { pub expr: Box, } +#[ast_node("TsSatisfactionExpr")] +#[derive(Eq, Hash, EqIgnoreSpan)] +#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] +pub struct TsSatisfactionExpr { + pub span: Span, + #[serde(rename = "expression")] + pub expr: Box, + #[serde(rename = "typeAnnotation")] + pub type_ann: Box, +} + #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Eq, Hash, EqIgnoreSpan)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] #[cfg_attr( diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index bdfefff979c..5a73ebf31fc 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -746,7 +746,7 @@ where #[emitter] fn emit_expr(&mut self, node: &Expr) -> Result { - match *node { + match node { Expr::Array(ref n) => emit!(n), Expr::Arrow(ref n) => emit!(n), Expr::Assign(ref n) => emit!(n), @@ -786,6 +786,9 @@ where Expr::TsInstantiation(ref n) => emit!(n), Expr::OptChain(ref n) => emit!(n), Expr::Invalid(ref n) => emit!(n), + Expr::TsSatisfaction(n) => { + emit!(n) + } } if self.comments.is_some() { diff --git a/crates/swc_ecma_codegen/src/typescript.rs b/crates/swc_ecma_codegen/src/typescript.rs index c56d68bedbf..2e4c7c2434a 100644 --- a/crates/swc_ecma_codegen/src/typescript.rs +++ b/crates/swc_ecma_codegen/src/typescript.rs @@ -39,6 +39,19 @@ where emit!(n.type_ann); } + #[emitter] + fn emit_ts_satisfaction_expr(&mut self, n: &TsSatisfactionExpr) -> Result { + self.emit_leading_comments_of_span(n.span(), false)?; + + emit!(n.expr); + + space!(); + keyword!("satisfies"); + space!(); + + emit!(n.type_ann); + } + #[emitter] fn emit_ts_call_signature_decl(&mut self, n: &TsCallSignatureDecl) -> Result { self.emit_leading_comments_of_span(n.span(), false)?; diff --git a/crates/swc_ecma_codegen/src/util.rs b/crates/swc_ecma_codegen/src/util.rs index 901881ccb6d..dfa3ab69380 100644 --- a/crates/swc_ecma_codegen/src/util.rs +++ b/crates/swc_ecma_codegen/src/util.rs @@ -141,7 +141,8 @@ impl StartsWithAlphaNum for Expr { Expr::TsNonNull(TsNonNullExpr { ref expr, .. }) | Expr::TsAs(TsAsExpr { ref expr, .. }) | Expr::TsConstAssertion(TsConstAssertion { ref expr, .. }) - | Expr::TsInstantiation(TsInstantiation { ref expr, .. }) => { + | Expr::TsInstantiation(TsInstantiation { ref expr, .. }) + | Expr::TsSatisfaction(TsSatisfactionExpr { ref expr, .. }) => { expr.starts_with_alpha_num() } diff --git a/crates/swc_ecma_minifier/src/util/size.rs b/crates/swc_ecma_minifier/src/util/size.rs index 35defc50990..959cb76bd64 100644 --- a/crates/swc_ecma_minifier/src/util/size.rs +++ b/crates/swc_ecma_minifier/src/util/size.rs @@ -197,6 +197,7 @@ impl SizeWithCtxt for Expr { Expr::TsNonNull(_) => TODO, Expr::TsAs(_) => TODO, Expr::TsInstantiation(_) => TODO, + Expr::TsSatisfaction(_) => TODO, } } } diff --git a/crates/swc_ecma_parser/src/macros.rs b/crates/swc_ecma_parser/src/macros.rs index 2a2bd7cea44..e971faf2733 100644 --- a/crates/swc_ecma_parser/src/macros.rs +++ b/crates/swc_ecma_parser/src/macros.rs @@ -332,6 +332,9 @@ macro_rules! tok { ("as") => { crate::token::Token::Word(crate::token::Word::Ident(swc_atoms::js_word!("as"))) }; + ("satisfies") => { + crate::token::Token::Word(crate::token::Word::Ident(swc_atoms::js_word!("satisfies"))) + }; ("namespace") => { crate::token::Token::Word(crate::token::Word::Ident(swc_atoms::js_word!("namespace"))) }; diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index 0efcdb9d69a..6dda59c4cef 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -422,6 +422,7 @@ impl Parser { type_ann, }))); } + // async a => body let arg = Pat::from(ident); let params = vec![arg]; diff --git a/crates/swc_ecma_parser/src/parser/expr/ops.rs b/crates/swc_ecma_parser/src/parser/expr/ops.rs index 36b8047fe53..018aa2e7d89 100644 --- a/crates/swc_ecma_parser/src/parser/expr/ops.rs +++ b/crates/swc_ecma_parser/src/parser/expr/ops.rs @@ -114,6 +114,23 @@ impl Parser { return self.parse_bin_op_recursively_inner(node, min_prec); } + if self.input.syntax().typescript() + && !self.input.had_line_break_before_cur() + && is!(self, "satisfies") + { + let start = left.span_lo(); + let expr = left; + let node = { + let type_ann = self.next_then_parse_ts_type()?; + Box::new(Expr::TsAs(TsAsExpr { + span: span!(self, start), + expr, + type_ann, + })) + }; + + return self.parse_bin_op_recursively_inner(node, min_prec); + } let ctx = self.ctx(); // Return left on eof diff --git a/crates/swc_ecma_parser/src/parser/util.rs b/crates/swc_ecma_parser/src/parser/util.rs index d13be77b8c0..5c28d20485f 100644 --- a/crates/swc_ecma_parser/src/parser/util.rs +++ b/crates/swc_ecma_parser/src/parser/util.rs @@ -314,7 +314,8 @@ pub(super) trait ExprExt { Expr::TsNonNull(TsNonNullExpr { ref expr, .. }) | Expr::TsTypeAssertion(TsTypeAssertion { ref expr, .. }) | Expr::TsAs(TsAsExpr { ref expr, .. }) - | Expr::TsInstantiation(TsInstantiation { ref expr, .. }) => { + | Expr::TsInstantiation(TsInstantiation { ref expr, .. }) + | Expr::TsSatisfaction(TsSatisfactionExpr { ref expr, .. }) => { expr.is_valid_simple_assignment_target(strict) } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction.json index 5fd9d8d0c6e..3c8bb6405c0 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction.json @@ -275,7 +275,7 @@ "type": "VariableDeclarator", "span": { "start": 105, - "end": 118, + "end": 131, "ctxt": 0 }, "id": { @@ -290,37 +290,64 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 110, - "end": 118, + "end": 131, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 112, - "end": 113, - "ctxt": 0 + "expression": { + "type": "ObjectExpression", + "span": { + "start": 110, + "end": 118, + "ctxt": 0 + }, + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 112, + "end": 113, + "ctxt": 0 + }, + "value": "a", + "optional": false }, - "value": "a", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 115, - "end": 116, - "ctxt": 0 - }, - "value": 1.0, - "raw": "1" + "value": { + "type": "NumericLiteral", + "span": { + "start": 115, + "end": 116, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + } } - } - ] + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 129, + "end": 131, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 129, + "end": 131, + "ctxt": 0 + }, + "value": "I1", + "optional": false + }, + "typeParams": null + } }, "definite": false } @@ -340,7 +367,7 @@ "type": "VariableDeclarator", "span": { "start": 145, - "end": 164, + "end": 177, "ctxt": 0 }, "id": { @@ -355,60 +382,87 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 150, - "end": 164, + "end": 177, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 152, - "end": 153, - "ctxt": 0 - }, - "value": "a", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 155, - "end": 156, - "ctxt": 0 - }, - "value": 1.0, - "raw": "1" - } + "expression": { + "type": "ObjectExpression", + "span": { + "start": 150, + "end": 164, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 158, - "end": 159, - "ctxt": 0 + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 152, + "end": 153, + "ctxt": 0 + }, + "value": "a", + "optional": false }, - "value": "b", - "optional": false + "value": { + "type": "NumericLiteral", + "span": { + "start": 155, + "end": 156, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + } }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 161, - "end": 162, - "ctxt": 0 + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 158, + "end": 159, + "ctxt": 0 + }, + "value": "b", + "optional": false }, - "value": 1.0, - "raw": "1" + "value": { + "type": "NumericLiteral", + "span": { + "start": 161, + "end": 162, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + } } - } - ] + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 175, + "end": 177, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 175, + "end": 177, + "ctxt": 0 + }, + "value": "I1", + "optional": false + }, + "typeParams": null + } }, "definite": false } @@ -428,7 +482,7 @@ "type": "VariableDeclarator", "span": { "start": 194, - "end": 202, + "end": 215, "ctxt": 0 }, "id": { @@ -443,13 +497,40 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 199, - "end": 202, + "end": 215, "ctxt": 0 }, - "properties": [] + "expression": { + "type": "ObjectExpression", + "span": { + "start": 199, + "end": 202, + "ctxt": 0 + }, + "properties": [] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 213, + "end": 215, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 213, + "end": 215, + "ctxt": 0 + }, + "value": "I1", + "optional": false + }, + "typeParams": null + } }, "definite": false } @@ -469,7 +550,7 @@ "type": "VariableDeclarator", "span": { "start": 233, - "end": 252, + "end": 265, "ctxt": 0 }, "id": { @@ -510,37 +591,64 @@ } }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 242, - "end": 252, + "end": 265, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 244, - "end": 245, - "ctxt": 0 + "expression": { + "type": "ObjectExpression", + "span": { + "start": 242, + "end": 252, + "ctxt": 0 + }, + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 244, + "end": 245, + "ctxt": 0 + }, + "value": "a", + "optional": false }, - "value": "a", - "optional": false - }, - "value": { - "type": "StringLiteral", - "span": { - "start": 247, - "end": 250, - "ctxt": 0 - }, - "value": "a", - "raw": "\"a\"" + "value": { + "type": "StringLiteral", + "span": { + "start": 247, + "end": 250, + "ctxt": 0 + }, + "value": "a", + "raw": "\"a\"" + } } - } - ] + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 263, + "end": 265, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 263, + "end": 265, + "ctxt": 0 + }, + "value": "T1", + "optional": false + }, + "typeParams": null + } }, "definite": false } @@ -560,7 +668,7 @@ "type": "VariableDeclarator", "span": { "start": 279, - "end": 305, + "end": 318, "ctxt": 0 }, "id": { @@ -575,88 +683,115 @@ "typeAnnotation": null }, "init": { - "type": "ParenthesisExpression", + "type": "TsAsExpression", "span": { "start": 284, - "end": 305, + "end": 318, "ctxt": 0 }, "expression": { - "type": "ArrowFunctionExpression", + "type": "ParenthesisExpression", "span": { - "start": 285, - "end": 304, + "start": 284, + "end": 305, "ctxt": 0 }, - "params": [ - { - "type": "Identifier", - "span": { - "start": 285, - "end": 286, - "ctxt": 0 - }, - "value": "m", - "optional": false, - "typeAnnotation": null - } - ], - "body": { - "type": "CallExpression", + "expression": { + "type": "ArrowFunctionExpression", "span": { - "start": 290, + "start": 285, "end": 304, "ctxt": 0 }, - "callee": { - "type": "MemberExpression", - "span": { - "start": 290, - "end": 301, - "ctxt": 0 - }, - "object": { + "params": [ + { "type": "Identifier", "span": { - "start": 290, - "end": 291, + "start": 285, + "end": 286, "ctxt": 0 }, "value": "m", - "optional": false + "optional": false, + "typeAnnotation": null + } + ], + "body": { + "type": "CallExpression", + "span": { + "start": 290, + "end": 304, + "ctxt": 0 }, - "property": { - "type": "Identifier", + "callee": { + "type": "MemberExpression", "span": { - "start": 292, + "start": 290, "end": 301, "ctxt": 0 }, - "value": "substring", - "optional": false - } - }, - "arguments": [ - { - "spread": null, - "expression": { - "type": "NumericLiteral", + "object": { + "type": "Identifier", "span": { - "start": 302, - "end": 303, + "start": 290, + "end": 291, "ctxt": 0 }, - "value": 0.0, - "raw": "0" + "value": "m", + "optional": false + }, + "property": { + "type": "Identifier", + "span": { + "start": 292, + "end": 301, + "ctxt": 0 + }, + "value": "substring", + "optional": false } - } - ], - "typeArguments": null + }, + "arguments": [ + { + "spread": null, + "expression": { + "type": "NumericLiteral", + "span": { + "start": 302, + "end": 303, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } + } + ], + "typeArguments": null + }, + "async": false, + "generator": false, + "typeParameters": null, + "returnType": null + } + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 316, + "end": 318, + "ctxt": 0 }, - "async": false, - "generator": false, - "typeParameters": null, - "returnType": null + "typeName": { + "type": "Identifier", + "span": { + "start": 316, + "end": 318, + "ctxt": 0 + }, + "value": "T2", + "optional": false + }, + "typeParams": null } }, "definite": false @@ -677,7 +812,7 @@ "type": "VariableDeclarator", "span": { "start": 333, - "end": 344, + "end": 371, "ctxt": 0 }, "id": { @@ -692,40 +827,94 @@ "typeAnnotation": null }, "init": { - "type": "ArrayExpression", + "type": "TsAsExpression", "span": { "start": 338, - "end": 344, + "end": 371, "ctxt": 0 }, - "elements": [ - { - "spread": null, - "expression": { - "type": "NumericLiteral", - "span": { - "start": 339, - "end": 340, - "ctxt": 0 - }, - "value": 1.0, - "raw": "1" - } + "expression": { + "type": "ArrayExpression", + "span": { + "start": 338, + "end": 344, + "ctxt": 0 }, - { - "spread": null, - "expression": { - "type": "NumericLiteral", + "elements": [ + { + "spread": null, + "expression": { + "type": "NumericLiteral", + "span": { + "start": 339, + "end": 340, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + } + }, + { + "spread": null, + "expression": { + "type": "NumericLiteral", + "span": { + "start": 342, + "end": 343, + "ctxt": 0 + }, + "value": 2.0, + "raw": "2" + } + } + ] + }, + "typeAnnotation": { + "type": "TsTupleType", + "span": { + "start": 355, + "end": 371, + "ctxt": 0 + }, + "elemTypes": [ + { + "type": "TsTupleElement", "span": { - "start": 342, - "end": 343, + "start": 356, + "end": 362, "ctxt": 0 }, - "value": 2.0, - "raw": "2" + "label": null, + "ty": { + "type": "TsKeywordType", + "span": { + "start": 356, + "end": 362, + "ctxt": 0 + }, + "kind": "number" + } + }, + { + "type": "TsTupleElement", + "span": { + "start": 364, + "end": 370, + "ctxt": 0 + }, + "label": null, + "ty": { + "type": "TsKeywordType", + "span": { + "start": 364, + "end": 370, + "ctxt": 0 + }, + "kind": "number" + } } - } - ] + ] + } }, "definite": false } @@ -817,7 +1006,7 @@ "type": "VariableDeclarator", "span": { "start": 408, - "end": 426, + "end": 438, "ctxt": 0 }, "id": { @@ -832,37 +1021,64 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 413, - "end": 426, + "end": 438, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 415, - "end": 416, - "ctxt": 0 + "expression": { + "type": "ObjectExpression", + "span": { + "start": 413, + "end": 426, + "ctxt": 0 + }, + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 415, + "end": 416, + "ctxt": 0 + }, + "value": "a", + "optional": false }, - "value": "a", - "optional": false - }, - "value": { - "type": "StringLiteral", - "span": { - "start": 418, - "end": 424, - "ctxt": 0 - }, - "value": "test", - "raw": "'test'" + "value": { + "type": "StringLiteral", + "span": { + "start": 418, + "end": 424, + "ctxt": 0 + }, + "value": "test", + "raw": "'test'" + } } - } - ] + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 437, + "end": 438, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 437, + "end": 438, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } }, "definite": false } @@ -882,7 +1098,7 @@ "type": "VariableDeclarator", "span": { "start": 444, - "end": 473, + "end": 485, "ctxt": 0 }, "id": { @@ -897,60 +1113,87 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 449, - "end": 473, + "end": 485, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 451, - "end": 452, - "ctxt": 0 - }, - "value": "a", - "optional": false - }, - "value": { - "type": "StringLiteral", - "span": { - "start": 454, - "end": 460, - "ctxt": 0 - }, - "value": "test", - "raw": "'test'" - } + "expression": { + "type": "ObjectExpression", + "span": { + "start": 449, + "end": 473, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 462, - "end": 463, - "ctxt": 0 + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 451, + "end": 452, + "ctxt": 0 + }, + "value": "a", + "optional": false }, - "value": "b", - "optional": false + "value": { + "type": "StringLiteral", + "span": { + "start": 454, + "end": 460, + "ctxt": 0 + }, + "value": "test", + "raw": "'test'" + } }, - "value": { - "type": "StringLiteral", - "span": { - "start": 465, - "end": 471, - "ctxt": 0 + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 462, + "end": 463, + "ctxt": 0 + }, + "value": "b", + "optional": false }, - "value": "test", - "raw": "'test'" + "value": { + "type": "StringLiteral", + "span": { + "start": 465, + "end": 471, + "ctxt": 0 + }, + "value": "test", + "raw": "'test'" + } } - } - ] + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 484, + "end": 485, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 484, + "end": 485, + "ctxt": 0 + }, + "value": "A", + "optional": false + }, + "typeParams": null + } }, "definite": false } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_contextualTyping1.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_contextualTyping1.json index 7bcf3efe703..58597ce140a 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_contextualTyping1.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_contextualTyping1.json @@ -152,7 +152,7 @@ "type": "VariableDeclarator", "span": { "start": 67, - "end": 132, + "end": 153, "ctxt": 0 }, "id": { @@ -167,186 +167,213 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 71, - "end": 132, + "end": 153, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 77, - "end": 83, - "ctxt": 0 - }, - "value": "isEven", - "optional": false - }, - "value": { - "type": "ArrowFunctionExpression", - "span": { - "start": 85, - "end": 101, - "ctxt": 0 - }, - "params": [ - { - "type": "Identifier", - "span": { - "start": 85, - "end": 86, - "ctxt": 0 - }, - "value": "n", - "optional": false, - "typeAnnotation": null - } - ], - "body": { - "type": "BinaryExpression", + "expression": { + "type": "ObjectExpression", + "span": { + "start": 71, + "end": 132, + "ctxt": 0 + }, + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", "span": { - "start": 90, + "start": 77, + "end": 83, + "ctxt": 0 + }, + "value": "isEven", + "optional": false + }, + "value": { + "type": "ArrowFunctionExpression", + "span": { + "start": 85, "end": 101, "ctxt": 0 }, - "operator": "===", - "left": { + "params": [ + { + "type": "Identifier", + "span": { + "start": 85, + "end": 86, + "ctxt": 0 + }, + "value": "n", + "optional": false, + "typeAnnotation": null + } + ], + "body": { "type": "BinaryExpression", "span": { "start": 90, - "end": 95, - "ctxt": 0 - }, - "operator": "%", - "left": { - "type": "Identifier", - "span": { - "start": 90, - "end": 91, - "ctxt": 0 - }, - "value": "n", - "optional": false - }, - "right": { - "type": "NumericLiteral", - "span": { - "start": 94, - "end": 95, - "ctxt": 0 - }, - "value": 2.0, - "raw": "2" - } - }, - "right": { - "type": "NumericLiteral", - "span": { - "start": 100, "end": 101, "ctxt": 0 }, - "value": 0.0, - "raw": "0" - } - }, - "async": false, - "generator": false, - "typeParameters": null, - "returnType": null - } - }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 107, - "end": 112, - "ctxt": 0 - }, - "value": "isOdd", - "optional": false - }, - "value": { - "type": "ArrowFunctionExpression", - "span": { - "start": 114, - "end": 130, - "ctxt": 0 - }, - "params": [ - { - "type": "Identifier", - "span": { - "start": 114, - "end": 115, - "ctxt": 0 - }, - "value": "n", - "optional": false, - "typeAnnotation": null - } - ], - "body": { - "type": "BinaryExpression", - "span": { - "start": 119, - "end": 130, - "ctxt": 0 - }, - "operator": "===", - "left": { - "type": "BinaryExpression", - "span": { - "start": 119, - "end": 124, - "ctxt": 0 - }, - "operator": "%", + "operator": "===", "left": { - "type": "Identifier", + "type": "BinaryExpression", "span": { - "start": 119, - "end": 120, + "start": 90, + "end": 95, "ctxt": 0 }, - "value": "n", - "optional": false + "operator": "%", + "left": { + "type": "Identifier", + "span": { + "start": 90, + "end": 91, + "ctxt": 0 + }, + "value": "n", + "optional": false + }, + "right": { + "type": "NumericLiteral", + "span": { + "start": 94, + "end": 95, + "ctxt": 0 + }, + "value": 2.0, + "raw": "2" + } }, "right": { "type": "NumericLiteral", "span": { - "start": 123, - "end": 124, + "start": 100, + "end": 101, "ctxt": 0 }, - "value": 2.0, - "raw": "2" + "value": 0.0, + "raw": "0" } }, - "right": { - "type": "NumericLiteral", + "async": false, + "generator": false, + "typeParameters": null, + "returnType": null + } + }, + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 107, + "end": 112, + "ctxt": 0 + }, + "value": "isOdd", + "optional": false + }, + "value": { + "type": "ArrowFunctionExpression", + "span": { + "start": 114, + "end": 130, + "ctxt": 0 + }, + "params": [ + { + "type": "Identifier", + "span": { + "start": 114, + "end": 115, + "ctxt": 0 + }, + "value": "n", + "optional": false, + "typeAnnotation": null + } + ], + "body": { + "type": "BinaryExpression", "span": { - "start": 129, + "start": 119, "end": 130, "ctxt": 0 }, - "value": 1.0, - "raw": "1" - } - }, - "async": false, - "generator": false, - "typeParameters": null, - "returnType": null + "operator": "===", + "left": { + "type": "BinaryExpression", + "span": { + "start": 119, + "end": 124, + "ctxt": 0 + }, + "operator": "%", + "left": { + "type": "Identifier", + "span": { + "start": 119, + "end": 120, + "ctxt": 0 + }, + "value": "n", + "optional": false + }, + "right": { + "type": "NumericLiteral", + "span": { + "start": 123, + "end": 124, + "ctxt": 0 + }, + "value": 2.0, + "raw": "2" + } + }, + "right": { + "type": "NumericLiteral", + "span": { + "start": 129, + "end": 130, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + } + }, + "async": false, + "generator": false, + "typeParameters": null, + "returnType": null + } } - } - ] + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 143, + "end": 153, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 143, + "end": 153, + "ctxt": 0 + }, + "value": "Predicates", + "optional": false + }, + "typeParams": null + } }, "definite": false } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_ensureInterfaceImpl.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_ensureInterfaceImpl.json index b6ecd90df85..b4e685055e1 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_ensureInterfaceImpl.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_ensureInterfaceImpl.json @@ -118,7 +118,7 @@ "type": "VariableDeclarator", "span": { "start": 62, - "end": 154, + "end": 198, "ctxt": 0 }, "id": { @@ -133,134 +133,217 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 68, - "end": 154, + "end": 198, "ctxt": 0 }, - "properties": [ - { - "type": "MethodProperty", - "key": { - "type": "Identifier", + "expression": { + "type": "ObjectExpression", + "span": { + "start": 68, + "end": 154, + "ctxt": 0 + }, + "properties": [ + { + "type": "MethodProperty", + "key": { + "type": "Identifier", + "span": { + "start": 74, + "end": 79, + "ctxt": 0 + }, + "value": "start", + "optional": false + }, + "params": [], + "decorators": [], "span": { "start": 74, - "end": 79, - "ctxt": 0 - }, - "value": "start", - "optional": false - }, - "params": [], - "decorators": [], - "span": { - "start": 74, - "end": 85, - "ctxt": 0 - }, - "body": { - "type": "BlockStatement", - "span": { - "start": 82, "end": 85, "ctxt": 0 }, - "stmts": [] - }, - "generator": false, - "async": false, - "typeParameters": null, - "returnType": null - }, - { - "type": "MethodProperty", - "key": { - "type": "Identifier", - "span": { - "start": 91, - "end": 95, - "ctxt": 0 - }, - "value": "move", - "optional": false - }, - "params": [ - { - "type": "Parameter", + "body": { + "type": "BlockStatement", "span": { - "start": 96, - "end": 97, + "start": 82, + "end": 85, "ctxt": 0 }, - "decorators": [], - "pat": { - "type": "Identifier", + "stmts": [] + }, + "generator": false, + "async": false, + "typeParameters": null, + "returnType": null + }, + { + "type": "MethodProperty", + "key": { + "type": "Identifier", + "span": { + "start": 91, + "end": 95, + "ctxt": 0 + }, + "value": "move", + "optional": false + }, + "params": [ + { + "type": "Parameter", "span": { "start": 96, "end": 97, "ctxt": 0 }, - "value": "d", - "optional": false, - "typeAnnotation": null + "decorators": [], + "pat": { + "type": "Identifier", + "span": { + "start": 96, + "end": 97, + "ctxt": 0 + }, + "value": "d", + "optional": false, + "typeAnnotation": null + } } - } - ], - "decorators": [], - "span": { - "start": 91, - "end": 136, - "ctxt": 0 - }, - "body": { - "type": "BlockStatement", + ], + "decorators": [], "span": { - "start": 99, + "start": 91, "end": 136, "ctxt": 0 }, - "stmts": [] + "body": { + "type": "BlockStatement", + "span": { + "start": 99, + "end": 136, + "ctxt": 0 + }, + "stmts": [] + }, + "generator": false, + "async": false, + "typeParameters": null, + "returnType": null }, - "generator": false, - "async": false, - "typeParameters": null, - "returnType": null - }, - { - "type": "MethodProperty", - "key": { - "type": "Identifier", + { + "type": "MethodProperty", + "key": { + "type": "Identifier", + "span": { + "start": 142, + "end": 146, + "ctxt": 0 + }, + "value": "stop", + "optional": false + }, + "params": [], + "decorators": [], "span": { "start": 142, - "end": 146, - "ctxt": 0 - }, - "value": "stop", - "optional": false - }, - "params": [], - "decorators": [], - "span": { - "start": 142, - "end": 152, - "ctxt": 0 - }, - "body": { - "type": "BlockStatement", - "span": { - "start": 149, "end": 152, "ctxt": 0 }, - "stmts": [] + "body": { + "type": "BlockStatement", + "span": { + "start": 149, + "end": 152, + "ctxt": 0 + }, + "stmts": [] + }, + "generator": false, + "async": false, + "typeParameters": null, + "returnType": null + } + ] + }, + "typeAnnotation": { + "type": "TsIntersectionType", + "span": { + "start": 165, + "end": 198, + "ctxt": 0 + }, + "types": [ + { + "type": "TsTypeReference", + "span": { + "start": 165, + "end": 172, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 165, + "end": 172, + "ctxt": 0 + }, + "value": "Movable", + "optional": false + }, + "typeParams": null }, - "generator": false, - "async": false, - "typeParameters": null, - "returnType": null - } - ] + { + "type": "TsTypeReference", + "span": { + "start": 175, + "end": 198, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 175, + "end": 181, + "ctxt": 0 + }, + "value": "Record", + "optional": false + }, + "typeParams": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 181, + "end": 198, + "ctxt": 0 + }, + "params": [ + { + "type": "TsKeywordType", + "span": { + "start": 182, + "end": 188, + "ctxt": 0 + }, + "kind": "string" + }, + { + "type": "TsKeywordType", + "span": { + "start": 190, + "end": 197, + "ctxt": 0 + }, + "kind": "unknown" + } + ] + } + } + ] + } }, "definite": false } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_js.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_js.json index 33bf5d08676..f8cd9727208 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_js.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_js.json @@ -20,7 +20,7 @@ "type": "VariableDeclarator", "span": { "start": 67, - "end": 80, + "end": 92, "ctxt": 0 }, "id": { @@ -35,14 +35,40 @@ "typeAnnotation": null }, "init": { - "type": "Identifier", + "type": "TsAsExpression", "span": { "start": 71, - "end": 80, + "end": 92, "ctxt": 0 }, - "value": "undefined", - "optional": false + "expression": { + "type": "Identifier", + "span": { + "start": 71, + "end": 80, + "ctxt": 0 + }, + "value": "undefined", + "optional": false + }, + "typeAnnotation": { + "type": "TsLiteralType", + "span": { + "start": 91, + "end": 92, + "ctxt": 0 + }, + "literal": { + "type": "NumericLiteral", + "span": { + "start": 91, + "end": 92, + "ctxt": 0 + }, + "value": 1.0, + "raw": "1" + } + } }, "definite": false } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_optionalMemberConformance.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_optionalMemberConformance.json index 1d432776b93..fea0e767553 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_optionalMemberConformance.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_optionalMemberConformance.json @@ -132,7 +132,7 @@ "type": "VariableDeclarator", "span": { "start": 99, - "end": 112, + "end": 139, "ctxt": 0 }, "id": { @@ -147,37 +147,92 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 103, - "end": 112, + "end": 139, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 105, - "end": 106, - "ctxt": 0 + "expression": { + "type": "ObjectExpression", + "span": { + "start": 103, + "end": 112, + "ctxt": 0 + }, + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 105, + "end": 106, + "ctxt": 0 + }, + "value": "x", + "optional": false }, - "value": "x", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 108, - "end": 110, - "ctxt": 0 - }, - "value": 10.0, - "raw": "10" + "value": { + "type": "NumericLiteral", + "span": { + "start": 108, + "end": 110, + "ctxt": 0 + }, + "value": 10.0, + "raw": "10" + } } + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 123, + "end": 139, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 123, + "end": 130, + "ctxt": 0 + }, + "value": "Partial", + "optional": false + }, + "typeParams": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 130, + "end": 139, + "ctxt": 0 + }, + "params": [ + { + "type": "TsTypeReference", + "span": { + "start": 131, + "end": 138, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 131, + "end": 138, + "ctxt": 0 + }, + "value": "Point2d", + "optional": false + }, + "typeParams": null + } + ] } - ] + } }, "definite": false } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propNameConstraining.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propNameConstraining.json index e02abb7aa1f..a18fe3f1dea 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propNameConstraining.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propNameConstraining.json @@ -122,7 +122,7 @@ "type": "VariableDeclarator", "span": { "start": 43, - "end": 122, + "end": 163, "ctxt": 0 }, "id": { @@ -137,83 +137,175 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 47, - "end": 122, + "end": 163, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 53, - "end": 54, - "ctxt": 0 - }, - "value": "a", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 56, - "end": 57, - "ctxt": 0 - }, - "value": 0.0, - "raw": "0" - } + "expression": { + "type": "ObjectExpression", + "span": { + "start": 47, + "end": 122, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 63, - "end": 64, - "ctxt": 0 + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 53, + "end": 54, + "ctxt": 0 + }, + "value": "a", + "optional": false }, - "value": "b", - "optional": false + "value": { + "type": "NumericLiteral", + "span": { + "start": 56, + "end": 57, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } }, - "value": { - "type": "StringLiteral", - "span": { - "start": 66, - "end": 73, - "ctxt": 0 + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 63, + "end": 64, + "ctxt": 0 + }, + "value": "b", + "optional": false }, - "value": "hello", - "raw": "\"hello\"" + "value": { + "type": "StringLiteral", + "span": { + "start": 66, + "end": 73, + "ctxt": 0 + }, + "value": "hello", + "raw": "\"hello\"" + } + }, + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 79, + "end": 80, + "ctxt": 0 + }, + "value": "x", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 82, + "end": 83, + "ctxt": 0 + }, + "value": 8.0, + "raw": "8" + } } + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 133, + "end": 163, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 79, - "end": 80, - "ctxt": 0 - }, - "value": "x", - "optional": false + "typeName": { + "type": "Identifier", + "span": { + "start": 133, + "end": 140, + "ctxt": 0 }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 82, - "end": 83, - "ctxt": 0 - }, - "value": 8.0, - "raw": "8" - } + "value": "Partial", + "optional": false + }, + "typeParams": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 140, + "end": 163, + "ctxt": 0 + }, + "params": [ + { + "type": "TsTypeReference", + "span": { + "start": 141, + "end": 162, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 141, + "end": 147, + "ctxt": 0 + }, + "value": "Record", + "optional": false + }, + "typeParams": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 147, + "end": 162, + "ctxt": 0 + }, + "params": [ + { + "type": "TsTypeReference", + "span": { + "start": 148, + "end": 152, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 148, + "end": 152, + "ctxt": 0 + }, + "value": "Keys", + "optional": false + }, + "typeParams": null + }, + { + "type": "TsKeywordType", + "span": { + "start": 154, + "end": 161, + "ctxt": 0 + }, + "kind": "unknown" + } + ] + } + } + ] } - ] + } }, "definite": false } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyNameFulfillment.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyNameFulfillment.json index d08f61615b0..e9905c22886 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyNameFulfillment.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyNameFulfillment.json @@ -122,7 +122,7 @@ "type": "VariableDeclarator", "span": { "start": 43, - "end": 122, + "end": 154, "ctxt": 0 }, "id": { @@ -137,83 +137,147 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 47, - "end": 122, + "end": 154, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 53, - "end": 54, - "ctxt": 0 - }, - "value": "a", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 56, - "end": 57, - "ctxt": 0 - }, - "value": 0.0, - "raw": "0" - } + "expression": { + "type": "ObjectExpression", + "span": { + "start": 47, + "end": 122, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 63, - "end": 64, - "ctxt": 0 + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 53, + "end": 54, + "ctxt": 0 + }, + "value": "a", + "optional": false }, - "value": "b", - "optional": false + "value": { + "type": "NumericLiteral", + "span": { + "start": 56, + "end": 57, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } }, - "value": { - "type": "StringLiteral", - "span": { - "start": 66, - "end": 73, - "ctxt": 0 + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 63, + "end": 64, + "ctxt": 0 + }, + "value": "b", + "optional": false }, - "value": "hello", - "raw": "\"hello\"" + "value": { + "type": "StringLiteral", + "span": { + "start": 66, + "end": 73, + "ctxt": 0 + }, + "value": "hello", + "raw": "\"hello\"" + } + }, + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 79, + "end": 80, + "ctxt": 0 + }, + "value": "x", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 82, + "end": 83, + "ctxt": 0 + }, + "value": 8.0, + "raw": "8" + } } + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 133, + "end": 154, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 79, - "end": 80, - "ctxt": 0 - }, - "value": "x", - "optional": false + "typeName": { + "type": "Identifier", + "span": { + "start": 133, + "end": 139, + "ctxt": 0 }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 82, - "end": 83, - "ctxt": 0 + "value": "Record", + "optional": false + }, + "typeParams": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 139, + "end": 154, + "ctxt": 0 + }, + "params": [ + { + "type": "TsTypeReference", + "span": { + "start": 140, + "end": 144, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 140, + "end": 144, + "ctxt": 0 + }, + "value": "Keys", + "optional": false + }, + "typeParams": null }, - "value": 8.0, - "raw": "8" - } + { + "type": "TsKeywordType", + "span": { + "start": 146, + "end": 153, + "ctxt": 0 + }, + "kind": "unknown" + } + ] } - ] + } }, "definite": false } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance1.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance1.json index 4be382c8ef1..78d1eb87625 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance1.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance1.json @@ -666,7 +666,7 @@ "type": "VariableDeclarator", "span": { "start": 471, - "end": 507, + "end": 523, "ctxt": 0 }, "id": { @@ -681,59 +681,86 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 476, - "end": 507, + "end": 523, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 482, - "end": 483, - "ctxt": 0 - }, - "value": "m", - "optional": false - }, - "value": { - "type": "BooleanLiteral", - "span": { - "start": 485, - "end": 489, - "ctxt": 0 - }, - "value": true - } + "expression": { + "type": "ObjectExpression", + "span": { + "start": 476, + "end": 507, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 495, - "end": 496, - "ctxt": 0 + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 482, + "end": 483, + "ctxt": 0 + }, + "value": "m", + "optional": false }, - "value": "s", - "optional": false + "value": { + "type": "BooleanLiteral", + "span": { + "start": 485, + "end": 489, + "ctxt": 0 + }, + "value": true + } }, - "value": { - "type": "StringLiteral", - "span": { - "start": 498, - "end": 505, - "ctxt": 0 + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 495, + "end": 496, + "ctxt": 0 + }, + "value": "s", + "optional": false }, - "value": "false", - "raw": "\"false\"" + "value": { + "type": "StringLiteral", + "span": { + "start": 498, + "end": 505, + "ctxt": 0 + }, + "value": "false", + "raw": "\"false\"" + } } - } - ] + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 518, + "end": 523, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 518, + "end": 523, + "ctxt": 0 + }, + "value": "Facts", + "optional": false + }, + "typeParams": null + } }, "definite": false } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance2.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance2.json index 3f55e207aa1..3643523c63c 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance2.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance2.json @@ -666,7 +666,7 @@ "type": "VariableDeclarator", "span": { "start": 452, - "end": 488, + "end": 504, "ctxt": 0 }, "id": { @@ -681,59 +681,86 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 457, - "end": 488, + "end": 504, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 463, - "end": 464, - "ctxt": 0 - }, - "value": "m", - "optional": false - }, - "value": { - "type": "BooleanLiteral", - "span": { - "start": 466, - "end": 470, - "ctxt": 0 - }, - "value": true - } + "expression": { + "type": "ObjectExpression", + "span": { + "start": 457, + "end": 488, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 476, - "end": 477, - "ctxt": 0 + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 463, + "end": 464, + "ctxt": 0 + }, + "value": "m", + "optional": false }, - "value": "s", - "optional": false + "value": { + "type": "BooleanLiteral", + "span": { + "start": 466, + "end": 470, + "ctxt": 0 + }, + "value": true + } }, - "value": { - "type": "StringLiteral", - "span": { - "start": 479, - "end": 486, - "ctxt": 0 + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 476, + "end": 477, + "ctxt": 0 + }, + "value": "s", + "optional": false }, - "value": "false", - "raw": "\"false\"" + "value": { + "type": "StringLiteral", + "span": { + "start": 479, + "end": 486, + "ctxt": 0 + }, + "value": "false", + "raw": "\"false\"" + } } - } - ] + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 499, + "end": 504, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 499, + "end": 504, + "ctxt": 0 + }, + "value": "Facts", + "optional": false + }, + "typeParams": null + } }, "definite": false } diff --git a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance3.json b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance3.json index 9e427381649..09062330e61 100644 --- a/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance3.json +++ b/crates/swc_ecma_parser/tests/tsc/typeSatisfaction_propertyValueConformance3.json @@ -188,7 +188,7 @@ "type": "VariableDeclarator", "span": { "start": 140, - "end": 291, + "end": 323, "ctxt": 0 }, "id": { @@ -203,290 +203,354 @@ "typeAnnotation": null }, "init": { - "type": "ObjectExpression", + "type": "TsAsExpression", "span": { "start": 150, - "end": 291, + "end": 323, "ctxt": 0 }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 156, - "end": 161, - "ctxt": 0 - }, - "value": "white", - "optional": false - }, - "value": { - "type": "ObjectExpression", - "span": { - "start": 163, - "end": 189, - "ctxt": 0 - }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 165, - "end": 166, - "ctxt": 0 - }, - "value": "r", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 168, - "end": 171, - "ctxt": 0 - }, - "value": 255.0, - "raw": "255" - } - }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 173, - "end": 174, - "ctxt": 0 - }, - "value": "g", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 176, - "end": 179, - "ctxt": 0 - }, - "value": 255.0, - "raw": "255" - } - }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 181, - "end": 182, - "ctxt": 0 - }, - "value": "b", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 184, - "end": 187, - "ctxt": 0 - }, - "value": 255.0, - "raw": "255" - } - } - ] - } + "expression": { + "type": "ObjectExpression", + "span": { + "start": 150, + "end": 291, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 195, - "end": 200, - "ctxt": 0 + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 156, + "end": 161, + "ctxt": 0 + }, + "value": "white", + "optional": false }, - "value": "black", - "optional": false + "value": { + "type": "ObjectExpression", + "span": { + "start": 163, + "end": 189, + "ctxt": 0 + }, + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 165, + "end": 166, + "ctxt": 0 + }, + "value": "r", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 168, + "end": 171, + "ctxt": 0 + }, + "value": 255.0, + "raw": "255" + } + }, + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 173, + "end": 174, + "ctxt": 0 + }, + "value": "g", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 176, + "end": 179, + "ctxt": 0 + }, + "value": 255.0, + "raw": "255" + } + }, + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 181, + "end": 182, + "ctxt": 0 + }, + "value": "b", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 184, + "end": 187, + "ctxt": 0 + }, + "value": 255.0, + "raw": "255" + } + } + ] + } }, - "value": { - "type": "ObjectExpression", - "span": { - "start": 202, - "end": 222, - "ctxt": 0 + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 195, + "end": 200, + "ctxt": 0 + }, + "value": "black", + "optional": false }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 204, - "end": 205, - "ctxt": 0 - }, - "value": "r", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 207, - "end": 208, - "ctxt": 0 - }, - "value": 0.0, - "raw": "0" - } + "value": { + "type": "ObjectExpression", + "span": { + "start": 202, + "end": 222, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 210, - "end": 211, - "ctxt": 0 + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 204, + "end": 205, + "ctxt": 0 + }, + "value": "r", + "optional": false }, - "value": "g", - "optional": false + "value": { + "type": "NumericLiteral", + "span": { + "start": 207, + "end": 208, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 213, - "end": 214, - "ctxt": 0 + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 210, + "end": 211, + "ctxt": 0 + }, + "value": "g", + "optional": false }, - "value": 0.0, - "raw": "0" + "value": { + "type": "NumericLiteral", + "span": { + "start": 213, + "end": 214, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } + }, + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 216, + "end": 217, + "ctxt": 0 + }, + "value": "d", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 219, + "end": 220, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } } + ] + } + }, + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 260, + "end": 264, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 216, - "end": 217, - "ctxt": 0 + "value": "blue", + "optional": false + }, + "value": { + "type": "ObjectExpression", + "span": { + "start": 266, + "end": 288, + "ctxt": 0 + }, + "properties": [ + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 268, + "end": 269, + "ctxt": 0 + }, + "value": "r", + "optional": false }, - "value": "d", - "optional": false + "value": { + "type": "NumericLiteral", + "span": { + "start": 271, + "end": 272, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 219, - "end": 220, - "ctxt": 0 + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 274, + "end": 275, + "ctxt": 0 + }, + "value": "g", + "optional": false }, - "value": 0.0, - "raw": "0" + "value": { + "type": "NumericLiteral", + "span": { + "start": 277, + "end": 278, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } + }, + { + "type": "KeyValueProperty", + "key": { + "type": "Identifier", + "span": { + "start": 280, + "end": 281, + "ctxt": 0 + }, + "value": "b", + "optional": false + }, + "value": { + "type": "NumericLiteral", + "span": { + "start": 283, + "end": 286, + "ctxt": 0 + }, + "value": 255.0, + "raw": "255" + } } - } - ] + ] + } } + ] + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 302, + "end": 323, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 260, - "end": 264, - "ctxt": 0 - }, - "value": "blue", - "optional": false + "typeName": { + "type": "Identifier", + "span": { + "start": 302, + "end": 308, + "ctxt": 0 }, - "value": { - "type": "ObjectExpression", - "span": { - "start": 266, - "end": 288, - "ctxt": 0 + "value": "Record", + "optional": false + }, + "typeParams": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 308, + "end": 323, + "ctxt": 0 + }, + "params": [ + { + "type": "TsKeywordType", + "span": { + "start": 309, + "end": 315, + "ctxt": 0 + }, + "kind": "string" }, - "properties": [ - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 268, - "end": 269, - "ctxt": 0 - }, - "value": "r", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 271, - "end": 272, - "ctxt": 0 - }, - "value": 0.0, - "raw": "0" - } + { + "type": "TsTypeReference", + "span": { + "start": 317, + "end": 322, + "ctxt": 0 }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 274, - "end": 275, - "ctxt": 0 - }, - "value": "g", - "optional": false + "typeName": { + "type": "Identifier", + "span": { + "start": 317, + "end": 322, + "ctxt": 0 }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 277, - "end": 278, - "ctxt": 0 - }, - "value": 0.0, - "raw": "0" - } + "value": "Color", + "optional": false }, - { - "type": "KeyValueProperty", - "key": { - "type": "Identifier", - "span": { - "start": 280, - "end": 281, - "ctxt": 0 - }, - "value": "b", - "optional": false - }, - "value": { - "type": "NumericLiteral", - "span": { - "start": 283, - "end": 286, - "ctxt": 0 - }, - "value": 255.0, - "raw": "255" - } - } - ] - } + "typeParams": null + } + ] } - ] + } }, "definite": false } diff --git a/crates/swc_ecma_quote_macros/src/ast/expr.rs b/crates/swc_ecma_quote_macros/src/ast/expr.rs index 93cee783b92..5ac395da59e 100644 --- a/crates/swc_ecma_quote_macros/src/ast/expr.rs +++ b/crates/swc_ecma_quote_macros/src/ast/expr.rs @@ -38,6 +38,7 @@ impl_enum!( TsNonNull, TsAs, TsInstantiation, + TsSatisfaction, PrivateName, OptChain, Invalid diff --git a/crates/swc_ecma_quote_macros/src/ast/typescript.rs b/crates/swc_ecma_quote_macros/src/ast/typescript.rs index 00407a7be6b..79dc08a5eb9 100644 --- a/crates/swc_ecma_quote_macros/src/ast/typescript.rs +++ b/crates/swc_ecma_quote_macros/src/ast/typescript.rs @@ -19,3 +19,4 @@ fail_todo!(TsTypeParamDecl); fail_todo!(TsExprWithTypeArgs); fail_todo!(TsIndexSignature); fail_todo!(TsParamProp); +fail_todo!(TsSatisfactionExpr); diff --git a/crates/swc_ecma_transforms_compat/src/es2015/destructuring.rs b/crates/swc_ecma_transforms_compat/src/es2015/destructuring.rs index a5c4b68e588..e915c3a9490 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/destructuring.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/destructuring.rs @@ -1170,14 +1170,12 @@ fn can_be_null(e: &Expr) -> bool { | Expr::Member(..) | Expr::SuperProp(..) | Expr::Call(..) - // an opt chain is either a member or a call | Expr::OptChain(..) | Expr::New(..) | Expr::Yield(..) | Expr::Await(..) | Expr::MetaProp(..) => true, - // This does not include null Expr::Lit(..) => false, Expr::Array(..) @@ -1198,7 +1196,6 @@ fn can_be_null(e: &Expr) -> bool { ref cons, ref alt, .. }) => can_be_null(cons) || can_be_null(alt), - // TODO(kdy1): I'm not sure about this. Expr::Unary(..) | Expr::Update(..) | Expr::Bin(..) => true, Expr::JSXMember(..) @@ -1207,12 +1204,12 @@ fn can_be_null(e: &Expr) -> bool { | Expr::JSXElement(..) | Expr::JSXFragment(..) => unreachable!("destructuring jsx"), - // Trust user Expr::TsNonNull(..) => false, Expr::TsAs(TsAsExpr { ref expr, .. }) | Expr::TsTypeAssertion(TsTypeAssertion { ref expr, .. }) | Expr::TsConstAssertion(TsConstAssertion { ref expr, .. }) - | Expr::TsInstantiation(TsInstantiation { ref expr, .. }) => can_be_null(expr), + | Expr::TsInstantiation(TsInstantiation { ref expr, .. }) + | Expr::TsSatisfaction(TsSatisfactionExpr { ref expr, .. }) => can_be_null(expr), Expr::Invalid(..) => unreachable!(), } diff --git a/crates/swc_ecma_transforms_typescript/src/strip.rs b/crates/swc_ecma_transforms_typescript/src/strip.rs index 9c1cf056daf..3b61019df06 100644 --- a/crates/swc_ecma_transforms_typescript/src/strip.rs +++ b/crates/swc_ecma_transforms_typescript/src/strip.rs @@ -436,7 +436,8 @@ where | Expr::TsNonNull(TsNonNullExpr { expr, .. }) | Expr::TsTypeAssertion(TsTypeAssertion { expr, .. }) | Expr::TsConstAssertion(TsConstAssertion { expr, .. }) - | Expr::TsInstantiation(TsInstantiation { expr, .. }) => { + | Expr::TsInstantiation(TsInstantiation { expr, .. }) + | Expr::TsSatisfaction(TsSatisfactionExpr { expr, .. }) => { expr.visit_mut_with(self); let expr = *expr.take(); *n = expr; diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs index 14249205165..688d9235d2c 100644 --- a/crates/swc_ecma_utils/src/lib.rs +++ b/crates/swc_ecma_utils/src/lib.rs @@ -1454,7 +1454,8 @@ pub trait ExprExt { Expr::TsAs(TsAsExpr { ref expr, .. }) | Expr::TsNonNull(TsNonNullExpr { ref expr, .. }) | Expr::TsTypeAssertion(TsTypeAssertion { ref expr, .. }) - | Expr::TsInstantiation(TsInstantiation { ref expr, .. }) => { + | Expr::TsInstantiation(TsInstantiation { ref expr, .. }) + | Expr::TsSatisfaction(TsSatisfactionExpr { ref expr, .. }) => { expr.may_have_side_effects(ctx) } @@ -2422,7 +2423,8 @@ impl ExprCtx { | Expr::TsNonNull(TsNonNullExpr { expr, .. }) | Expr::TsAs(TsAsExpr { expr, .. }) | Expr::TsConstAssertion(TsConstAssertion { expr, .. }) - | Expr::TsInstantiation(TsInstantiation { expr, .. }) => { + | Expr::TsInstantiation(TsInstantiation { expr, .. }) + | Expr::TsSatisfaction(TsSatisfactionExpr { expr, .. }) => { self.extract_side_effects_to(to, *expr) } Expr::OptChain(OptChainExpr { base: child, .. }) => { diff --git a/crates/swc_ecma_visit/src/lib.rs b/crates/swc_ecma_visit/src/lib.rs index ad53b90b702..42faf58544a 100644 --- a/crates/swc_ecma_visit/src/lib.rs +++ b/crates/swc_ecma_visit/src/lib.rs @@ -660,6 +660,7 @@ define!({ TsConstAssertion(TsConstAssertion), TsNonNull(TsNonNullExpr), TsAs(TsAsExpr), + TsSatisfaction(TsSatisfactionExpr), TsInstantiation(TsInstantiation), PrivateName(PrivateName), OptChain(OptChainExpr), diff --git a/crates/swc_estree_compat/src/babelify/expr.rs b/crates/swc_estree_compat/src/babelify/expr.rs index 7b9a9cdc467..d10aeb45899 100644 --- a/crates/swc_estree_compat/src/babelify/expr.rs +++ b/crates/swc_estree_compat/src/babelify/expr.rs @@ -157,6 +157,10 @@ impl Babelify for Expr { "illegal conversion: Cannot convert {:?} to ExprOutput - babel has no equivalent", &self ), + Expr::TsSatisfaction(_) => panic!( + "illegal conversion: Cannot convert {:?} to ExprOutput - babel has no equivalent", + &self + ), Expr::OptChain(_) => panic!( "illegal conversion: Cannot convert {:?} to ExprOutput - babel has no equivalent", &self diff --git a/postinstall.js b/postinstall.js index 8071c11b3c2..fa278f2945e 100644 --- a/postinstall.js +++ b/postinstall.js @@ -1 +1,227 @@ -// This'll be generated by build process for the installation of the `@swc/core` package. \ No newline at end of file +"use strict"; +var __createBinding = + (this && this.__createBinding) || + (Object.create + ? function (o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if ( + !desc || + ("get" in desc + ? !m.__esModule + : desc.writable || desc.configurable) + ) { + desc = { + enumerable: true, + get: function () { + return m[k]; + }, + }; + } + Object.defineProperty(o, k2, desc); + } + : function (o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; + }); +var __setModuleDefault = + (this && this.__setModuleDefault) || + (Object.create + ? function (o, v) { + Object.defineProperty(o, "default", { + enumerable: true, + value: v, + }); + } + : function (o, v) { + o["default"] = v; + }); +var __importStar = + (this && this.__importStar) || + function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) + for (var k in mod) + if ( + k !== "default" && + Object.prototype.hasOwnProperty.call(mod, k) + ) + __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; + }; +var __awaiter = + (this && this.__awaiter) || + function (thisArg, _arguments, P, generator) { + function adopt(value) { + return value instanceof P + ? value + : new P(function (resolve) { + resolve(value); + }); + } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { + try { + step(generator.next(value)); + } catch (e) { + reject(e); + } + } + function rejected(value) { + try { + step(generator["throw"](value)); + } catch (e) { + reject(e); + } + } + function step(result) { + result.done + ? resolve(result.value) + : adopt(result.value).then(fulfilled, rejected); + } + step( + (generator = generator.apply(thisArg, _arguments || [])).next() + ); + }); + }; +Object.defineProperty(exports, "__esModule", { value: true }); +/** + * A postinstall script runs after `@swc/core` is installed. + * + * It checks if corresponding optional dependencies for native binary is installed and can be loaded properly. + * If it fails, it'll internally try to install `@swc/wasm` as fallback. + */ +const fs_1 = require("fs"); +const assert = __importStar(require("assert")); +const path = __importStar(require("path")); +const child_process = __importStar(require("child_process")); +const fs = __importStar(require("fs")); +function removeRecursive(dir) { + for (const entry of fs.readdirSync(dir)) { + const entryPath = path.join(dir, entry); + let stats; + try { + stats = fs.lstatSync(entryPath); + } catch (_a) { + continue; // Guard against https://github.com/nodejs/node/issues/4760 + } + if (stats.isDirectory()) removeRecursive(entryPath); + else fs.unlinkSync(entryPath); + } + fs.rmdirSync(dir); +} +/** + * Trying to validate @swc/core's native binary installation, then installs if it is not supported. + */ +const validateBinary = () => + __awaiter(void 0, void 0, void 0, function* () { + var _a; + try { + const { name } = require(path.resolve( + process.env.INIT_CWD, + "package.json" + )); + if (name === "@swc/core") { + return; + } + } catch (_) { + return; + } + // TODO: We do not take care of the case if user try to install with `--no-optional`. + // For now, it is considered as deliberate decision. + let binding; + try { + binding = require("./binding"); + // Check if binding binary actually works. + // For the latest version, checks target triple. If it's old version doesn't have target triple, use parseSync instead. + const triple = binding.getTargetTriple + ? binding.getTargetTriple() + : binding.parseSync( + "console.log()", + Buffer.from(JSON.stringify({ syntax: "ecmascript" })) + ); + assert.ok( + triple, + "Failed to read target triple from native binary." + ); + } catch (error) { + // if error is unsupported architecture, ignore to display. + if ( + !((_a = error.message) === null || _a === void 0 + ? void 0 + : _a.includes("Unsupported architecture")) + ) { + console.warn(error); + } + console.warn( + `@swc/core was not able to resolve native bindings installation. It'll try to use @swc/wasm as fallback instead.` + ); + } + if (!!binding) { + return; + } + // User choose to override the binary installation. Skip remanining validation. + if (!!process.env["SWC_BINARY_PATH"]) { + console.warn( + `@swc/core could not resolve native bindings installation, but found manual override config SWC_BINARY_PATH specified. Skipping remaning validation.` + ); + return; + } + // Check if top-level package.json installs @swc/wasm separately already + let wasmBinding; + try { + wasmBinding = require.resolve(`@swc/wasm`); + } catch (_) {} + if (!!wasmBinding && (0, fs_1.existsSync)(wasmBinding)) { + return; + } + const env = Object.assign(Object.assign({}, process.env), { + npm_config_global: undefined, + }); + const { version } = require(path.join( + path.dirname(require.resolve("@swc/core")), + "package.json" + )); + // We want to place @swc/wasm next to the @swc/core as if normal installation was done, + // but can't directly set cwd to INIT_CWD as npm seems to acquire lock to the working dir. + // Instead, create a temporary inner and move it out. + const coreDir = path.dirname(require.resolve("@swc/core")); + const installDir = path.join(coreDir, "npm-install"); + try { + fs.mkdirSync(installDir); + fs.writeFileSync(path.join(installDir, "package.json"), "{}"); + // Instead of carrying over own dependencies to download & resolve package which increases installation sizes of `@swc/core`, + // assume & relies on system's npm installation. + child_process.execSync( + `npm install --no-save --loglevel=error --prefer-offline --no-audit --progress=false @swc/wasm@${version}`, + { cwd: installDir, stdio: "pipe", env } + ); + const installedBinPath = path.join( + installDir, + "node_modules", + `@swc/wasm` + ); + // INIT_CWD is injected via npm. If it doesn't exists, can't proceed. + fs.renameSync( + installedBinPath, + path.resolve(process.env.INIT_CWD, "node_modules", `@swc/wasm`) + ); + } catch (error) { + console.error(error); + console.error(`Failed to install fallback @swc/wasm@${version}. @swc/core will not properly. +Please install @swc/wasm manually, or retry whole installation. +If there are unexpected errors, please report at https://github.com/swc-project/swc/issues`); + } finally { + try { + removeRecursive(installDir); + } catch (_) { + // Gracefully ignore any failures. This'll make few leftover files but it shouldn't block installation. + } + } + }); +validateBinary().catch((error) => { + // for now just throw the error as-is. + throw error; +});