@esportsplus/typescript 0.26.2 → 0.26.4

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.
@@ -6,7 +6,7 @@ type CoordinatorResult = {
6
6
  sourceFile: ts.SourceFile;
7
7
  };
8
8
  declare const _default: {
9
- transform: (plugins: Plugin[], sourceCode: string, sourceFile: ts.SourceFile, program: ts.Program, shared: SharedContext) => CoordinatorResult;
9
+ transform: (plugins: Plugin[], code: string, file: ts.SourceFile, program: ts.Program, shared: SharedContext) => CoordinatorResult;
10
10
  };
11
11
  export default _default;
12
12
  export type { CoordinatorResult };
@@ -1,81 +1,75 @@
1
1
  import { ts } from '../index.js';
2
2
  import imports from './imports.js';
3
- function applyImports(sourceCode, sourceFile, intents) {
4
- let result = sourceCode;
3
+ function applyImports(code, file, intents) {
5
4
  for (let i = 0, n = intents.length; i < n; i++) {
6
5
  let intent = intents[i];
7
- result = modify(result, sourceFile, intent.package, {
6
+ code = modify(code, file, intent.package, {
8
7
  add: intent.add,
9
8
  namespace: intent.namespace,
10
9
  remove: intent.remove
11
10
  });
12
11
  if (i < n - 1) {
13
- sourceFile = ts.createSourceFile(sourceFile.fileName, result, sourceFile.languageVersion, true);
12
+ file = ts.createSourceFile(file.fileName, code, file.languageVersion, true);
14
13
  }
15
14
  }
16
- return result;
15
+ return code;
17
16
  }
18
- function applyIntents(sourceCode, sourceFile, intents) {
17
+ function applyIntents(code, file, intents) {
19
18
  if (intents.length === 0) {
20
- return sourceCode;
19
+ return code;
21
20
  }
22
- return replaceReverse(sourceCode, intents.map(intent => ({
21
+ return replaceReverse(code, intents.map(intent => ({
23
22
  end: intent.node.end,
24
- newText: intent.generate(sourceFile),
25
- start: intent.node.getStart(sourceFile)
23
+ newText: intent.generate(file),
24
+ start: intent.node.getStart(file)
26
25
  })));
27
26
  }
28
- function applyPrepend(sourceCode, sourceFile, prepend) {
27
+ function applyPrepend(code, file, prepend) {
29
28
  if (prepend.length === 0) {
30
- return sourceCode;
31
- }
32
- let insertPos = findLastImportEnd(sourceFile), prependText = prepend.join('\n') + '\n';
33
- if (insertPos === 0) {
34
- return prependText + sourceCode;
29
+ return code;
35
30
  }
36
- return sourceCode.slice(0, insertPos) + '\n' + prependText + sourceCode.slice(insertPos);
37
- }
38
- function findLastImportEnd(sourceFile) {
39
- let lastEnd = 0;
40
- for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
41
- let stmt = sourceFile.statements[i];
31
+ let position = 0;
32
+ for (let i = 0, n = file.statements.length; i < n; i++) {
33
+ let stmt = file.statements[i];
42
34
  if (ts.isImportDeclaration(stmt)) {
43
- lastEnd = stmt.end;
35
+ position = stmt.end;
44
36
  }
45
37
  else {
46
38
  break;
47
39
  }
48
40
  }
49
- return lastEnd;
41
+ if (position === 0) {
42
+ return prepend.join('\n') + code;
43
+ }
44
+ return code.slice(0, position) + prepend.join('\n') + code.slice(position);
50
45
  }
51
- function hasPattern(sourceCode, patterns) {
46
+ function hasPattern(code, patterns) {
52
47
  for (let i = 0, n = patterns.length; i < n; i++) {
53
- if (sourceCode.indexOf(patterns[i]) !== -1) {
48
+ if (code.indexOf(patterns[i]) !== -1) {
54
49
  return true;
55
50
  }
56
51
  }
57
52
  return false;
58
53
  }
59
- const modify = (sourceCode, sourceFile, packageName, options) => {
60
- let { namespace } = options;
54
+ function modify(code, file, pkg, options) {
61
55
  if (!options.add && !options.namespace && !options.remove) {
62
- return sourceCode;
56
+ return code;
63
57
  }
64
- let add = options.add ? new Set(options.add) : null, found = imports.find(sourceFile, packageName), remove = options.remove ? new Set(options.remove) : null;
58
+ let { namespace } = options, add = options.add ? new Set(options.add) : null, found = imports.find(file, pkg);
65
59
  if (found.length === 0) {
66
60
  let statements = [];
67
61
  if (namespace) {
68
- statements.push(`import * as ${namespace} from '${packageName}';`);
62
+ statements.push(`import * as ${namespace} from '${pkg}';`);
69
63
  }
70
64
  if (add && add.size > 0) {
71
- statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
65
+ statements.push(`import { ${[...add].sort().join(', ')} } from '${pkg}';`);
72
66
  }
73
67
  if (statements.length === 0) {
74
- return sourceCode;
68
+ return code;
75
69
  }
76
- return statements.join('\n') + '\n' + sourceCode;
70
+ return statements.join('\n') + '\n' + code;
77
71
  }
78
- let specifiers = new Set();
72
+ let remove = options.remove ? new Set(options.remove) : null, specifiers = new Set();
79
73
  for (let i = 0, n = found.length; i < n; i++) {
80
74
  for (let [name, alias] of found[i].specifiers) {
81
75
  if (!remove || (!remove.has(name) && !remove.has(alias))) {
@@ -90,10 +84,10 @@ const modify = (sourceCode, sourceFile, packageName, options) => {
90
84
  }
91
85
  let statements = [];
92
86
  if (namespace) {
93
- statements.push(`import * as ${namespace} from '${packageName}';`);
87
+ statements.push(`import * as ${namespace} from '${pkg}';`);
94
88
  }
95
89
  if (specifiers.size > 0) {
96
- statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
90
+ statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${pkg}';`);
97
91
  }
98
92
  let replacements = [];
99
93
  for (let i = 0, n = found.length; i < n; i++) {
@@ -103,9 +97,10 @@ const modify = (sourceCode, sourceFile, packageName, options) => {
103
97
  start: found[i].start
104
98
  });
105
99
  }
106
- return replaceReverse(sourceCode, replacements);
107
- };
108
- const replaceReverse = (code, replacements) => {
100
+ return replaceReverse(code, replacements);
101
+ }
102
+ ;
103
+ function replaceReverse(code, replacements) {
109
104
  if (replacements.length === 0) {
110
105
  return code;
111
106
  }
@@ -116,12 +111,13 @@ const replaceReverse = (code, replacements) => {
116
111
  result = result.substring(0, r.start) + r.newText + result.substring(r.end);
117
112
  }
118
113
  return result;
119
- };
120
- const transform = (plugins, sourceCode, sourceFile, program, shared) => {
114
+ }
115
+ ;
116
+ const transform = (plugins, code, file, program, shared) => {
121
117
  if (plugins.length === 0) {
122
- return { changed: false, code: sourceCode, sourceFile };
118
+ return { changed: false, code, sourceFile: file };
123
119
  }
124
- let changed = false, currentCode = sourceCode, currentSourceFile = sourceFile;
120
+ let changed = false, currentCode = code, currentSourceFile = file;
125
121
  for (let i = 0, n = plugins.length; i < n; i++) {
126
122
  let plugin = plugins[i];
127
123
  if (plugin.patterns && !hasPattern(currentCode, plugin.patterns)) {
@@ -16,8 +16,7 @@ export default ({ name, onWatchChange, plugins }) => {
16
16
  return null;
17
17
  }
18
18
  try {
19
- let prog = program.get(root || ''), shared = contexts.get(root || '') ?? contexts.set(root || '', new Map()).get(root || ''), sourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);
20
- let result = coordinator.transform(plugins, code, sourceFile, prog, shared);
19
+ let result = coordinator.transform(plugins, code, ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true), program.get(root || ''), contexts.get(root || '') ?? contexts.set(root || '', new Map()).get(root || ''));
21
20
  if (!result.changed) {
22
21
  return null;
23
22
  }
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.2",
40
+ "version": "0.26.4",
41
41
  "scripts": {
42
42
  "build": "tsc && tsc-alias",
43
43
  "-": "-"
@@ -10,89 +10,72 @@ type CoordinatorResult = {
10
10
  };
11
11
 
12
12
 
13
- function applyImports(
14
- sourceCode: string,
15
- sourceFile: ts.SourceFile,
16
- intents: ImportIntent[]
17
- ): string {
18
- let result = sourceCode;
19
-
13
+ function applyImports(code: string, file: ts.SourceFile, intents: ImportIntent[]): string {
20
14
  for (let i = 0, n = intents.length; i < n; i++) {
21
15
  let intent = intents[i];
22
16
 
23
- result = modify(result, sourceFile, intent.package, {
17
+ code = modify(code, file, intent.package, {
24
18
  add: intent.add,
25
19
  namespace: intent.namespace,
26
20
  remove: intent.remove
27
21
  });
28
22
 
29
23
  if (i < n - 1) {
30
- sourceFile = ts.createSourceFile(
31
- sourceFile.fileName,
32
- result,
33
- sourceFile.languageVersion,
24
+ file = ts.createSourceFile(
25
+ file.fileName,
26
+ code,
27
+ file.languageVersion,
34
28
  true
35
29
  );
36
30
  }
37
31
  }
38
32
 
39
- return result;
33
+ return code;
40
34
  }
41
35
 
42
- function applyIntents(
43
- sourceCode: string,
44
- sourceFile: ts.SourceFile,
45
- intents: ReplacementIntent[]
46
- ): string {
36
+ function applyIntents(code: string, file: ts.SourceFile, intents: ReplacementIntent[]): string {
47
37
  if (intents.length === 0) {
48
- return sourceCode;
38
+ return code;
49
39
  }
50
40
 
51
41
  return replaceReverse(
52
- sourceCode,
42
+ code,
53
43
  intents.map(intent => ({
54
44
  end: intent.node.end,
55
- newText: intent.generate(sourceFile),
56
- start: intent.node.getStart(sourceFile)
45
+ newText: intent.generate(file),
46
+ start: intent.node.getStart(file)
57
47
  }))
58
48
  );
59
49
  }
60
50
 
61
- function applyPrepend(sourceCode: string, sourceFile: ts.SourceFile, prepend: string[]): string {
51
+ function applyPrepend(code: string, file: ts.SourceFile, prepend: string[]): string {
62
52
  if (prepend.length === 0) {
63
- return sourceCode;
64
- }
65
-
66
- let insertPos = findLastImportEnd(sourceFile),
67
- prependText = prepend.join('\n') + '\n';
68
-
69
- if (insertPos === 0) {
70
- return prependText + sourceCode;
53
+ return code;
71
54
  }
72
55
 
73
- return sourceCode.slice(0, insertPos) + '\n' + prependText + sourceCode.slice(insertPos);
74
- }
75
-
76
- function findLastImportEnd(sourceFile: ts.SourceFile): number {
77
- let lastEnd = 0;
56
+ let position = 0;
78
57
 
79
- for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
80
- let stmt = sourceFile.statements[i];
58
+ for (let i = 0, n = file.statements.length; i < n; i++) {
59
+ let stmt = file.statements[i];
81
60
 
82
61
  if (ts.isImportDeclaration(stmt)) {
83
- lastEnd = stmt.end;
62
+ position = stmt.end;
84
63
  }
85
64
  else {
86
65
  break;
87
66
  }
88
67
  }
89
68
 
90
- return lastEnd;
69
+ if (position === 0) {
70
+ return prepend.join('\n') + code;
71
+ }
72
+
73
+ return code.slice(0, position) + prepend.join('\n') + code.slice(position);
91
74
  }
92
75
 
93
- function hasPattern(sourceCode: string, patterns: string[]): boolean {
76
+ function hasPattern(code: string, patterns: string[]): boolean {
94
77
  for (let i = 0, n = patterns.length; i < n; i++) {
95
- if (sourceCode.indexOf(patterns[i]) !== -1) {
78
+ if (code.indexOf(patterns[i]) !== -1) {
96
79
  return true;
97
80
  }
98
81
  }
@@ -100,43 +83,36 @@ function hasPattern(sourceCode: string, patterns: string[]): boolean {
100
83
  return false;
101
84
  }
102
85
 
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
86
+ function modify(code: string, file: ts.SourceFile, pkg: string, options: ModifyOptions): string {
112
87
  if (!options.add && !options.namespace && !options.remove) {
113
- return sourceCode;
88
+ return code;
114
89
  }
115
90
 
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;
91
+ let { namespace } = options,
92
+ add = options.add ? new Set(options.add) : null,
93
+ found = imports.find(file, pkg);
119
94
 
120
95
  if (found.length === 0) {
121
96
  let statements: string[] = [];
122
97
 
123
98
  if (namespace) {
124
- statements.push(`import * as ${namespace} from '${packageName}';`);
99
+ statements.push(`import * as ${namespace} from '${pkg}';`);
125
100
  }
126
101
 
127
102
  if (add && add.size > 0) {
128
- statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
103
+ statements.push(`import { ${[...add].sort().join(', ')} } from '${pkg}';`);
129
104
  }
130
105
 
131
106
  if (statements.length === 0) {
132
- return sourceCode;
107
+ return code;
133
108
  }
134
109
 
135
- return statements.join('\n') + '\n' + sourceCode;
110
+ return statements.join('\n') + '\n' + code;
136
111
  }
137
112
 
138
- // Collect all non-removed specifiers from existing imports
139
- let specifiers = new Set<string>();
113
+ let remove = options.remove ? new Set(options.remove) : null,
114
+ // Collect all non-removed specifiers from existing imports
115
+ specifiers = new Set<string>();
140
116
 
141
117
  for (let i = 0, n = found.length; i < n; i++) {
142
118
  for (let [name, alias] of found[i].specifiers) {
@@ -156,11 +132,11 @@ const modify = (
156
132
  let statements: string[] = [];
157
133
 
158
134
  if (namespace) {
159
- statements.push(`import * as ${namespace} from '${packageName}';`);
135
+ statements.push(`import * as ${namespace} from '${pkg}';`);
160
136
  }
161
137
 
162
138
  if (specifiers.size > 0) {
163
- statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
139
+ statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${pkg}';`);
164
140
  }
165
141
 
166
142
  // Build replacements - replace first import, remove others
@@ -174,10 +150,10 @@ const modify = (
174
150
  });
175
151
  }
176
152
 
177
- return replaceReverse(sourceCode, replacements);
153
+ return replaceReverse(code, replacements);
178
154
  };
179
155
 
180
- const replaceReverse = (code: string, replacements: Replacement[]): string => {
156
+ function replaceReverse(code: string, replacements: Replacement[]): string {
181
157
  if (replacements.length === 0) {
182
158
  return code;
183
159
  }
@@ -202,18 +178,18 @@ const replaceReverse = (code: string, replacements: Replacement[]): string => {
202
178
  */
203
179
  const transform = (
204
180
  plugins: Plugin[],
205
- sourceCode: string,
206
- sourceFile: ts.SourceFile,
181
+ code: string,
182
+ file: ts.SourceFile,
207
183
  program: ts.Program,
208
184
  shared: SharedContext
209
185
  ): CoordinatorResult => {
210
186
  if (plugins.length === 0) {
211
- return { changed: false, code: sourceCode, sourceFile };
187
+ return { changed: false, code, sourceFile: file };
212
188
  }
213
189
 
214
190
  let changed = false,
215
- currentCode = sourceCode,
216
- currentSourceFile = sourceFile;
191
+ currentCode = code,
192
+ currentSourceFile = file;
217
193
 
218
194
  for (let i = 0, n = plugins.length; i < n; i++) {
219
195
  let plugin = plugins[i];
@@ -223,12 +199,12 @@ const transform = (
223
199
  }
224
200
 
225
201
  let result = plugin.transform({
226
- checker: program.getTypeChecker(),
227
- code: currentCode,
228
- program,
229
- shared,
230
- sourceFile: currentSourceFile
231
- });
202
+ checker: program.getTypeChecker(),
203
+ code: currentCode,
204
+ program,
205
+ shared,
206
+ sourceFile: currentSourceFile
207
+ });
232
208
 
233
209
  let hasChanges = (result.imports && result.imports.length > 0) ||
234
210
  (result.prepend && result.prepend.length > 0) ||
@@ -40,11 +40,13 @@ export default ({ name, onWatchChange, plugins }: VitePluginOptions) => {
40
40
  }
41
41
 
42
42
  try {
43
- let prog = program.get(root || ''),
44
- shared = contexts.get(root || '') ?? contexts.set(root || '', new Map()).get(root || '')!,
45
- sourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);
46
-
47
- let result = coordinator.transform(plugins, code, sourceFile, prog, shared);
43
+ let result = coordinator.transform(
44
+ plugins,
45
+ code,
46
+ ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true),
47
+ program.get(root || ''),
48
+ contexts.get(root || '') ?? contexts.set(root || '', new Map()).get(root || '')!
49
+ );
48
50
 
49
51
  if (!result.changed) {
50
52
  return null;