UNPKG

3.77 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3// See LICENSE in the project root for license information.
4var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5 if (k2 === undefined) k2 = k;
6 var desc = Object.getOwnPropertyDescriptor(m, k);
7 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8 desc = { enumerable: true, get: function() { return m[k]; } };
9 }
10 Object.defineProperty(o, k2, desc);
11}) : (function(o, m, k, k2) {
12 if (k2 === undefined) k2 = k;
13 o[k2] = m[k];
14}));
15var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16 Object.defineProperty(o, "default", { enumerable: true, value: v });
17}) : function(o, v) {
18 o["default"] = v;
19});
20var __importStar = (this && this.__importStar) || function (mod) {
21 if (mod && mod.__esModule) return mod;
22 var result = {};
23 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24 __setModuleDefault(result, mod);
25 return result;
26};
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.SyntaxHelpers = void 0;
29const ts = __importStar(require("typescript"));
30/**
31 * Helpers for validating various text string formats.
32 */
33class SyntaxHelpers {
34 /**
35 * Tests whether the input string is safe to use as an ECMAScript identifier without quotes.
36 *
37 * @remarks
38 * For example:
39 *
40 * ```ts
41 * class X {
42 * public okay: number = 1;
43 * public "not okay!": number = 2;
44 * }
45 * ```
46 *
47 * A precise check is extremely complicated and highly dependent on the ECMAScript standard version
48 * and how faithfully the interpreter implements it. To keep things simple, `isSafeUnquotedMemberIdentifier()`
49 * conservatively accepts any identifier that would be valid with ECMAScript 5, and returns false otherwise.
50 */
51 static isSafeUnquotedMemberIdentifier(identifier) {
52 if (identifier.length === 0) {
53 return false; // cannot be empty
54 }
55 if (!ts.isIdentifierStart(identifier.charCodeAt(0), ts.ScriptTarget.ES5)) {
56 return false;
57 }
58 for (let i = 1; i < identifier.length; i++) {
59 if (!ts.isIdentifierPart(identifier.charCodeAt(i), ts.ScriptTarget.ES5)) {
60 return false;
61 }
62 }
63 return true;
64 }
65 /**
66 * Given an arbitrary input string, return a regular TypeScript identifier name.
67 *
68 * @remarks
69 * Example input: "api-extractor-lib1-test"
70 * Example output: "apiExtractorLib1Test"
71 */
72 static makeCamelCaseIdentifier(input) {
73 const parts = input.split(/\W+/).filter((x) => x.length > 0);
74 if (parts.length === 0) {
75 return '_';
76 }
77 for (let i = 0; i < parts.length; ++i) {
78 let part = parts[i];
79 if (part.toUpperCase() === part) {
80 // Preserve existing case unless the part is all upper-case
81 part = part.toLowerCase();
82 }
83 if (i === 0) {
84 // If the first part starts with a number, prepend "_"
85 if (/[0-9]/.test(part.charAt(0))) {
86 part = '_' + part;
87 }
88 }
89 else {
90 // Capitalize the first letter of each part, except for the first one
91 part = part.charAt(0).toUpperCase() + part.slice(1);
92 }
93 parts[i] = part;
94 }
95 return parts.join('');
96 }
97}
98exports.SyntaxHelpers = SyntaxHelpers;
99//# sourceMappingURL=SyntaxHelpers.js.map
\No newline at end of file