forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringChecks.ts
More file actions
44 lines (39 loc) · 1.32 KB
/
StringChecks.ts
File metadata and controls
44 lines (39 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as ts from 'typescript';
/**
* Helpers for validating various text string formats.
*/
export class StringChecks {
/**
* Tests whether the input string is safe to use as an ECMAScript identifier without quotes.
*
* @remarks
* For example:
*
* ```ts
* class X {
* public okay: number = 1;
* public "not okay!": number = 2;
* }
* ```
*
* A precise check is extremely complicated and highly dependent on the ECMAScript standard version
* and how faithfully the interpreter implements it. To keep things simple, `isSafeUnquotedMemberIdentifier()`
* conservatively accepts any identifier that would be valid with ECMAScript 5, and returns false otherwise.
*/
public static isSafeUnquotedMemberIdentifier(identifier: string): boolean {
if (identifier.length === 0) {
return false; // cannot be empty
}
if (!ts.isIdentifierStart(identifier.charCodeAt(0), ts.ScriptTarget.ES5)) {
return false;
}
for (let i: number = 1; i < identifier.length; i++) {
if (!ts.isIdentifierPart(identifier.charCodeAt(i), ts.ScriptTarget.ES5)) {
return false;
}
}
return true;
}
}