ts-const-value-transformer 0.9.0 → 0.10.0

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.
@@ -0,0 +1,226 @@
1
+ import * as tsNamespace from 'typescript';
2
+ import { nodeToTypeString, nodeToTypeFlags, isExpressionReadonly, isExternalReference, isUndefinedIdentifier, isEnumAccess, } from './lsp/lspTransformerUtil.mjs';
3
+ import { printSourceWithMapWithProxy, printSourceWithProxy, transformAndPrintSourceWithMapWithProxy, transformAndPrintSourceWithProxy, transformSourceWithProxy, } from './transform.mjs';
4
+ function makeLspProxy(tsInstance, lspClient, getSourceFile) {
5
+ return {
6
+ visitEachChild(node, visitor, context) {
7
+ return tsInstance.visitEachChild(node, visitor, context);
8
+ },
9
+ getNodeText(node, sourceFile) {
10
+ return node.getText(sourceFile);
11
+ },
12
+ getNodeFullText(node, sourceFile) {
13
+ return node.getFullText(sourceFile);
14
+ },
15
+ appendMultiLineComment(node, comment) {
16
+ return tsInstance.addSyntheticTrailingComment(node, tsInstance.SyntaxKind.MultiLineCommentTrivia, comment);
17
+ },
18
+ setTextRange(range, location) {
19
+ return tsInstance.setTextRange(range, location);
20
+ },
21
+ isExpression(node) {
22
+ return tsInstance.isExpression(node);
23
+ },
24
+ isAsExpression(node) {
25
+ return tsInstance.isAsExpression(node);
26
+ },
27
+ isCallLikeExpression(node) {
28
+ return tsInstance.isCallLikeExpression(node);
29
+ },
30
+ isTemplateExpression(node) {
31
+ return tsInstance.isTemplateExpression(node);
32
+ },
33
+ isPropertyAccessExpression(node) {
34
+ return tsInstance.isPropertyAccessExpression(node);
35
+ },
36
+ isElementAccessExpression(node) {
37
+ return tsInstance.isElementAccessExpression(node);
38
+ },
39
+ isInterfaceDeclaration(node) {
40
+ return tsInstance.isInterfaceDeclaration(node);
41
+ },
42
+ isTypeAliasDeclaration(node) {
43
+ return tsInstance.isTypeAliasDeclaration(node);
44
+ },
45
+ isImportDeclaration(node) {
46
+ return tsInstance.isImportDeclaration(node);
47
+ },
48
+ isTypeOnlyExportDeclaration(node) {
49
+ return tsInstance.isTypeOnlyExportDeclaration(node);
50
+ },
51
+ isIdentifier(node) {
52
+ return tsInstance.isIdentifier(node);
53
+ },
54
+ isComputedPropertyName(node) {
55
+ return tsInstance.isComputedPropertyName(node);
56
+ },
57
+ getTypeAtLocation(node, sourceFile) {
58
+ if (!lspClient) {
59
+ return undefined;
60
+ }
61
+ const typeString = nodeToTypeString(node, sourceFile, tsInstance, lspClient);
62
+ const flags = nodeToTypeFlags(node, sourceFile, tsInstance, lspClient);
63
+ let value = typeString;
64
+ if ((flags & tsInstance.TypeFlags.NumberLiteral) !== 0) {
65
+ value = Number(typeString);
66
+ }
67
+ else if ((flags & tsInstance.TypeFlags.StringLiteral) !== 0) {
68
+ try {
69
+ value = JSON.parse(typeString);
70
+ }
71
+ catch { }
72
+ }
73
+ const t = {
74
+ flags,
75
+ typeString,
76
+ value,
77
+ };
78
+ return t;
79
+ },
80
+ isEnumLiteral(type) {
81
+ return ((type.flags & tsInstance.TypeFlags.EnumLiteral) !== 0);
82
+ },
83
+ isStringLiteral(type) {
84
+ return ((type.flags & tsInstance.TypeFlags.StringLiteral) !== 0);
85
+ },
86
+ isNumberLiteral(type) {
87
+ return ((type.flags & tsInstance.TypeFlags.NumberLiteral) !== 0);
88
+ },
89
+ isBigIntLiteral(type) {
90
+ return ((type.flags & tsInstance.TypeFlags.BigIntLiteral) !== 0);
91
+ },
92
+ isBooleanLiteral(type) {
93
+ return ((type.flags & tsInstance.TypeFlags.BooleanLiteral) !== 0);
94
+ },
95
+ isNullType(type) {
96
+ return (type.flags & tsInstance.TypeFlags.Null) !== 0;
97
+ },
98
+ isUndefinedType(type) {
99
+ return (type.flags & tsInstance.TypeFlags.Undefined) !== 0;
100
+ },
101
+ typeToString(type) {
102
+ return type.typeString;
103
+ },
104
+ factory: {
105
+ createIdentifier(text) {
106
+ return tsInstance.factory.createIdentifier(text);
107
+ },
108
+ createStringLiteral(value) {
109
+ return tsInstance.factory.createStringLiteral(value);
110
+ },
111
+ createNumericLiteral(value) {
112
+ return tsInstance.factory.createNumericLiteral(value);
113
+ },
114
+ createExpressionWithMinusToken(operand) {
115
+ return tsInstance.factory.createParenthesizedExpression(tsInstance.factory.createPrefixUnaryExpression(tsInstance.SyntaxKind.MinusToken, operand));
116
+ },
117
+ createBigIntLiteral(value) {
118
+ return tsInstance.factory.createBigIntLiteral(value);
119
+ },
120
+ createTrue() {
121
+ return tsInstance.factory.createTrue();
122
+ },
123
+ createFalse() {
124
+ return tsInstance.factory.createFalse();
125
+ },
126
+ createNull() {
127
+ return tsInstance.factory.createNull();
128
+ },
129
+ createParenthesizedExpression(expression) {
130
+ return tsInstance.factory.createParenthesizedExpression(expression);
131
+ },
132
+ createVoidZero() {
133
+ return tsInstance.factory.createParenthesizedExpression(tsInstance.factory.createVoidZero());
134
+ },
135
+ },
136
+ isEnumAccess(node, sourceFile) {
137
+ if (!lspClient) {
138
+ return false;
139
+ }
140
+ return isEnumAccess(node, sourceFile, tsInstance, lspClient);
141
+ },
142
+ isEnumIdentifier(node, sourceFile) {
143
+ if (!lspClient) {
144
+ return false;
145
+ }
146
+ const typeFlags = nodeToTypeFlags(node, sourceFile, tsInstance, lspClient);
147
+ return (typeFlags & tsInstance.TypeFlags.EnumLiteral) !== 0;
148
+ },
149
+ isExternalReference(node, externalNames, sourceFile) {
150
+ if (!lspClient) {
151
+ return false;
152
+ }
153
+ return isExternalReference(node, sourceFile, tsInstance, lspClient, externalNames);
154
+ },
155
+ hasPureAnnotation(node, sourceFile) {
156
+ const fullText = node.getFullText(sourceFile);
157
+ const ranges = tsInstance.getLeadingCommentRanges(fullText, 0) ?? [];
158
+ for (const range of ranges) {
159
+ if (range.kind !== tsInstance.SyntaxKind.MultiLineCommentTrivia) {
160
+ continue;
161
+ }
162
+ const text = fullText.slice(range.pos + 2, range.end - 2).trim();
163
+ if ((text[0] === '@' || text[0] === '#') &&
164
+ text.slice(1) === '__PURE__') {
165
+ return true;
166
+ }
167
+ }
168
+ return false;
169
+ },
170
+ isReadonlyExpression(node, sourceFile) {
171
+ if (!lspClient) {
172
+ return null;
173
+ }
174
+ return isExpressionReadonly(node, sourceFile, tsInstance, lspClient, getSourceFile);
175
+ },
176
+ isHoistablePropertyAccess(_node, sourceFile) {
177
+ if (!lspClient) {
178
+ return false;
179
+ }
180
+ const node = _node;
181
+ const typeFlags = nodeToTypeFlags(node, sourceFile, tsInstance, lspClient);
182
+ return typeFlags !== 0;
183
+ },
184
+ isUndefinedIdentifier(node, parent, sourceFile) {
185
+ if (!lspClient) {
186
+ return false;
187
+ }
188
+ return isUndefinedIdentifier(node, parent, sourceFile, tsInstance, lspClient);
189
+ },
190
+ makeStringLiteralSource(value) {
191
+ // TypeScript namespace may export `function escapeNonAsciiString(s: string, quoteChar?: CharacterCodes.doubleQuote | CharacterCodes.singleQuote | CharacterCodes.backtick): string`
192
+ return 'escapeNonAsciiString' in tsInstance
193
+ ? `"${tsInstance.escapeNonAsciiString(value, 'CharacterCodes' in tsInstance
194
+ ? tsInstance.CharacterCodes
195
+ .doubleQuote
196
+ : 34 /* doubleQuote */)}"`
197
+ : JSON.stringify(value);
198
+ },
199
+ getLineStarts(sourceFile) {
200
+ return sourceFile.getLineStarts();
201
+ },
202
+ };
203
+ }
204
+ export function printSource(sourceFile, tsInstance) {
205
+ const _ts = tsInstance ?? tsNamespace;
206
+ return printSourceWithProxy(sourceFile, makeLspProxy(_ts, null, null));
207
+ }
208
+ export function printSourceWithMap(sourceFile, originalSourceName, startOfSourceMap, tsInstance) {
209
+ const _ts = tsInstance ?? tsNamespace;
210
+ return printSourceWithMapWithProxy(sourceFile, originalSourceName, makeLspProxy(_ts, null, null), startOfSourceMap);
211
+ }
212
+ export function transformSource(sourceFile, lspClient, getSourceFile, options) {
213
+ const ts = options?.ts ?? tsNamespace;
214
+ const proxy = makeLspProxy(ts, lspClient, getSourceFile);
215
+ return transformSourceWithProxy(sourceFile, proxy, undefined, options);
216
+ }
217
+ export function transformAndPrintSource(sourceFile, lspClient, getSourceFile, options) {
218
+ const ts = options?.ts ?? tsNamespace;
219
+ const proxy = makeLspProxy(ts, lspClient, getSourceFile);
220
+ return transformAndPrintSourceWithProxy(sourceFile, proxy, undefined, options);
221
+ }
222
+ export function transformAndPrintSourceWithMap(sourceFile, lspClient, getSourceFile, originalSourceName, options, startOfSourceMap) {
223
+ const ts = options?.ts ?? tsNamespace;
224
+ const proxy = makeLspProxy(ts, lspClient, getSourceFile);
225
+ return transformAndPrintSourceWithMapWithProxy(sourceFile, proxy, undefined, originalSourceName, options, startOfSourceMap);
226
+ }
@@ -1,5 +1,6 @@
1
1
  import * as sourceMap from 'source-map';
2
2
  import type * as ts from 'typescript';
3
+ import type { ApiProxy, ProxyTypes } from './TsProxy.mjs';
3
4
  export interface TransformOptions {
4
5
  /** `typescript` namespace object */
5
6
  ts?: typeof ts;
@@ -21,6 +22,8 @@ export interface TransformOptions {
21
22
  useUndefinedSymbolForUndefinedValue?: boolean | undefined;
22
23
  /** Hoist `undefined` symbol to `void 0` (or `undefined` if {@linkcode useUndefinedSymbolForUndefinedValue} is true). Default is true. */
23
24
  hoistUndefinedSymbol?: boolean | undefined;
25
+ /** Hoist template literals with constant (not including variables). Default is false because it is not necessary for ES2015 or later; Script minifiers would optimize for those values. For ES5 (which will be deprecated in the future), TypeScript converts template literals to such as `''.concat(...)`, which is not treated as constant values. */
26
+ hoistConstTemplateLiteral?: boolean | undefined;
24
27
  /**
25
28
  * External names (tested with `.includes()` for string, with `.test()` for RegExp) for `hoistExternalValues` settings (If `hoistExternalValues` is not specified, this setting will be used).
26
29
  * - Path separators for input file name are always normalized to '/' internally.
@@ -36,8 +39,8 @@ export interface TransformOptions {
36
39
  ignoreFiles?: ReadonlyArray<string | RegExp> | ((fileName: string) => boolean);
37
40
  }
38
41
  export declare function getIgnoreFilesFunction(ignoreFiles: TransformOptions['ignoreFiles']): (fileName: string) => boolean;
39
- export declare function transformSource(sourceFile: ts.SourceFile, program: ts.Program, context: ts.TransformationContext | undefined, options?: TransformOptions): ts.SourceFile;
40
- export declare function printSource(sourceFile: ts.SourceFile, tsInstance?: typeof ts): string;
41
- export declare function printSourceWithMap(sourceFile: ts.SourceFile, originalSourceName: string, startOfSourceMap?: sourceMap.RawSourceMap, tsInstance?: typeof ts): [string, sourceMap.RawSourceMap];
42
- export declare function transformAndPrintSource(sourceFile: ts.SourceFile, program: ts.Program, context: ts.TransformationContext | undefined, options?: TransformOptions): string;
43
- export declare function transformAndPrintSourceWithMap(sourceFile: ts.SourceFile, program: ts.Program, context: ts.TransformationContext | undefined, originalSourceName: string, options?: TransformOptions, startOfSourceMap?: sourceMap.RawSourceMap): [string, sourceMap.RawSourceMap];
42
+ export declare function transformSourceWithProxy<TTransformationContext = void>(sourceFile: ProxyTypes.SourceFile, proxy: ApiProxy<TTransformationContext>, context: TTransformationContext | undefined, options?: TransformOptions): ProxyTypes.SourceFile;
43
+ export declare function printSourceWithProxy<TTransformationContext = void>(sourceFile: ProxyTypes.SourceFile, proxy: ApiProxy<TTransformationContext>): string;
44
+ export declare function printSourceWithMapWithProxy<TTransformationContext = void>(sourceFile: ProxyTypes.SourceFile, originalSourceName: string, proxy: ApiProxy<TTransformationContext>, startOfSourceMap?: sourceMap.RawSourceMap): [string, sourceMap.RawSourceMap];
45
+ export declare function transformAndPrintSourceWithProxy<TTransformationContext = void>(sourceFile: ProxyTypes.SourceFile, proxy: ApiProxy<TTransformationContext>, context: TTransformationContext | undefined, options?: TransformOptions): string;
46
+ export declare function transformAndPrintSourceWithMapWithProxy<TTransformationContext = void>(sourceFile: ProxyTypes.SourceFile, proxy: ApiProxy<TTransformationContext>, context: TTransformationContext | undefined, originalSourceName: string, options?: TransformOptions, startOfSourceMap?: sourceMap.RawSourceMap): [string, sourceMap.RawSourceMap];