ts-const-value-transformer 0.2.1 → 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 CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.3.0
4
+
5
+ Add `externalNames` option for `hoistExternalValues`
6
+
3
7
  ## v0.2.1
4
8
 
5
9
  Fix for tracing imported symbol
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
 
@@ -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;
@@ -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 && isExternalReference(node, program)) {
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
- if (program.isSourceFileFromExternalLibrary(nodeFrom.getSourceFile())) {
141
- return true;
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)) {
@@ -1,2 +1,2 @@
1
- declare const _default: "0.2.1";
1
+ declare const _default: "0.3.0";
2
2
  export default _default;
package/dist/version.mjs CHANGED
@@ -1 +1 @@
1
- export default '0.2.1';
1
+ export default '0.3.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-const-value-transformer",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "engines": {
5
5
  "node": ">=20.19.3"
6
6
  },