From 6a00cbe0ab59924b51f07e9a5d2f37dae15746ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 26 Mar 2020 16:54:50 -0400 Subject: [PATCH] fix: isIdentifierName should reject empty string (#11339) --- .../src/identifier.js | 2 +- .../test/identifier.spec.js | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 packages/babel-helper-validator-identifier/test/identifier.spec.js diff --git a/packages/babel-helper-validator-identifier/src/identifier.js b/packages/babel-helper-validator-identifier/src/identifier.js index db0f0abb7d73..5088281bbf8e 100644 --- a/packages/babel-helper-validator-identifier/src/identifier.js +++ b/packages/babel-helper-validator-identifier/src/identifier.js @@ -98,5 +98,5 @@ export function isIdentifierName(name: string): boolean { return false; } } - return true; + return !isFirst; } diff --git a/packages/babel-helper-validator-identifier/test/identifier.spec.js b/packages/babel-helper-validator-identifier/test/identifier.spec.js new file mode 100644 index 000000000000..60a25cb57851 --- /dev/null +++ b/packages/babel-helper-validator-identifier/test/identifier.spec.js @@ -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); + }); +});