Skip to content

Commit

Permalink
fix(es/parser): Consider ASI while parsing TypeScript type aliases (#…
Browse files Browse the repository at this point in the history
…8263)

**Related issue:**

 - Closes #8073.
  • Loading branch information
kdy1 committed Nov 10, 2023
1 parent e0525b5 commit e589126
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _class_private_field_get = require("@swc/helpers/_/_class_private_field_get");
var _class_private_field_init = require("@swc/helpers/_/_class_private_field_init");
var _x = /*#__PURE__*/ new WeakMap();
var _x = new WeakMap();
class Foo {
test() {
var _this, _this_y;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _class_private_field_get = require("@swc/helpers/_/_class_private_field_get");
var _class_private_field_init = require("@swc/helpers/_/_class_private_field_init");
var _x = /*#__PURE__*/ new WeakMap();
var _x = new WeakMap();
class Foo {
test() {
var _this, _this_y;
Expand Down
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8073/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": "unknown"
}
8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8073/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
avplay.setListener({
onsubtitlechange: (duration, subtitles, type, attributes) => {
duration // $ExpectType string
subtitles // $ExpectType string
type // $ExpectType string
attributes // $ExpectType AVPlaySubtitleAttribute[]
}
})
8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8073/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
avplay.setListener({
onsubtitlechange: (duration, subtitles, type, attributes)=>{
duration;
subtitles;
type;
attributes;
}
});
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-8xxx/8199/output/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jsx as _jsx } from "react/jsx-runtime";
export const IconSpecHotkey = (param)=>{
let { icon } = param;
return /*#__PURE__*/ _jsx("div", {
return _jsx("div", {
children: icon
});
};
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-8xxx/8210/output/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
const Component = ()=>{
return /*#__PURE__*/ _jsxDEV("p", {
thing: /*#__PURE__*/ _jsxDEV("a", {}, void 0, false, {
return _jsxDEV("p", {
thing: _jsxDEV("a", {}, void 0, false, {
fileName: "$DIR/tests/fixture/issues-8xxx/8210/input/1.js",
lineNumber: 2,
columnNumber: 23
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2581,7 +2581,7 @@ impl<I: Tokens> Parser<I> {
}

"type" => {
if next || is!(self, IdentRef) {
if next || (!self.input.had_line_break_before_cur() && is!(self, IdentRef)) {
if next {
bump!(self);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,59 @@
]
},
{
"type": "TsTypeAliasDeclaration",
"type": "ExpressionStatement",
"span": {
"start": 34,
"end": 52,
"end": 38,
"ctxt": 0
},
"declare": false,
"id": {
"expression": {
"type": "Identifier",
"span": {
"start": 39,
"end": 42,
"start": 34,
"end": 38,
"ctxt": 0
},
"value": "Foo",
"value": "type",
"optional": false
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 39,
"end": 52,
"ctxt": 0
},
"typeParams": null,
"typeAnnotation": {
"type": "TsKeywordType",
"expression": {
"type": "AssignmentExpression",
"span": {
"start": 45,
"start": 39,
"end": 51,
"ctxt": 0
},
"kind": "string"
"operator": "=",
"left": {
"type": "Identifier",
"span": {
"start": 39,
"end": 42,
"ctxt": 0
},
"value": "Foo",
"optional": false,
"typeAnnotation": null
},
"right": {
"type": "Identifier",
"span": {
"start": 45,
"end": 51,
"ctxt": 0
},
"value": "string",
"optional": false
}
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,32 +133,59 @@
},
"body": [
{
"type": "TsTypeAliasDeclaration",
"type": "ExpressionStatement",
"span": {
"start": 60,
"end": 82,
"end": 64,
"ctxt": 0
},
"declare": false,
"id": {
"expression": {
"type": "Identifier",
"span": {
"start": 69,
"end": 72,
"start": 60,
"end": 64,
"ctxt": 0
},
"value": "Foo",
"value": "type",
"optional": false
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 69,
"end": 82,
"ctxt": 0
},
"typeParams": null,
"typeAnnotation": {
"type": "TsKeywordType",
"expression": {
"type": "AssignmentExpression",
"span": {
"start": 75,
"start": 69,
"end": 81,
"ctxt": 0
},
"kind": "string"
"operator": "=",
"left": {
"type": "Identifier",
"span": {
"start": 69,
"end": 72,
"ctxt": 0
},
"value": "Foo",
"optional": false,
"typeAnnotation": null
},
"right": {
"type": "Identifier",
"span": {
"start": 75,
"end": 81,
"ctxt": 0
},
"value": "string",
"optional": false
}
}
}
]
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_ecma_parser/tests/typescript/issue-8073/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
avplay.setListener({
onsubtitlechange: (duration, subtitles, type, attributes) => {
duration // $ExpectType string
subtitles // $ExpectType string
type // $ExpectType string
attributes // $ExpectType AVPlaySubtitleAttribute[]
}
})
Loading

0 comments on commit e589126

Please sign in to comment.