Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed May 1, 2024
1 parent 846dfe3 commit 8dd23f3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
23 changes: 20 additions & 3 deletions benchmark/babel-parser/real-case-ts/bench.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import baseline from "@babel-baseline/parser";
import current from "@babel/parser";
import { copyFileSync, readFileSync, rmSync } from "fs";
// eslint-disable-next-line import/no-extraneous-dependencies
import { commonJS } from "$repo-utils";
import { Benchmark } from "../../util.mjs";
import { readFileSync } from "fs";

const { require } = commonJS(import.meta.url);

const benchmark = new Benchmark();

Expand All @@ -27,10 +29,25 @@ function benchCases(name, implementation, options) {
});
}
}
copyFileSync(
require.resolve("@babel-baseline/parser"),
"./parser-baseline.cjs"
);
copyFileSync(
"../../../packages/babel-parser/lib/index.js",
"./parser-current.cjs"
);

const baseline = require("./parser-baseline.cjs");
const current = require("./parser-current.cjs");

benchCases("current", current.parse);
benchCases("baseline", baseline.parse);
benchCases("current", current.parse);
benchCases("baseline", baseline.parse);

benchmark.suite.on("complete", () => {
rmSync("./parser-current.cjs");
rmSync("./parser-baseline.cjs");
});
benchmark.run();
5 changes: 5 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3694,6 +3694,9 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
binding: BindingFlag,
) {
switch (type) {
// Allow "typecasts" to appear on the left of assignment expressions,
// because it may be in an arrow function.
// e.g. `const f = (foo: number = 0) => foo;`
case "TSTypeCastExpression":
return true;
case "TSParameterProperty":
Expand All @@ -3707,6 +3710,8 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
(binding !== BindingFlag.TYPE_NONE ||
!isUnparenthesizedInAssign) && ["expression", true]
);
default:
return super.isValidLVal(type, isUnparenthesizedInAssign, binding);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(a = b) += 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"type": "File",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":13,"index":13}},
"errors": [
"SyntaxError: Invalid left-hand side in assignment expression. (1:1)"
],
"program": {
"type": "Program",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":13,"index":13}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":13,"index":13}},
"expression": {
"type": "AssignmentExpression",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":12,"index":12}},
"operator": "+=",
"left": {
"type": "AssignmentExpression",
"start":1,"end":6,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":6,"index":6}},
"operator": "=",
"left": {
"type": "Identifier",
"start":1,"end":2,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":2,"index":2},"identifierName":"a"},
"name": "a"
},
"right": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"b"},
"name": "b"
},
"extra": {
"parenthesized": true,
"parenStart": 0
}
},
"right": {
"type": "NumericLiteral",
"start":11,"end":12,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":12,"index":12}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
}
],
"directives": []
}
}

0 comments on commit 8dd23f3

Please sign in to comment.