@esportsplus/typescript 0.26.2 → 0.26.3

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,79 +1,74 @@
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, text = prepend.join('\n') + '\n';
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 text + code;
43
+ }
44
+ return code.slice(0, position) + '\n' + text + 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) => {
54
+ function modify(code, file, pkg, options) {
60
55
  let { namespace } = options;
61
56
  if (!options.add && !options.namespace && !options.remove) {
62
- return sourceCode;
57
+ return code;
63
58
  }
64
- let add = options.add ? new Set(options.add) : null, found = imports.find(sourceFile, packageName), remove = options.remove ? new Set(options.remove) : null;
59
+ let add = options.add ? new Set(options.add) : null, found = imports.find(file, pkg), remove = options.remove ? new Set(options.remove) : null;
65
60
  if (found.length === 0) {
66
61
  let statements = [];
67
62
  if (namespace) {
68
- statements.push(`import * as ${namespace} from '${packageName}';`);
63
+ statements.push(`import * as ${namespace} from '${pkg}';`);
69
64
  }
70
65
  if (add && add.size > 0) {
71
- statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
66
+ statements.push(`import { ${[...add].sort().join(', ')} } from '${pkg}';`);
72
67
  }
73
68
  if (statements.length === 0) {
74
- return sourceCode;
69
+ return code;
75
70
  }
76
- return statements.join('\n') + '\n' + sourceCode;
71
+ return statements.join('\n') + '\n' + code;
77
72
  }
78
73
  let specifiers = new Set();
79
74
  for (let i = 0, n = found.length; i < n; i++) {
@@ -90,10 +85,10 @@ const modify = (sourceCode, sourceFile, packageName, options) => {
90
85
  }
91
86
  let statements = [];
92
87
  if (namespace) {
93
- statements.push(`import * as ${namespace} from '${packageName}';`);
88
+ statements.push(`import * as ${namespace} from '${pkg}';`);
94
89
  }
95
90
  if (specifiers.size > 0) {
96
- statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
91
+ statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${pkg}';`);
97
92
  }
98
93
  let replacements = [];
99
94
  for (let i = 0, n = found.length; i < n; i++) {
@@ -103,9 +98,10 @@ const modify = (sourceCode, sourceFile, packageName, options) => {
103
98
  start: found[i].start
104
99
  });
105
100
  }
106
- return replaceReverse(sourceCode, replacements);
107
- };
108
- const replaceReverse = (code, replacements) => {
101
+ return replaceReverse(code, replacements);
102
+ }
103
+ ;
104
+ function replaceReverse(code, replacements) {
109
105
  if (replacements.length === 0) {
110
106
  return code;
111
107
  }
@@ -116,12 +112,13 @@ const replaceReverse = (code, replacements) => {
116
112
  result = result.substring(0, r.start) + r.newText + result.substring(r.end);
117
113
  }
118
114
  return result;
119
- };
120
- const transform = (plugins, sourceCode, sourceFile, program, shared) => {
115
+ }
116
+ ;
117
+ const transform = (plugins, code, file, program, shared) => {
121
118
  if (plugins.length === 0) {
122
- return { changed: false, code: sourceCode, sourceFile };
119
+ return { changed: false, code, sourceFile: file };
123
120
  }
124
- let changed = false, currentCode = sourceCode, currentSourceFile = sourceFile;
121
+ let changed = false, currentCode = code, currentSourceFile = file;
125
122
  for (let i = 0, n = plugins.length; i < n; i++) {
126
123
  let plugin = plugins[i];
127
124
  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.3",
41
41
  "scripts": {
42
42
  "build": "tsc && tsc-alias",
43
43
  "-": "-"
@@ -10,89 +10,73 @@ 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;
53
+ return code;
64
54
  }
65
55
 
66
- let insertPos = findLastImportEnd(sourceFile),
67
- prependText = prepend.join('\n') + '\n';
56
+ let position = 0,
57
+ text = prepend.join('\n') + '\n';
68
58
 
69
- if (insertPos === 0) {
70
- return prependText + sourceCode;
71
- }
72
-
73
- return sourceCode.slice(0, insertPos) + '\n' + prependText + sourceCode.slice(insertPos);
74
- }
75
-
76
- function findLastImportEnd(sourceFile: ts.SourceFile): number {
77
- let lastEnd = 0;
78
-
79
- for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
80
- let stmt = sourceFile.statements[i];
59
+ for (let i = 0, n = file.statements.length; i < n; i++) {
60
+ let stmt = file.statements[i];
81
61
 
82
62
  if (ts.isImportDeclaration(stmt)) {
83
- lastEnd = stmt.end;
63
+ position = stmt.end;
84
64
  }
85
65
  else {
86
66
  break;
87
67
  }
88
68
  }
89
69
 
90
- return lastEnd;
70
+ if (position === 0) {
71
+ return text + code;
72
+ }
73
+
74
+ return code.slice(0, position) + '\n' + text + code.slice(position);
91
75
  }
92
76
 
93
- function hasPattern(sourceCode: string, patterns: string[]): boolean {
77
+ function hasPattern(code: string, patterns: string[]): boolean {
94
78
  for (let i = 0, n = patterns.length; i < n; i++) {
95
- if (sourceCode.indexOf(patterns[i]) !== -1) {
79
+ if (code.indexOf(patterns[i]) !== -1) {
96
80
  return true;
97
81
  }
98
82
  }
@@ -100,39 +84,34 @@ function hasPattern(sourceCode: string, patterns: string[]): boolean {
100
84
  return false;
101
85
  }
102
86
 
103
- const modify = (
104
- sourceCode: string,
105
- sourceFile: ts.SourceFile,
106
- packageName: string,
107
- options: ModifyOptions
108
- ): string => {
87
+ function modify(code: string, file: ts.SourceFile, pkg: string, options: ModifyOptions): string {
109
88
  let { namespace } = options;
110
89
 
111
90
  // Fast path: nothing to change
112
91
  if (!options.add && !options.namespace && !options.remove) {
113
- return sourceCode;
92
+ return code;
114
93
  }
115
94
 
116
95
  let add = options.add ? new Set(options.add) : null,
117
- found = imports.find(sourceFile, packageName),
96
+ found = imports.find(file, pkg),
118
97
  remove = options.remove ? new Set(options.remove) : null;
119
98
 
120
99
  if (found.length === 0) {
121
100
  let statements: string[] = [];
122
101
 
123
102
  if (namespace) {
124
- statements.push(`import * as ${namespace} from '${packageName}';`);
103
+ statements.push(`import * as ${namespace} from '${pkg}';`);
125
104
  }
126
105
 
127
106
  if (add && add.size > 0) {
128
- statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
107
+ statements.push(`import { ${[...add].sort().join(', ')} } from '${pkg}';`);
129
108
  }
130
109
 
131
110
  if (statements.length === 0) {
132
- return sourceCode;
111
+ return code;
133
112
  }
134
113
 
135
- return statements.join('\n') + '\n' + sourceCode;
114
+ return statements.join('\n') + '\n' + code;
136
115
  }
137
116
 
138
117
  // Collect all non-removed specifiers from existing imports
@@ -156,11 +135,11 @@ const modify = (
156
135
  let statements: string[] = [];
157
136
 
158
137
  if (namespace) {
159
- statements.push(`import * as ${namespace} from '${packageName}';`);
138
+ statements.push(`import * as ${namespace} from '${pkg}';`);
160
139
  }
161
140
 
162
141
  if (specifiers.size > 0) {
163
- statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
142
+ statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${pkg}';`);
164
143
  }
165
144
 
166
145
  // Build replacements - replace first import, remove others
@@ -174,10 +153,10 @@ const modify = (
174
153
  });
175
154
  }
176
155
 
177
- return replaceReverse(sourceCode, replacements);
156
+ return replaceReverse(code, replacements);
178
157
  };
179
158
 
180
- const replaceReverse = (code: string, replacements: Replacement[]): string => {
159
+ function replaceReverse(code: string, replacements: Replacement[]): string {
181
160
  if (replacements.length === 0) {
182
161
  return code;
183
162
  }
@@ -202,18 +181,18 @@ const replaceReverse = (code: string, replacements: Replacement[]): string => {
202
181
  */
203
182
  const transform = (
204
183
  plugins: Plugin[],
205
- sourceCode: string,
206
- sourceFile: ts.SourceFile,
184
+ code: string,
185
+ file: ts.SourceFile,
207
186
  program: ts.Program,
208
187
  shared: SharedContext
209
188
  ): CoordinatorResult => {
210
189
  if (plugins.length === 0) {
211
- return { changed: false, code: sourceCode, sourceFile };
190
+ return { changed: false, code, sourceFile: file };
212
191
  }
213
192
 
214
193
  let changed = false,
215
- currentCode = sourceCode,
216
- currentSourceFile = sourceFile;
194
+ currentCode = code,
195
+ currentSourceFile = file;
217
196
 
218
197
  for (let i = 0, n = plugins.length; i < n; i++) {
219
198
  let plugin = plugins[i];
@@ -223,12 +202,12 @@ const transform = (
223
202
  }
224
203
 
225
204
  let result = plugin.transform({
226
- checker: program.getTypeChecker(),
227
- code: currentCode,
228
- program,
229
- shared,
230
- sourceFile: currentSourceFile
231
- });
205
+ checker: program.getTypeChecker(),
206
+ code: currentCode,
207
+ program,
208
+ shared,
209
+ sourceFile: currentSourceFile
210
+ });
232
211
 
233
212
  let hasChanges = (result.imports && result.imports.length > 0) ||
234
213
  (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;