@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/CHANGELOG.md +26 -0
- package/dist/lex-builder.d.ts +8 -0
- package/dist/lex-builder.d.ts.map +1 -1
- package/dist/lex-builder.js +14 -3
- package/dist/lex-builder.js.map +1 -1
- package/dist/lex-def-builder.d.ts.map +1 -1
- package/dist/lex-def-builder.js +4 -10
- package/dist/lex-def-builder.js.map +1 -1
- package/dist/ref-resolver.d.ts.map +1 -1
- package/dist/ref-resolver.js +45 -18
- package/dist/ref-resolver.js.map +1 -1
- package/dist/ts-lang.d.ts +5 -2
- package/dist/ts-lang.d.ts.map +1 -1
- package/dist/ts-lang.js +58 -46
- package/dist/ts-lang.js.map +1 -1
- package/dist/util.d.ts +1 -0
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +5 -0
- package/dist/util.js.map +1 -1
- package/package.json +3 -3
- package/src/lex-builder.ts +28 -4
- package/src/lex-def-builder.ts +6 -11
- package/src/ref-resolver.ts +79 -20
- package/src/ts-lang.ts +60 -44
- package/src/util.ts +5 -0
package/src/ts-lang.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
'
|
|
87
|
-
|
|
88
|
-
'
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
125
|
-
|
|
126
|
-
'
|
|
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
|
-
|
|
129
|
-
|
|
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
|
|
133
|
-
return
|
|
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
|
}
|