fix: isIdentifierName should reject empty string (#11339)

This commit is contained in:
Huáng Jùnliàng 2020-03-26 16:54:50 -04:00 committed by GitHub
parent a901bd50b1
commit 6a00cbe0ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -98,5 +98,5 @@ export function isIdentifierName(name: string): boolean {
return false;
}
}
return true;
return !isFirst;
}

View File

@ -0,0 +1,22 @@
import { isIdentifierName } from "..";
describe("isIdentifierName", function() {
it("returns false if provided string is empty", function() {
expect(isIdentifierName("")).toBe(false);
});
it.each(["hello", "$", "ゆゆ式", "$20", "hello20", "_", "if"])(
"returns true if provided string %p is an IdentifierName",
function(word) {
expect(isIdentifierName(word)).toBe(true);
},
);
it.each(["+hello", "0$", "-ゆゆ式", "#_", "_#"])(
"returns false if provided string %p is not an IdentifierName",
function(word) {
expect(isIdentifierName(word)).toBe(false);
},
);
it("supports astral symbols", function() {
expect(isIdentifierName("x\uDB40\uDDD5")).toBe(true);
});
});