ts-const-value-transformer 0.2.0 → 0.3.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.
- package/CHANGELOG.md +8 -0
- package/README.md +6 -0
- package/dist/transform.d.mts +6 -0
- package/dist/transform.mjs +26 -5
- package/dist/version.d.mts +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -160,6 +160,12 @@ export interface TransformOptions {
|
|
|
160
160
|
hoistPureFunctionCall?: boolean | undefined;
|
|
161
161
|
/** Hoist expressions with `as XXX`. Default is false because the base (non-`as`) value may be non-constant. */
|
|
162
162
|
unsafeHoistAsExpresion?: boolean | undefined;
|
|
163
|
+
/**
|
|
164
|
+
* External names (tested with `.includes()` for string, with `.test()` for RegExp) for `hoistExternalValues` settings (If `hoistExternalValues` is not specified, this setting will be used).
|
|
165
|
+
* - Path separators for input file name are always normalized to '/' internally.
|
|
166
|
+
* - Default is `['/node_modules/']`.
|
|
167
|
+
*/
|
|
168
|
+
externalNames?: ReadonlyArray<string | RegExp> | undefined;
|
|
163
169
|
}
|
|
164
170
|
```
|
|
165
171
|
|
package/dist/transform.d.mts
CHANGED
|
@@ -15,6 +15,12 @@ export interface TransformOptions {
|
|
|
15
15
|
hoistPureFunctionCall?: boolean | undefined;
|
|
16
16
|
/** Hoist expressions with `as XXX`. Default is false because the base (non-`as`) value may be non-constant. */
|
|
17
17
|
unsafeHoistAsExpresion?: boolean | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* External names (tested with `.includes()` for string, with `.test()` for RegExp) for `hoistExternalValues` settings (If `hoistExternalValues` is not specified, this setting will be used).
|
|
20
|
+
* - Path separators for input file name are always normalized to '/' internally.
|
|
21
|
+
* - Default is `['/node_modules/']`.
|
|
22
|
+
*/
|
|
23
|
+
externalNames?: ReadonlyArray<string | RegExp> | undefined;
|
|
18
24
|
}
|
|
19
25
|
export declare function transformSource(sourceFile: ts.SourceFile, program: ts.Program, context: ts.TransformationContext, options?: TransformOptions): ts.SourceFile;
|
|
20
26
|
export declare function printSource(sourceFile: ts.SourceFile): string;
|
package/dist/transform.mjs
CHANGED
|
@@ -11,6 +11,7 @@ function assignDefaultValues(options = {}) {
|
|
|
11
11
|
unsafeHoistAsExpresion: options.unsafeHoistAsExpresion ?? false,
|
|
12
12
|
hoistPureFunctionCall: options.hoistPureFunctionCall ?? false,
|
|
13
13
|
unsafeHoistFunctionCall: options.unsafeHoistFunctionCall ?? false,
|
|
14
|
+
externalNames: options.externalNames ?? [],
|
|
14
15
|
};
|
|
15
16
|
}
|
|
16
17
|
////////////////////////////////////////////////////////////////////////////////
|
|
@@ -55,7 +56,8 @@ function visitNodeAndReplaceIfNeeded(node, sourceFile, program, context, options
|
|
|
55
56
|
return node;
|
|
56
57
|
}
|
|
57
58
|
}
|
|
58
|
-
if (!options.hoistExternalValues &&
|
|
59
|
+
if (!options.hoistExternalValues &&
|
|
60
|
+
isExternalReference(node, program, options.externalNames)) {
|
|
59
61
|
return node;
|
|
60
62
|
}
|
|
61
63
|
if (ts.isIdentifier(node)) {
|
|
@@ -132,13 +134,28 @@ function isEnumIdentifier(node, program, tsInstance) {
|
|
|
132
134
|
const type = typeChecker.getTypeAtLocation(node);
|
|
133
135
|
return (type.getFlags() & ts.TypeFlags.EnumLiteral) !== 0;
|
|
134
136
|
}
|
|
135
|
-
function isExternalReference(node, program) {
|
|
137
|
+
function isExternalReference(node, program, externalNames) {
|
|
136
138
|
const typeChecker = program.getTypeChecker();
|
|
137
139
|
const nodeSym = typeChecker.getSymbolAtLocation(node);
|
|
138
140
|
let nodeFrom = nodeSym?.getDeclarations()?.[0];
|
|
139
141
|
while (nodeFrom) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
+
const sourceFileName = nodeFrom.getSourceFile();
|
|
143
|
+
if (externalNames.length === 0) {
|
|
144
|
+
if (/[\\/]node_modules[\\/]/.test(sourceFileName.fileName)) {
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
if (externalNames.some((part) => {
|
|
150
|
+
if (typeof part === 'string') {
|
|
151
|
+
return sourceFileName.fileName.replace(/\\/g, '/').includes(part);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
return part.test(sourceFileName.fileName);
|
|
155
|
+
}
|
|
156
|
+
})) {
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
142
159
|
}
|
|
143
160
|
// Walk into the 'import' variables
|
|
144
161
|
if (!ts.isImportSpecifier(nodeFrom)) {
|
|
@@ -146,7 +163,11 @@ function isExternalReference(node, program) {
|
|
|
146
163
|
}
|
|
147
164
|
const baseName = nodeFrom.propertyName ?? nodeFrom.name;
|
|
148
165
|
const baseSym = typeChecker.getSymbolAtLocation(baseName);
|
|
149
|
-
|
|
166
|
+
// We must follow 'aliased' symbol for parsing the symbol which name is not changed from the exported symbol name
|
|
167
|
+
const exportedSym = baseSym && baseSym.getFlags() & ts.SymbolFlags.Alias
|
|
168
|
+
? typeChecker.getAliasedSymbol(baseSym)
|
|
169
|
+
: baseSym;
|
|
170
|
+
nodeFrom = exportedSym?.getDeclarations()?.[0];
|
|
150
171
|
}
|
|
151
172
|
const type = typeChecker.getTypeAtLocation(node);
|
|
152
173
|
const sym = type.getSymbol();
|
package/dist/version.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "0.
|
|
1
|
+
declare const _default: "0.3.0";
|
|
2
2
|
export default _default;
|
package/dist/version.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '0.
|
|
1
|
+
export default '0.3.0';
|