@esportsplus/typescript 0.26.0 → 0.26.2

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,5 +1,5 @@
1
- import type { Replacement } from './types.js';
2
- declare const _default: {
3
- replaceReverse: (code: string, replacements: Replacement[]) => string;
1
+ declare const code: {
2
+ (literals: TemplateStringsArray, ...values: unknown[]): string;
3
+ escape(str: string): string;
4
4
  };
5
- export default _default;
5
+ export default code;
@@ -1,13 +1,17 @@
1
- const replaceReverse = (code, replacements) => {
2
- if (replacements.length === 0) {
3
- return code;
1
+ const SINGLE_QUOTE_REGEX = /'/g;
2
+ const code = (literals, ...values) => {
3
+ let buffer = '';
4
+ for (let i = 0, n = literals.length; i < n; i++) {
5
+ buffer += literals[i];
6
+ let value = values[i];
7
+ if (value == null || value === false) {
8
+ value = '';
9
+ }
10
+ buffer += value;
4
11
  }
5
- replacements.sort((a, b) => b.start - a.start);
6
- let result = code;
7
- for (let i = 0, n = replacements.length; i < n; i++) {
8
- let r = replacements[i];
9
- result = result.substring(0, r.start) + r.newText + result.substring(r.end);
10
- }
11
- return result;
12
+ return buffer;
13
+ };
14
+ code.escape = (str) => {
15
+ return str.replace(SINGLE_QUOTE_REGEX, "\\'");
12
16
  };
13
- export default { replaceReverse };
17
+ export default code;
@@ -1,11 +1,10 @@
1
1
  import { ts } from '../index.js';
2
- import code from './code.js';
3
2
  import imports from './imports.js';
4
3
  function applyImports(sourceCode, sourceFile, intents) {
5
4
  let result = sourceCode;
6
5
  for (let i = 0, n = intents.length; i < n; i++) {
7
6
  let intent = intents[i];
8
- result = imports.modify(result, sourceFile, intent.package, {
7
+ result = modify(result, sourceFile, intent.package, {
9
8
  add: intent.add,
10
9
  namespace: intent.namespace,
11
10
  remove: intent.remove
@@ -20,12 +19,11 @@ function applyIntents(sourceCode, sourceFile, intents) {
20
19
  if (intents.length === 0) {
21
20
  return sourceCode;
22
21
  }
23
- let replacements = intents.map(intent => ({
22
+ return replaceReverse(sourceCode, intents.map(intent => ({
24
23
  end: intent.node.end,
25
24
  newText: intent.generate(sourceFile),
26
25
  start: intent.node.getStart(sourceFile)
27
- }));
28
- return code.replaceReverse(sourceCode, replacements);
26
+ })));
29
27
  }
30
28
  function applyPrepend(sourceCode, sourceFile, prepend) {
31
29
  if (prepend.length === 0) {
@@ -58,6 +56,67 @@ function hasPattern(sourceCode, patterns) {
58
56
  }
59
57
  return false;
60
58
  }
59
+ const modify = (sourceCode, sourceFile, packageName, options) => {
60
+ let { namespace } = options;
61
+ if (!options.add && !options.namespace && !options.remove) {
62
+ return sourceCode;
63
+ }
64
+ let add = options.add ? new Set(options.add) : null, found = imports.find(sourceFile, packageName), remove = options.remove ? new Set(options.remove) : null;
65
+ if (found.length === 0) {
66
+ let statements = [];
67
+ if (namespace) {
68
+ statements.push(`import * as ${namespace} from '${packageName}';`);
69
+ }
70
+ if (add && add.size > 0) {
71
+ statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
72
+ }
73
+ if (statements.length === 0) {
74
+ return sourceCode;
75
+ }
76
+ return statements.join('\n') + '\n' + sourceCode;
77
+ }
78
+ let specifiers = new Set();
79
+ for (let i = 0, n = found.length; i < n; i++) {
80
+ for (let [name, alias] of found[i].specifiers) {
81
+ if (!remove || (!remove.has(name) && !remove.has(alias))) {
82
+ specifiers.add(name === alias ? name : `${name} as ${alias}`);
83
+ }
84
+ }
85
+ }
86
+ if (add) {
87
+ for (let name of add) {
88
+ specifiers.add(name);
89
+ }
90
+ }
91
+ let statements = [];
92
+ if (namespace) {
93
+ statements.push(`import * as ${namespace} from '${packageName}';`);
94
+ }
95
+ if (specifiers.size > 0) {
96
+ statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
97
+ }
98
+ let replacements = [];
99
+ for (let i = 0, n = found.length; i < n; i++) {
100
+ replacements.push({
101
+ end: found[i].end,
102
+ newText: i === 0 ? statements.join('\n') : '',
103
+ start: found[i].start
104
+ });
105
+ }
106
+ return replaceReverse(sourceCode, replacements);
107
+ };
108
+ const replaceReverse = (code, replacements) => {
109
+ if (replacements.length === 0) {
110
+ return code;
111
+ }
112
+ replacements.sort((a, b) => b.start - a.start);
113
+ let result = code;
114
+ for (let i = 0, n = replacements.length; i < n; i++) {
115
+ let r = replacements[i];
116
+ result = result.substring(0, r.start) + r.newText + result.substring(r.end);
117
+ }
118
+ return result;
119
+ };
61
120
  const transform = (plugins, sourceCode, sourceFile, program, shared) => {
62
121
  if (plugins.length === 0) {
63
122
  return { changed: false, code: sourceCode, sourceFile };
@@ -12,8 +12,6 @@ type ModifyOptions = {
12
12
  declare const _default: {
13
13
  find: (sourceFile: ts.SourceFile, packageName: string) => ImportInfo[];
14
14
  inPackage: (checker: ts.TypeChecker, node: ts.Node, pkg: string, symbolName?: string, packageImports?: Set<string>) => boolean;
15
- modify: (sourceCode: string, sourceFile: ts.SourceFile, packageName: string, options: ModifyOptions) => string;
16
- trace: (node: ts.Identifier, checker: ts.TypeChecker) => string | null;
17
15
  };
18
16
  export default _default;
19
17
  export type { ImportInfo, ModifyOptions };
@@ -1,5 +1,4 @@
1
1
  import { ts } from '../index.js';
2
- import code from './code.js';
3
2
  const find = (sourceFile, packageName) => {
4
3
  let imports = [];
5
4
  for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
@@ -52,67 +51,4 @@ const inPackage = (checker, node, pkg, symbolName, packageImports) => {
52
51
  }
53
52
  return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
54
53
  };
55
- const modify = (sourceCode, sourceFile, packageName, options) => {
56
- let { namespace } = options;
57
- if (!options.add && !options.namespace && !options.remove) {
58
- return sourceCode;
59
- }
60
- let add = options.add ? new Set(options.add) : null, imports = find(sourceFile, packageName), remove = options.remove ? new Set(options.remove) : null;
61
- if (imports.length === 0) {
62
- let statements = [];
63
- if (namespace) {
64
- statements.push(`import * as ${namespace} from '${packageName}';`);
65
- }
66
- if (add && add.size > 0) {
67
- statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
68
- }
69
- if (statements.length === 0) {
70
- return sourceCode;
71
- }
72
- return statements.join('\n') + '\n' + sourceCode;
73
- }
74
- let specifiers = new Set();
75
- for (let i = 0, n = imports.length; i < n; i++) {
76
- for (let [name, alias] of imports[i].specifiers) {
77
- if (!remove || (!remove.has(name) && !remove.has(alias))) {
78
- specifiers.add(name === alias ? name : `${name} as ${alias}`);
79
- }
80
- }
81
- }
82
- if (add) {
83
- for (let name of add) {
84
- specifiers.add(name);
85
- }
86
- }
87
- let statements = [];
88
- if (namespace) {
89
- statements.push(`import * as ${namespace} from '${packageName}';`);
90
- }
91
- if (specifiers.size > 0) {
92
- statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
93
- }
94
- let replacements = [];
95
- for (let i = 0, n = imports.length; i < n; i++) {
96
- replacements.push({
97
- end: imports[i].end,
98
- newText: i === 0 ? statements.join('\n') : '',
99
- start: imports[i].start
100
- });
101
- }
102
- return code.replaceReverse(sourceCode, replacements);
103
- };
104
- const trace = (node, checker) => {
105
- let symbol = checker.getSymbolAtLocation(node);
106
- if (!symbol) {
107
- return null;
108
- }
109
- if (symbol.flags & ts.SymbolFlags.Alias) {
110
- symbol = checker.getAliasedSymbol(symbol);
111
- }
112
- let declarations = symbol.getDeclarations();
113
- if (!declarations || declarations.length === 0) {
114
- return null;
115
- }
116
- return declarations[0].getSourceFile().fileName;
117
- };
118
- export default { find, inPackage, modify, trace };
54
+ export default { find, inPackage };
package/package.json CHANGED
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "type": "module",
39
39
  "types": "build/index.d.ts",
40
- "version": "0.26.0",
40
+ "version": "0.26.2",
41
41
  "scripts": {
42
42
  "build": "tsc && tsc-alias",
43
43
  "-": "-"
@@ -1,23 +1,27 @@
1
- import type { Replacement } from './types';
1
+ const SINGLE_QUOTE_REGEX = /'/g;
2
2
 
3
3
 
4
- const replaceReverse = (code: string, replacements: Replacement[]): string => {
5
- if (replacements.length === 0) {
6
- return code;
7
- }
4
+ const code = (literals: TemplateStringsArray, ...values: unknown[]): string => {
5
+ let buffer = '';
8
6
 
9
- replacements.sort((a, b) => b.start - a.start);
7
+ for (let i = 0, n = literals.length; i < n; i++) {
8
+ buffer += literals[i];
10
9
 
11
- let result = code;
10
+ let value = values[i];
12
11
 
13
- for (let i = 0, n = replacements.length; i < n; i++) {
14
- let r = replacements[i];
12
+ if (value == null || value === false) {
13
+ value = '';
14
+ }
15
15
 
16
- result = result.substring(0, r.start) + r.newText + result.substring(r.end);
16
+ buffer += value;
17
17
  }
18
18
 
19
- return result;
19
+ return buffer;
20
+ };
21
+
22
+ code.escape = (str: string): string => {
23
+ return str.replace(SINGLE_QUOTE_REGEX, "\\'");
20
24
  };
21
25
 
22
26
 
23
- export default { replaceReverse };
27
+ export default code;
@@ -1,7 +1,6 @@
1
- import type { ImportIntent, Plugin, ReplacementIntent, SharedContext } from './types';
1
+ import type { ImportIntent, Plugin, Replacement, ReplacementIntent, SharedContext } from './types';
2
2
  import { ts } from '~/index';
3
- import code from './code';
4
- import imports from './imports';
3
+ import imports, { ModifyOptions } from './imports';
5
4
 
6
5
 
7
6
  type CoordinatorResult = {
@@ -21,7 +20,7 @@ function applyImports(
21
20
  for (let i = 0, n = intents.length; i < n; i++) {
22
21
  let intent = intents[i];
23
22
 
24
- result = imports.modify(result, sourceFile, intent.package, {
23
+ result = modify(result, sourceFile, intent.package, {
25
24
  add: intent.add,
26
25
  namespace: intent.namespace,
27
26
  remove: intent.remove
@@ -49,13 +48,14 @@ function applyIntents(
49
48
  return sourceCode;
50
49
  }
51
50
 
52
- let replacements = intents.map(intent => ({
53
- end: intent.node.end,
54
- newText: intent.generate(sourceFile),
55
- start: intent.node.getStart(sourceFile)
56
- }));
57
-
58
- return code.replaceReverse(sourceCode, replacements);
51
+ return replaceReverse(
52
+ sourceCode,
53
+ intents.map(intent => ({
54
+ end: intent.node.end,
55
+ newText: intent.generate(sourceFile),
56
+ start: intent.node.getStart(sourceFile)
57
+ }))
58
+ );
59
59
  }
60
60
 
61
61
  function applyPrepend(sourceCode: string, sourceFile: ts.SourceFile, prepend: string[]): string {
@@ -100,6 +100,101 @@ function hasPattern(sourceCode: string, patterns: string[]): boolean {
100
100
  return false;
101
101
  }
102
102
 
103
+ const modify = (
104
+ sourceCode: string,
105
+ sourceFile: ts.SourceFile,
106
+ packageName: string,
107
+ options: ModifyOptions
108
+ ): string => {
109
+ let { namespace } = options;
110
+
111
+ // Fast path: nothing to change
112
+ if (!options.add && !options.namespace && !options.remove) {
113
+ return sourceCode;
114
+ }
115
+
116
+ let add = options.add ? new Set(options.add) : null,
117
+ found = imports.find(sourceFile, packageName),
118
+ remove = options.remove ? new Set(options.remove) : null;
119
+
120
+ if (found.length === 0) {
121
+ let statements: string[] = [];
122
+
123
+ if (namespace) {
124
+ statements.push(`import * as ${namespace} from '${packageName}';`);
125
+ }
126
+
127
+ if (add && add.size > 0) {
128
+ statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
129
+ }
130
+
131
+ if (statements.length === 0) {
132
+ return sourceCode;
133
+ }
134
+
135
+ return statements.join('\n') + '\n' + sourceCode;
136
+ }
137
+
138
+ // Collect all non-removed specifiers from existing imports
139
+ let specifiers = new Set<string>();
140
+
141
+ for (let i = 0, n = found.length; i < n; i++) {
142
+ for (let [name, alias] of found[i].specifiers) {
143
+ if (!remove || (!remove.has(name) && !remove.has(alias))) {
144
+ specifiers.add(name === alias ? name : `${name} as ${alias}`);
145
+ }
146
+ }
147
+ }
148
+
149
+ if (add) {
150
+ for (let name of add) {
151
+ specifiers.add(name);
152
+ }
153
+ }
154
+
155
+ // Build replacement text - namespace import first, then named imports
156
+ let statements: string[] = [];
157
+
158
+ if (namespace) {
159
+ statements.push(`import * as ${namespace} from '${packageName}';`);
160
+ }
161
+
162
+ if (specifiers.size > 0) {
163
+ statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
164
+ }
165
+
166
+ // Build replacements - replace first import, remove others
167
+ let replacements: Replacement[] = [];
168
+
169
+ for (let i = 0, n = found.length; i < n; i++) {
170
+ replacements.push({
171
+ end: found[i].end,
172
+ newText: i === 0 ? statements.join('\n') : '',
173
+ start: found[i].start
174
+ });
175
+ }
176
+
177
+ return replaceReverse(sourceCode, replacements);
178
+ };
179
+
180
+ const replaceReverse = (code: string, replacements: Replacement[]): string => {
181
+ if (replacements.length === 0) {
182
+ return code;
183
+ }
184
+
185
+ replacements.sort((a, b) => b.start - a.start);
186
+
187
+ let result = code;
188
+
189
+ for (let i = 0, n = replacements.length; i < n; i++) {
190
+ let r = replacements[i];
191
+
192
+ result = result.substring(0, r.start) + r.newText + result.substring(r.end);
193
+ }
194
+
195
+ return result;
196
+ };
197
+
103
198
 
104
199
  /**
105
200
  * Transform source through all plugins sequentially.
@@ -1,6 +1,4 @@
1
- import type { Replacement } from './types';
2
1
  import { ts } from '~/index';
3
- import code from './code';
4
2
 
5
3
 
6
4
  type ImportInfo = {
@@ -104,105 +102,6 @@ const inPackage = (
104
102
  return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
105
103
  };
106
104
 
107
- // Modify imports: remove specified, add needed, delete if empty
108
- const modify = (
109
- sourceCode: string,
110
- sourceFile: ts.SourceFile,
111
- packageName: string,
112
- options: ModifyOptions
113
- ): string => {
114
- let { namespace } = options;
115
-
116
- // Fast path: nothing to change
117
- if (!options.add && !options.namespace && !options.remove) {
118
- return sourceCode;
119
- }
120
-
121
- let add = options.add ? new Set(options.add) : null,
122
- imports = find(sourceFile, packageName),
123
- remove = options.remove ? new Set(options.remove) : null;
124
-
125
- if (imports.length === 0) {
126
- let statements: string[] = [];
127
-
128
- if (namespace) {
129
- statements.push(`import * as ${namespace} from '${packageName}';`);
130
- }
131
-
132
- if (add && add.size > 0) {
133
- statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
134
- }
135
-
136
- if (statements.length === 0) {
137
- return sourceCode;
138
- }
139
-
140
- return statements.join('\n') + '\n' + sourceCode;
141
- }
142
-
143
- // Collect all non-removed specifiers from existing imports
144
- let specifiers = new Set<string>();
145
-
146
- for (let i = 0, n = imports.length; i < n; i++) {
147
- for (let [name, alias] of imports[i].specifiers) {
148
- if (!remove || (!remove.has(name) && !remove.has(alias))) {
149
- specifiers.add(name === alias ? name : `${name} as ${alias}`);
150
- }
151
- }
152
- }
153
-
154
- if (add) {
155
- for (let name of add) {
156
- specifiers.add(name);
157
- }
158
- }
159
-
160
- // Build replacement text - namespace import first, then named imports
161
- let statements: string[] = [];
162
-
163
- if (namespace) {
164
- statements.push(`import * as ${namespace} from '${packageName}';`);
165
- }
166
-
167
- if (specifiers.size > 0) {
168
- statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
169
- }
170
-
171
- // Build replacements - replace first import, remove others
172
- let replacements: Replacement[] = [];
173
-
174
- for (let i = 0, n = imports.length; i < n; i++) {
175
- replacements.push({
176
- end: imports[i].end,
177
- newText: i === 0 ? statements.join('\n') : '',
178
- start: imports[i].start
179
- });
180
- }
181
-
182
- return code.replaceReverse(sourceCode, replacements);
183
- };
184
-
185
- // Trace symbol through re-exports to find original declaration source file
186
- const trace = (node: ts.Identifier, checker: ts.TypeChecker): string | null => {
187
- let symbol = checker.getSymbolAtLocation(node);
188
-
189
- if (!symbol) {
190
- return null;
191
- }
192
-
193
- if (symbol.flags & ts.SymbolFlags.Alias) {
194
- symbol = checker.getAliasedSymbol(symbol);
195
- }
196
-
197
- let declarations = symbol.getDeclarations();
198
-
199
- if (!declarations || declarations.length === 0) {
200
- return null;
201
- }
202
-
203
- return declarations[0].getSourceFile().fileName;
204
- };
205
-
206
105
 
207
- export default { find, inPackage, modify, trace };
106
+ export default { find, inPackage };
208
107
  export type { ImportInfo, ModifyOptions };