@atproto/lex-builder 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ts-lang.ts CHANGED
@@ -1,6 +1,8 @@
1
- const RESERVED_WORDS = new Set([
2
- // JavaScript keywords
3
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar
1
+ /**
2
+ * JavaScript keywords
3
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar}
4
+ */
5
+ const JS_KEYWORDS = new Set([
4
6
  'abstract',
5
7
  'arguments',
6
8
  'as',
@@ -73,62 +75,76 @@ const RESERVED_WORDS = new Set([
73
75
  'while',
74
76
  'with',
75
77
  'yield',
76
- // Constructors and globals
77
- 'Array',
78
- 'Boolean',
79
- 'Buffer',
80
- 'Date',
81
- 'Error',
82
- 'Function',
83
- 'Infinity',
84
- 'JSON',
85
- 'Map',
86
- 'Math',
87
- 'NaN',
88
- 'Number',
89
- 'Object',
90
- 'Set',
91
- 'String',
92
- 'Symbol',
93
- 'console',
94
- 'document',
95
- 'global',
78
+ ])
79
+
80
+ export function isJsKeyword(word: string) {
81
+ return JS_KEYWORDS.has(word)
82
+ }
83
+
84
+ // Only important to list var/type names that are likely to be used in the
85
+ // generated code files.
86
+ const GLOBAL_IDENTIFIERS = new Set([
87
+ // import { l } from "@atproto/lex-schema"
88
+ 'l',
89
+ // JS Globals
90
+ 'self',
96
91
  'globalThis',
97
- 'window',
98
- // Test globals
99
- 'afterAll',
100
- 'afterEach',
101
- 'assert',
102
- 'beforeAll',
103
- 'beforeEach',
104
- 'describe',
105
- 'expect',
106
- 'it',
107
- 'test',
108
- // CommonJS globals
92
+ // ESM
93
+ 'import',
94
+ // CommonJS
109
95
  '__dirname',
110
96
  '__filename',
111
97
  'require',
112
98
  'module',
113
99
  'exports',
114
- // TypeScript
115
- 'Record',
100
+ // TS Primitives
116
101
  'any',
102
+ 'bigint',
103
+ 'boolean',
117
104
  'declare',
118
105
  'never',
106
+ 'null',
119
107
  'number',
120
108
  'object',
121
109
  'string',
122
110
  'symbol',
111
+ 'undefined',
123
112
  'unknown',
124
- // Future reserved words
125
- 'constructor',
126
- 'meta',
113
+ 'void',
114
+ // TS Utility types
115
+ 'Record',
116
+ 'Partial',
117
+ 'Readonly',
118
+ 'Pick',
119
+ 'Omit',
120
+ 'Exclude',
121
+ 'Extract',
122
+ 'InstanceType',
123
+ 'ReturnType',
124
+ 'Required',
125
+ 'ThisType',
126
+ 'Uppercase',
127
+ 'Lowercase',
128
+ 'Capitalize',
129
+ 'Uncapitalize',
127
130
  ])
128
- export function isReservedWord(word: string) {
129
- return RESERVED_WORDS.has(word)
131
+
132
+ export function isGlobalIdentifier(word: string) {
133
+ return GLOBAL_IDENTIFIERS.has(word)
134
+ }
135
+
136
+ export function isSafeLocalIdentifier(name: string) {
137
+ return !isGlobalIdentifier(name) && isValidJsIdentifier(name)
138
+ }
139
+
140
+ export function isValidJsIdentifier(name: string) {
141
+ return (
142
+ name.length > 0 &&
143
+ !isJsKeyword(name) &&
144
+ /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)
145
+ )
130
146
  }
131
147
 
132
- export function isSafeIdentifier(name: string) {
133
- return !isReservedWord(name) && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)
148
+ export function asNamespaceExport(name: string) {
149
+ return isValidJsIdentifier(name) ? name : JSON.stringify(name)
134
150
  }
package/src/util.ts CHANGED
@@ -13,6 +13,11 @@ export function memoize<T extends (arg: string) => NonNullable<unknown> | null>(
13
13
  }) as T
14
14
  }
15
15
 
16
+ export function startsWithLower(str: string) {
17
+ const code = str.charCodeAt(0)
18
+ return code >= 97 && code <= 122 // 'a' to 'z'
19
+ }
20
+
16
21
  export function ucFirst(str: string) {
17
22
  return str.charAt(0).toUpperCase() + str.slice(1)
18
23
  }