Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix non-bmp characters in identifier validation #1139

Merged
merged 1 commit into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Fix escaping of non-BMP characters in property names ([#977](https://github.com/evanw/esbuild/issues/977))

Property names in object literals do not have to be quoted if the property is a valid JavaScript identifier. This is defined as starting with a character in the `ID_Start` Unicode category and ending with zero or more characters in the `ID_Continue` Unicode category. However, esbuild had a bug where non-BMP characters (i.e. characters encoded using two UTF-16 code units instead of one) were always checked against `ID_Continue` instead of `ID_Start` because they included a code unit that wasn't at the start. This could result in invalid JavaScript being generated when using `--charset=utf8` because `ID_Continue` is a superset of `ID_Start` and contains some characters that are not valid at the start of an identifier. This bug has been fixed.

## 0.11.8

* Fix hash calculation for code splitting and dynamic imports ([#1076](https://github.com/evanw/esbuild/issues/1076))
Expand Down
3 changes: 2 additions & 1 deletion internal/js_lexer/js_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,15 @@ func IsIdentifierUTF16(text []uint16) bool {
return false
}
for i := 0; i < n; i++ {
isStart := i == 0
r1 := rune(text[i])
if r1 >= 0xD800 && r1 <= 0xDBFF && i+1 < n {
if r2 := rune(text[i+1]); r2 >= 0xDC00 && r2 <= 0xDFFF {
r1 = (r1 << 10) + r2 + (0x10000 - (0xD800 << 10) - 0xDC00)
i++
}
}
if i == 0 {
if isStart {
if !IsIdentifierStart(r1) {
return false
}
Expand Down
6 changes: 6 additions & 0 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3386,6 +3386,12 @@ let transformTests = {
assert.strictEqual(code, `var x = "y";\nvar stdin_default = {x};\nexport {\n stdin_default as default,\n x\n};\n`)
},

async jsonInvalidIdentifierStart({ esbuild }) {
// This character is a valid "ID_Continue" but not a valid "ID_Start" so it must be quoted
const { code } = await esbuild.transform(`{ "\\uD835\\uDFCE": "y" }`, { loader: 'json' })
assert.strictEqual(code, `module.exports = {"\\u{1D7CE}": "y"};\n`)
},

async text({ esbuild }) {
const { code } = await esbuild.transform(`This is some text`, { loader: 'text' })
assert.strictEqual(code, `module.exports = "This is some text";\n`)
Expand Down