@esportsplus/typescript 0.25.0 → 0.25.1

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.
@@ -1,6 +1,4 @@
1
- import { ts } from '../../index.js';
1
+ import ts from 'typescript';
2
2
  declare const getExpressionName: (node: ts.Expression) => string | null;
3
- declare const getPropertyPath: (node: ts.Expression) => string[] | null;
4
- declare const getPropertyPathString: (node: ts.Expression) => string | null;
5
- declare const unwrapParentheses: (expr: ts.Expression) => ts.Expression;
6
- export { getExpressionName, getPropertyPath, getPropertyPathString, unwrapParentheses };
3
+ declare const getPropertyPath: (node: ts.Expression) => string | null;
4
+ export { getExpressionName, getPropertyPath };
@@ -1,10 +1,10 @@
1
- import { ts } from '../../index.js';
1
+ import ts from 'typescript';
2
2
  const getExpressionName = (node) => {
3
3
  if (ts.isIdentifier(node)) {
4
4
  return node.text;
5
5
  }
6
6
  if (ts.isPropertyAccessExpression(node)) {
7
- return getPropertyPathString(node);
7
+ return getPropertyPath(node);
8
8
  }
9
9
  return null;
10
10
  };
@@ -16,18 +16,8 @@ const getPropertyPath = (node) => {
16
16
  }
17
17
  if (ts.isIdentifier(current)) {
18
18
  parts.push(current.text);
19
- return parts.reverse();
19
+ return parts.reverse().join('.');
20
20
  }
21
21
  return null;
22
22
  };
23
- const getPropertyPathString = (node) => {
24
- let parts = getPropertyPath(node);
25
- return parts ? parts.join('.') : null;
26
- };
27
- const unwrapParentheses = (expr) => {
28
- while (ts.isParenthesizedExpression(expr)) {
29
- expr = expr.expression;
30
- }
31
- return expr;
32
- };
33
- export { getExpressionName, getPropertyPath, getPropertyPathString, unwrapParentheses };
23
+ export { getExpressionName, getPropertyPath };
@@ -2,7 +2,6 @@ type Range = {
2
2
  end: number;
3
3
  start: number;
4
4
  };
5
- declare const containsPosition: (range: Range, pos: number) => boolean;
6
5
  declare const inRange: (ranges: Range[], start: number, end: number) => boolean;
7
- export { containsPosition, inRange };
6
+ export { inRange };
8
7
  export type { Range };
@@ -1,6 +1,3 @@
1
- const containsPosition = (range, pos) => {
2
- return pos >= range.start && pos <= range.end;
3
- };
4
1
  const inRange = (ranges, start, end) => {
5
2
  for (let i = 0, n = ranges.length; i < n; i++) {
6
3
  let r = ranges[i];
@@ -10,4 +7,4 @@ const inRange = (ranges, start, end) => {
10
7
  }
11
8
  return false;
12
9
  };
13
- export { containsPosition, inRange };
10
+ export { inRange };
@@ -1,7 +1,5 @@
1
- import type { QuickCheckPattern, Replacement } from './types.js';
1
+ import type { Replacement } from './types.js';
2
2
  declare const _default: {
3
- contains: (code: string, { regex, patterns }: QuickCheckPattern) => boolean;
4
- replace: (code: string, replacements: Replacement[]) => string;
5
3
  replaceReverse: (code: string, replacements: Replacement[]) => string;
6
4
  };
7
5
  export default _default;
@@ -1,35 +1,3 @@
1
- const contains = (code, { regex, patterns }) => {
2
- if (regex) {
3
- return regex.test(code);
4
- }
5
- if (patterns) {
6
- for (let i = 0, n = patterns.length; i < n; i++) {
7
- if (code.indexOf(patterns[i]) !== -1) {
8
- return true;
9
- }
10
- }
11
- }
12
- return false;
13
- };
14
- const replace = (code, replacements) => {
15
- if (replacements.length === 0) {
16
- return code;
17
- }
18
- replacements.sort((a, b) => a.start - b.start);
19
- let parts = [], pos = 0;
20
- for (let i = 0, n = replacements.length; i < n; i++) {
21
- let r = replacements[i];
22
- if (r.start > pos) {
23
- parts.push(code.substring(pos, r.start));
24
- }
25
- parts.push(r.newText);
26
- pos = r.end;
27
- }
28
- if (pos < code.length) {
29
- parts.push(code.substring(pos));
30
- }
31
- return parts.join('');
32
- };
33
1
  const replaceReverse = (code, replacements) => {
34
2
  if (replacements.length === 0) {
35
3
  return code;
@@ -42,4 +10,4 @@ const replaceReverse = (code, replacements) => {
42
10
  }
43
11
  return result;
44
12
  };
45
- export default { contains, replace, replaceReverse };
13
+ export default { replaceReverse };
@@ -12,6 +12,7 @@ type ModifyOptions = {
12
12
  declare const _default: {
13
13
  find: (sourceFile: ts.SourceFile, packageName: string) => ImportInfo[];
14
14
  isFromPackage: (node: ts.Identifier, packageName: string, checker?: ts.TypeChecker) => boolean;
15
+ isSymbolFromPackage: (checker: ts.TypeChecker, node: ts.Node, packageName: string, symbolName?: string) => boolean;
15
16
  modify: (sourceCode: string, sourceFile: ts.SourceFile, packageName: string, options: ModifyOptions) => string;
16
17
  trace: (node: ts.Identifier, checker: ts.TypeChecker) => string | null;
17
18
  };
@@ -29,6 +29,28 @@ const isFromPackage = (node, packageName, checker) => {
29
29
  let origin = trace(node, checker);
30
30
  return origin === null || origin.includes(packageName);
31
31
  };
32
+ const isSymbolFromPackage = (checker, node, packageName, symbolName) => {
33
+ let symbol = checker.getSymbolAtLocation(node);
34
+ if (!symbol) {
35
+ return false;
36
+ }
37
+ if (symbol.flags & ts.SymbolFlags.Alias) {
38
+ symbol = checker.getAliasedSymbol(symbol);
39
+ }
40
+ if (symbolName && symbol.name !== symbolName) {
41
+ return false;
42
+ }
43
+ let declarations = symbol.getDeclarations();
44
+ if (!declarations || declarations.length === 0) {
45
+ return false;
46
+ }
47
+ for (let i = 0, n = declarations.length; i < n; i++) {
48
+ if (declarations[i].getSourceFile().fileName.includes(packageName)) {
49
+ return true;
50
+ }
51
+ }
52
+ return false;
53
+ };
32
54
  const modify = (sourceCode, sourceFile, packageName, options) => {
33
55
  let { namespace } = options;
34
56
  if (!options.add && !options.namespace && !options.remove) {
@@ -92,4 +114,4 @@ const trace = (node, checker) => {
92
114
  }
93
115
  return declarations[0].getSourceFile().fileName;
94
116
  };
95
- export default { find, isFromPackage, modify, trace };
117
+ export default { find, isFromPackage, isSymbolFromPackage, modify, trace };
@@ -3,6 +3,5 @@ export { default as code } from './code.js';
3
3
  export { default as coordinator } from './coordinator.js';
4
4
  export { default as imports } from './imports.js';
5
5
  export { default as plugin } from './plugins/index.js';
6
- export { default as program } from './program.js';
7
6
  export { default as uid } from './uid.js';
8
7
  export type * from './types.js';
@@ -3,5 +3,4 @@ export { default as code } from './code.js';
3
3
  export { default as coordinator } from './coordinator.js';
4
4
  export { default as imports } from './imports.js';
5
5
  export { default as plugin } from './plugins/index.js';
6
- export { default as program } from './program.js';
7
6
  export { default as uid } from './uid.js';
@@ -1,11 +1,11 @@
1
1
  declare const _default: {
2
- tsc: (plugins: import("../types.js").Plugin[]) => (program: import("typescript").Program, shared: import("../types.js").SharedContext) => {
2
+ tsc: (plugins: import("../index.js").Plugin[]) => (program: import("typescript").Program, shared: import("../index.js").SharedContext) => {
3
3
  transform: import("typescript").TransformerFactory<import("typescript").SourceFile>;
4
4
  };
5
5
  vite: ({ name, onWatchChange, plugins }: {
6
6
  name: string;
7
7
  onWatchChange?: () => void;
8
- plugins: import("../types.js").Plugin[];
8
+ plugins: import("../index.js").Plugin[];
9
9
  }) => ({ root }?: {
10
10
  root?: string;
11
11
  }) => {
@@ -1,7 +1,6 @@
1
1
  import type { Plugin, SharedContext } from '../types.js';
2
2
  import type ts from 'typescript';
3
- type PluginInstance = {
3
+ declare const _default: (plugins: Plugin[]) => (program: ts.Program, shared: SharedContext) => {
4
4
  transform: ts.TransformerFactory<ts.SourceFile>;
5
5
  };
6
- declare const _default: (plugins: Plugin[]) => (program: ts.Program, shared: SharedContext) => PluginInstance;
7
6
  export default _default;
@@ -10,10 +10,6 @@ type Plugin = {
10
10
  transform: (ctx: TransformContext) => TransformResult;
11
11
  };
12
12
  type PluginFactory = (options?: Record<string, unknown>) => Plugin;
13
- type QuickCheckPattern = {
14
- patterns?: string[];
15
- regex?: RegExp;
16
- };
17
13
  type Range = {
18
14
  end: number;
19
15
  start: number;
@@ -38,4 +34,4 @@ type TransformResult = {
38
34
  prepend?: string[];
39
35
  replacements?: ReplacementIntent[];
40
36
  };
41
- export type { ImportIntent, Plugin, PluginFactory, QuickCheckPattern, Range, Replacement, ReplacementIntent, SharedContext, TransformContext, TransformResult };
37
+ export type { ImportIntent, Plugin, PluginFactory, Range, Replacement, ReplacementIntent, SharedContext, TransformContext, TransformResult };
package/package.json CHANGED
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "type": "module",
39
39
  "types": "build/index.d.ts",
40
- "version": "0.25.0",
40
+ "version": "0.25.1",
41
41
  "scripts": {
42
42
  "build": "tsc && tsc-alias",
43
43
  "-": "-"
@@ -1,4 +1,4 @@
1
- import { ts } from '../..';
1
+ import ts from 'typescript';
2
2
 
3
3
 
4
4
  const getExpressionName = (node: ts.Expression): string | null => {
@@ -7,13 +7,13 @@ const getExpressionName = (node: ts.Expression): string | null => {
7
7
  }
8
8
 
9
9
  if (ts.isPropertyAccessExpression(node)) {
10
- return getPropertyPathString(node);
10
+ return getPropertyPath(node);
11
11
  }
12
12
 
13
13
  return null;
14
- }
14
+ };
15
15
 
16
- const getPropertyPath = (node: ts.Expression): string[] | null => {
16
+ const getPropertyPath = (node: ts.Expression): string | null => {
17
17
  let current: ts.Node = node,
18
18
  parts: string[] = [];
19
19
 
@@ -24,25 +24,11 @@ const getPropertyPath = (node: ts.Expression): string[] | null => {
24
24
 
25
25
  if (ts.isIdentifier(current)) {
26
26
  parts.push(current.text);
27
- return parts.reverse();
27
+ return parts.reverse().join('.');
28
28
  }
29
29
 
30
30
  return null;
31
- }
31
+ };
32
32
 
33
- const getPropertyPathString = (node: ts.Expression): string | null => {
34
- let parts = getPropertyPath(node);
35
33
 
36
- return parts ? parts.join('.') : null;
37
- }
38
-
39
- const unwrapParentheses = (expr: ts.Expression): ts.Expression => {
40
- while (ts.isParenthesizedExpression(expr)) {
41
- expr = expr.expression;
42
- }
43
-
44
- return expr;
45
- }
46
-
47
-
48
- export { getExpressionName, getPropertyPath, getPropertyPathString, unwrapParentheses };
34
+ export { getExpressionName, getPropertyPath };
@@ -4,10 +4,6 @@ type Range = {
4
4
  };
5
5
 
6
6
 
7
- const containsPosition = (range: Range, pos: number): boolean => {
8
- return pos >= range.start && pos <= range.end;
9
- }
10
-
11
7
  const inRange = (ranges: Range[], start: number, end: number): boolean => {
12
8
  for (let i = 0, n = ranges.length; i < n; i++) {
13
9
  let r = ranges[i];
@@ -18,8 +14,8 @@ const inRange = (ranges: Range[], start: number, end: number): boolean => {
18
14
  }
19
15
 
20
16
  return false;
21
- }
17
+ };
22
18
 
23
19
 
24
- export { containsPosition, inRange };
20
+ export { inRange };
25
21
  export type { Range };
@@ -1,51 +1,7 @@
1
- import type { QuickCheckPattern, Replacement } from './types.js';
1
+ import type { Replacement } from './types';
2
2
 
3
3
 
4
- const contains = (code: string, { regex, patterns }: QuickCheckPattern): boolean => {
5
- if (regex) {
6
- return regex.test(code);
7
- }
8
-
9
- if (patterns) {
10
- for (let i = 0, n = patterns.length; i < n; i++) {
11
- if (code.indexOf(patterns[i]) !== -1) {
12
- return true;
13
- }
14
- }
15
- }
16
-
17
- return false;
18
- };
19
-
20
- const replace = (code: string, replacements: Replacement[]): string => {
21
- if (replacements.length === 0) {
22
- return code;
23
- }
24
-
25
- replacements.sort((a, b) => a.start - b.start);
26
-
27
- let parts: string[] = [],
28
- pos = 0;
29
-
30
- for (let i = 0, n = replacements.length; i < n; i++) {
31
- let r = replacements[i];
32
-
33
- if (r.start > pos) {
34
- parts.push(code.substring(pos, r.start));
35
- }
36
-
37
- parts.push(r.newText);
38
- pos = r.end;
39
- }
40
-
41
- if (pos < code.length) {
42
- parts.push(code.substring(pos));
43
- }
44
-
45
- return parts.join('');
46
- };
47
-
48
- const replaceReverse =(code: string, replacements: Replacement[]): string => {
4
+ const replaceReverse = (code: string, replacements: Replacement[]): string => {
49
5
  if (replacements.length === 0) {
50
6
  return code;
51
7
  }
@@ -64,4 +20,4 @@ const replaceReverse =(code: string, replacements: Replacement[]): string => {
64
20
  };
65
21
 
66
22
 
67
- export default { contains, replace, replaceReverse };
23
+ export default { replaceReverse };
@@ -1,7 +1,7 @@
1
- import type { ImportIntent, Plugin, ReplacementIntent, SharedContext } from './types.js';
2
- import { ts } from '~/index.js';
3
- import code from './code.js';
4
- import imports from './imports.js';
1
+ import type { ImportIntent, Plugin, ReplacementIntent, SharedContext } from './types';
2
+ import { ts } from '~/index';
3
+ import code from './code';
4
+ import imports from './imports';
5
5
 
6
6
 
7
7
  type CoordinatorResult = {
@@ -1,6 +1,6 @@
1
- import type { Replacement } from './types.js';
2
- import { ts } from '~/index.js';
3
- import code from './code.js';
1
+ import type { Replacement } from './types';
2
+ import { ts } from '~/index';
3
+ import code from './code';
4
4
 
5
5
 
6
6
  type ImportInfo = {
@@ -65,6 +65,44 @@ const isFromPackage = (node: ts.Identifier, packageName: string, checker?: ts.Ty
65
65
  return origin === null || origin.includes(packageName);
66
66
  };
67
67
 
68
+ // Check if node's symbol originates from a specific package (with optional symbol name validation)
69
+ const isSymbolFromPackage = (
70
+ checker: ts.TypeChecker,
71
+ node: ts.Node,
72
+ packageName: string,
73
+ symbolName?: string
74
+ ): boolean => {
75
+ let symbol = checker.getSymbolAtLocation(node);
76
+
77
+ if (!symbol) {
78
+ return false;
79
+ }
80
+
81
+ // Follow aliases to original symbol (handles re-exports and aliased imports)
82
+ if (symbol.flags & ts.SymbolFlags.Alias) {
83
+ symbol = checker.getAliasedSymbol(symbol);
84
+ }
85
+
86
+ // Check symbol name if specified
87
+ if (symbolName && symbol.name !== symbolName) {
88
+ return false;
89
+ }
90
+
91
+ let declarations = symbol.getDeclarations();
92
+
93
+ if (!declarations || declarations.length === 0) {
94
+ return false;
95
+ }
96
+
97
+ // Check if any declaration is from the expected package
98
+ for (let i = 0, n = declarations.length; i < n; i++) {
99
+ if (declarations[i].getSourceFile().fileName.includes(packageName)) {
100
+ return true;
101
+ }
102
+ }
103
+
104
+ return false;
105
+ };
68
106
 
69
107
  // Modify imports: remove specified, add needed, delete if empty
70
108
  const modify = (
@@ -166,5 +204,5 @@ const trace = (node: ts.Identifier, checker: ts.TypeChecker): string | null => {
166
204
  };
167
205
 
168
206
 
169
- export default { find, isFromPackage, modify, trace };
207
+ export default { find, isFromPackage, isSymbolFromPackage, modify, trace };
170
208
  export type { ImportInfo, ModifyOptions };
@@ -1,8 +1,7 @@
1
- export * as ast from './ast/index.js';
2
- export { default as code } from './code.js';
3
- export { default as coordinator } from './coordinator.js';
4
- export { default as imports } from './imports.js';
5
- export { default as plugin } from './plugins/index.js';
6
- export { default as program } from './program.js';
7
- export { default as uid } from './uid.js';
8
- export type * from './types.js';
1
+ export * as ast from './ast/index';
2
+ export { default as code } from './code';
3
+ export { default as coordinator } from './coordinator';
4
+ export { default as imports } from './imports';
5
+ export { default as plugin } from './plugins/index';
6
+ export { default as uid } from './uid';
7
+ export type * from './types';
@@ -1,5 +1,5 @@
1
- import tsc from './tsc.js';
2
- import vite from './vite.js';
1
+ import tsc from './tsc';
2
+ import vite from './vite';
3
3
 
4
4
 
5
5
  export default { tsc, vite };
@@ -1,15 +1,10 @@
1
- import type { Plugin, SharedContext } from '../types.js';
1
+ import type { Plugin, SharedContext } from '../types';
2
2
  import type ts from 'typescript';
3
- import coordinator from '../coordinator.js';
4
-
5
-
6
- type PluginInstance = {
7
- transform: ts.TransformerFactory<ts.SourceFile>;
8
- };
3
+ import coordinator from '../coordinator';
9
4
 
10
5
 
11
6
  export default (plugins: Plugin[]) => {
12
- return (program: ts.Program, shared: SharedContext): PluginInstance => {
7
+ return (program: ts.Program, shared: SharedContext) => {
13
8
  return {
14
9
  transform: (() => {
15
10
  return (sourceFile: ts.SourceFile) => {
@@ -1,8 +1,8 @@
1
1
  import type { ResolvedConfig } from 'vite';
2
- import type { Plugin, SharedContext } from '../types.js';
3
- import { ts } from '~/index.js';
4
- import coordinator from '../coordinator.js';
5
- import program from '../program.js';
2
+ import type { Plugin, SharedContext } from '../types';
3
+ import { ts } from '~/index';
4
+ import coordinator from '../coordinator';
5
+ import program from '../program';
6
6
 
7
7
 
8
8
  type VitePlugin = {
@@ -51,4 +51,4 @@ const del = (root: string): void => {
51
51
  }
52
52
 
53
53
 
54
- export default { get, delete: del };
54
+ export default { get, delete: del };
@@ -24,11 +24,6 @@ type Plugin = {
24
24
 
25
25
  type PluginFactory = (options?: Record<string, unknown>) => Plugin;
26
26
 
27
- type QuickCheckPattern = {
28
- patterns?: string[];
29
- regex?: RegExp;
30
- };
31
-
32
27
  type Range = {
33
28
  end: number;
34
29
  start: number;
@@ -82,7 +77,6 @@ type TransformResult = {
82
77
  export type {
83
78
  ImportIntent,
84
79
  Plugin, PluginFactory,
85
- QuickCheckPattern,
86
80
  Range, Replacement, ReplacementIntent,
87
81
  SharedContext,
88
82
  TransformContext, TransformResult