fix(45114): throw an error when using '#' as an identifier (#45124)

This commit is contained in:
Oleksandr T 2021-07-26 21:39:17 +03:00 committed by GitHub
parent ba226167bc
commit 11c7daef62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 111 additions and 2 deletions

View file

@ -2047,8 +2047,15 @@ namespace ts {
pos++;
return token = SyntaxKind.Unknown;
}
pos++;
scanIdentifier(codePointAt(text, pos), languageVersion);
if (isIdentifierStart(codePointAt(text, pos + 1), languageVersion)) {
pos++;
scanIdentifier(codePointAt(text, pos), languageVersion);
}
else {
tokenValue = String.fromCharCode(codePointAt(text, pos));
error(Diagnostics.Invalid_character, pos++, charSize(ch));
}
return token = SyntaxKind.PrivateIdentifier;
default:
const identifierKind = scanIdentifier(ch, languageVersion);

View file

@ -0,0 +1,25 @@
tests/cases/conformance/classes/members/privateNames/privateNameHashCharName.ts(1,1): error TS1127: Invalid character.
tests/cases/conformance/classes/members/privateNames/privateNameHashCharName.ts(1,1): error TS2304: Cannot find name '#'.
tests/cases/conformance/classes/members/privateNames/privateNameHashCharName.ts(4,5): error TS1127: Invalid character.
tests/cases/conformance/classes/members/privateNames/privateNameHashCharName.ts(7,14): error TS1127: Invalid character.
==== tests/cases/conformance/classes/members/privateNames/privateNameHashCharName.ts (4 errors) ====
#
~
!!! error TS1127: Invalid character.
~
!!! error TS2304: Cannot find name '#'.
class C {
#
~
!!! error TS1127: Invalid character.
m() {
this.#
~
!!! error TS1127: Invalid character.
}
}

View file

@ -0,0 +1,29 @@
//// [privateNameHashCharName.ts]
#
class C {
#
m() {
this.#
}
}
//// [privateNameHashCharName.js]
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _C_;
#;
class C {
constructor() {
_C_.set(this, void 0);
}
m() {
__classPrivateFieldGet(this, _C_, "f");
}
}
_C_ = new WeakMap();

View file

@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/members/privateNames/privateNameHashCharName.ts ===
#
class C {
>C : Symbol(C, Decl(privateNameHashCharName.ts, 0, 1))
#
># : Symbol(C[#], Decl(privateNameHashCharName.ts, 2, 9))
m() {
>m : Symbol(C.m, Decl(privateNameHashCharName.ts, 3, 5))
this.#
>this.# : Symbol(C[#], Decl(privateNameHashCharName.ts, 2, 9))
>this : Symbol(C, Decl(privateNameHashCharName.ts, 0, 1))
}
}

View file

@ -0,0 +1,19 @@
=== tests/cases/conformance/classes/members/privateNames/privateNameHashCharName.ts ===
#
># : any
class C {
>C : C
#
># : any
m() {
>m : () => void
this.#
>this.# : any
>this : this
}
}

View file

@ -0,0 +1,11 @@
// @target: es6
#
class C {
#
m() {
this.#
}
}