Skip to content

Commit

Permalink
fix(es/parser): Correctly parse the keyword (#8483)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8482.
  • Loading branch information
magic-akari authored Jan 4, 2024
1 parent 4e860c9 commit 740e6f3
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 7 deletions.
24 changes: 24 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8482/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"externalHelpers": true,
"minify": {
"compress": false,
"mangle": false
},
"keepClassNames": true,
"loose": true
},
"minify": false,
"isModule": true,
"module": {
"type": "umd"
},
"env": {
"targets": "",
"bugfixes": true
}
}
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-8xxx/8482/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export let a = ''
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8482/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.index = {});
})(this, function(exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "a", {
enumerable: true,
get: function() {
return a;
}
});
let a = "";
});
6 changes: 5 additions & 1 deletion crates/swc_ecma_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,12 +824,16 @@ impl<'a> Lexer<'a> {
// Optimization
{
let s = l.input.uncons_while(|c| {
if !c.is_ident_part() {
return false;
}

// Performance optimization
if c.is_ascii_uppercase() || c.is_ascii_digit() || !c.is_ascii() {
can_be_keyword = false;
}

c.is_ident_part()
true
});
if !s.is_empty() {
first = false;
Expand Down
7 changes: 1 addition & 6 deletions crates/swc_ecma_parser/src/lexer/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,7 @@ const L_I: ByteHandler = Some(|lexer| {
})
});

const L_J: ByteHandler = Some(|lexer| {
lexer.read_word_with(|s| match s {
"let" => Some(Word::Keyword(Keyword::Let)),
_ => None,
})
});
const L_J: ByteHandler = IDN;

const L_K: ByteHandler = Some(|lexer| {
lexer.read_word_with(|s| match s {
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/tests/js/issue-8482/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export let a = ''
61 changes: 61 additions & 0 deletions crates/swc_ecma_parser/tests/js/issue-8482/input.js.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"type": "Module",
"span": {
"start": 1,
"end": 19,
"ctxt": 0
},
"body": [
{
"type": "ExportDeclaration",
"span": {
"start": 1,
"end": 19,
"ctxt": 0
},
"declaration": {
"type": "VariableDeclaration",
"span": {
"start": 8,
"end": 19,
"ctxt": 0
},
"kind": "let",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 13,
"end": 19,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 13,
"end": 14,
"ctxt": 0
},
"value": "a",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "StringLiteral",
"span": {
"start": 17,
"end": 19,
"ctxt": 0
},
"value": "",
"raw": "''"
},
"definite": false
}
]
}
}
],
"interpreter": null
}

0 comments on commit 740e6f3

Please sign in to comment.