Skip to content

Commit 36b4fef

Browse files
authored
Transfer code from cli type organizer
1 parent 12cf70a commit 36b4fef

8 files changed

Lines changed: 87 additions & 39 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,8 @@ See [TypeScript Code Organizer Command Line Interface](https://www.npmjs.com/pac
206206
- fix issue with import statements with comments
207207
- fix issue with sorting members with dependencies
208208
- fix organizing imports with type aliases
209+
210+
### 2.0.9
211+
212+
- fix issue with removing empty lines in code expressions
213+
- fix issue with using typed imports

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "tsco",
33
"displayName": "TypeScript Code Organizer",
44
"description": "TypeScript Code Organizer for VS Code",
5-
"version": "2.0.8",
5+
"version": "2.0.9",
66
"publisher": "aljazsim",
77
"author": {
88
"name": "aljazsim",

src/tsco-cli/configuration/configuration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export class Configuration
5353
if (configurationFilePath && await fileExists(configurationFilePath))
5454
{
5555
configuration = JSON.parse(await readFile(configurationFilePath));
56+
57+
console.log(`tsco using configuration ${configurationFilePath}`);
5658
}
5759
else
5860
{

src/tsco-cli/elements/import-node.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import { ElementNode } from "./element-node";
55

66
export class ImportNode extends ElementNode
77
{
8-
// #region Properties (6)
8+
// #region Properties (7)
99

1010
public readonly name: string;
1111

1212
public isModuleReference = false;
1313
public nameBinding: string | null = null;
14-
public namedImports: { type: boolean, name: string, alias: string | null }[] | null = null;
14+
public namedImports: { typeOnly: boolean, name: string, alias: string | null }[] | null = null;
1515
public namespace: string | null = null;
1616
public source: string;
17+
public type = false;
1718

1819
// #endregion Properties
1920

@@ -29,7 +30,7 @@ export class ImportNode extends ElementNode
2930
this.source = this.getSource(importDeclaration);
3031

3132
const isRelativeReference = this.source.startsWith(".") || this.source.startsWith("..");
32-
const isAbsoluteReference = !isRelativeReference && (this.source.indexOf("/") > -1 || this.source.indexOf("\\") > -1);
33+
const isAbsoluteReference = !isRelativeReference && !this.source.startsWith("@") && (this.source.indexOf("/") > -1 || this.source.indexOf("\\") > -1);
3334

3435
this.isModuleReference = !isRelativeReference && !isAbsoluteReference;
3536
}
@@ -59,9 +60,9 @@ export class ImportNode extends ElementNode
5960
}
6061
else if (ts.isNamedImports(node.importClause?.namedBindings))
6162
{
62-
this.namedImports = distinct(node.importClause?.namedBindings.elements.map(e => (
63+
this.namedImports = distinct(node.importClause!.namedBindings.elements.map(e => (
6364
{
64-
type: e.isTypeOnly,
65+
typeOnly: node.importClause!.isTypeOnly || e.isTypeOnly,
6566
name: e.name.text.trim(),
6667
alias: e.propertyName?.text.trim() ?? null
6768
})));

src/tsco-cli/source-code/source-code-analyzer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class SourceCodeAnalyzer
118118
elements = elements.concat(this.traverseSyntaxTree(childNode, sourceFile, configuration));
119119
}
120120
}
121-
else if (!ts.isEmptyStatement(node))
121+
else if (!ts.isEmptyStatement(node) && node.kind != ts.SyntaxKind.EndOfFileToken)
122122
{
123123
// expression
124124
elements.push(new ExpressionNode(sourceFile, node));

src/tsco-cli/source-code/source-code-organizer.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ElementNodeGroup } from "../elements/element-node-group";
88
import { ImportNode } from "../elements/import-node";
99
import { VariableNode } from "../elements/variable-node";
1010
import { ModuleMemberType } from "../enums/module-member-type";
11-
import { distinct, except, intersect, remove } from "../helpers/array-helper";
11+
import { except, intersect, remove } from "../helpers/array-helper";
1212
import { compareStrings } from "../helpers/comparing-helper";
1313
import { getFileExtension } from "../helpers/file-system-helper";
1414
import { getClasses, getEnums, getExpressions, getFunctions, getImports, getInterfaces, getTypeAliases, getVariables, order } from "../helpers/node-helper";
@@ -22,7 +22,7 @@ export class SourceCodeOrganizer
2222

2323
public static async organizeSourceCode(sourceCodeFilePath: string, sourceCode: string, configuration: Configuration)
2424
{
25-
const ignoreComment1Regex = new RegExp("//\\s*tsco:ignore");
25+
const ignoreComment1Regex = new RegExp("//\\s*tsco\\s*:\\s*ignore");
2626
const ignoreComment2Regex = new RegExp("//\\s*<auto-generated\\s*/>");
2727

2828
if (sourceCode &&
@@ -32,6 +32,7 @@ export class SourceCodeOrganizer
3232
try
3333
{
3434
const sourceCodeWithoutRegions = new SourceCode(sourceCode);
35+
3536
sourceCodeWithoutRegions.removeRegions(); // strip regions, they will get re-generated
3637

3738
const sourceFile = ts.createSourceFile(sourceCodeFilePath, sourceCodeWithoutRegions.toString(), ts.ScriptTarget.Latest, false, ts.ScriptKind.TS);
@@ -95,7 +96,29 @@ export class SourceCodeOrganizer
9596
(!firstImport.namespace && !secondImport.namespace))
9697
{
9798
firstImport.nameBinding = firstImport.nameBinding ?? secondImport.nameBinding;
98-
firstImport.namedImports = distinct((firstImport.namedImports ?? []).concat(secondImport.namedImports ?? []));
99+
firstImport.namedImports = firstImport.namedImports ?? [];
100+
101+
for (const secondNamedImport of secondImport.namedImports ?? [])
102+
{
103+
const match = firstImport.namedImports.find(ni => ni.name === secondNamedImport.name);
104+
105+
if (!match)
106+
{
107+
firstImport.namedImports.push(secondNamedImport);
108+
}
109+
else
110+
{
111+
if (!match.typeOnly && secondNamedImport.typeOnly)
112+
{
113+
match.typeOnly = true;
114+
}
115+
116+
if (match.alias == null && !secondNamedImport.alias)
117+
{
118+
match.alias = secondNamedImport.alias;
119+
}
120+
}
121+
}
99122

100123
remove(imports, secondImport);
101124
}
@@ -281,7 +304,7 @@ export class SourceCodeOrganizer
281304
{
282305
for (const import1 of imports.filter(i => i.isEmptyReference))
283306
{
284-
if (import1.isModuleReference || !getFileExtension(import1.source) || getFileExtension(import1.source) === ".ts" || getFileExtension(import1.source) === ".js")
307+
if (import1.isModuleReference || !getFileExtension(import1.source) || getFileExtension(import1.source) === ".ts" || getFileExtension(import1.source) === "")
285308
{
286309
remove(imports, import1);
287310
}

src/tsco-cli/source-code/source-code-printer.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AccessorNode } from "../elements/accessor-node";
44
import { ClassNode } from "../elements/class-node";
55
import { ElementNode } from "../elements/element-node";
66
import { ElementNodeGroup } from "../elements/element-node-group";
7+
import { ExpressionNode } from "../elements/expression-node";
78
import { FunctionNode } from "../elements/function-node";
89
import { GetterNode } from "../elements/getter-node";
910
import { GetterSignatureNode } from "../elements/getter-signature-node";
@@ -70,15 +71,15 @@ export class SourceCodePrinter
7071

7172
if (beforeMembers.length > 0)
7273
{
73-
nodeSourceCode.add(beforeMembers);
74+
nodeSourceCode.addAfter(beforeMembers);
7475
nodeSourceCode.addNewLineAfter();
7576
}
7677

77-
nodeSourceCode.add(members);
78+
nodeSourceCode.addAfter(members);
7879

7980
if (afterMembers.length > 0)
8081
{
81-
nodeSourceCode.add(afterMembers);
82+
nodeSourceCode.addAfter(afterMembers);
8283
}
8384

8485
return nodeSourceCode;
@@ -92,6 +93,17 @@ export class SourceCodePrinter
9293
const nameBinding = node.nameBinding;
9394
const namespace = node.namespace;
9495
let sourceCode = ""
96+
let namedImportsSourceCode = "";
97+
98+
if (namedImports.length > 0)
99+
{
100+
const allTypeOnly = namedImports.every(ni => ni.typeOnly);
101+
102+
namedImportsSourceCode += allTypeOnly ? "type " : "";
103+
namedImportsSourceCode += "{ ";
104+
namedImportsSourceCode += namedImports.map(ni => (ni.typeOnly && !allTypeOnly ? "type " : "") + (ni.alias ? (ni.alias + " as ") : "") + ni.name).join(", ");
105+
namedImportsSourceCode += " }";
106+
}
95107

96108
if (nameBinding)
97109
{
@@ -101,7 +113,7 @@ export class SourceCodePrinter
101113
}
102114
else if (namedImports.length > 0)
103115
{
104-
sourceCode = `import ${nameBinding}, { ${namedImports.map(ni => (ni.type ? "type " : "") + (ni.alias ? (ni.alias + " as ") : "") + ni.name).join(", ")} } from ${quote}${source}${quote};`;
116+
sourceCode = `import ${nameBinding}, ${namedImportsSourceCode} from ${quote}${source}${quote};`;
105117
}
106118
else
107119
{
@@ -114,7 +126,7 @@ export class SourceCodePrinter
114126
}
115127
else if (namedImports.length > 0)
116128
{
117-
sourceCode = `import { ${namedImports.map(ni => (ni.type ? "type " : "") + (ni.alias ? (ni.alias + " as ") : "") + ni.name).join(", ")} } from ${quote}${source}${quote};`;
129+
sourceCode = `import ${namedImportsSourceCode} from ${quote}${source}${quote};`;
118130
}
119131
else
120132
{
@@ -138,15 +150,15 @@ export class SourceCodePrinter
138150

139151
if (beforeMembers.length > 0)
140152
{
141-
nodeSourceCode.add(beforeMembers);
153+
nodeSourceCode.addAfter(beforeMembers);
142154
nodeSourceCode.addNewLineAfter();
143155
}
144156

145-
nodeSourceCode.add(members);
157+
nodeSourceCode.addAfter(members);
146158

147159
if (afterMembers.length > 0)
148160
{
149-
nodeSourceCode.add(afterMembers);
161+
nodeSourceCode.addAfter(afterMembers);
150162
}
151163

152164
return nodeSourceCode;
@@ -198,7 +210,7 @@ export class SourceCodePrinter
198210
const nodeGroupNodeCount = nodeGroup.getNodeCount();
199211

200212
// print subgroups
201-
nodeGroupSourceCode.add(this.printNodeGroups(nodeGroup.nodeSubGroups, configuration));
213+
nodeGroupSourceCode.addAfter(this.printNodeGroups(nodeGroup.nodeSubGroups, configuration));
202214

203215
// print nodes within a group
204216
for (const node of nodeGroup.nodes)
@@ -229,7 +241,8 @@ export class SourceCodePrinter
229241
node instanceof FunctionNode ||
230242
node instanceof MethodNode ||
231243
(node instanceof PropertyNode && node.writeMode !== WriteModifier.readOnly && node.isArrowFunction && configuration.classes.members.treatArrowFunctionPropertiesAsMethods) ||
232-
(node instanceof PropertyNode && node.writeMode === WriteModifier.readOnly && node.isArrowFunction && configuration.classes.members.treatArrowFunctionReadOnlyPropertiesAsMethods))
244+
(node instanceof PropertyNode && node.writeMode === WriteModifier.readOnly && node.isArrowFunction && configuration.classes.members.treatArrowFunctionReadOnlyPropertiesAsMethods) ||
245+
node instanceof ExpressionNode)
233246
{
234247
if (nodeGroup.nodes.indexOf(node) > 0)
235248
{
@@ -249,7 +262,7 @@ export class SourceCodePrinter
249262
}
250263
}
251264

252-
nodeGroupSourceCode.add(nodeSourceCode);
265+
nodeGroupSourceCode.addAfter(nodeSourceCode);
253266
}
254267

255268
if (nodeGroup.isRegion && nodeGroup.regionConfiguration?.addRegions)
@@ -277,7 +290,7 @@ export class SourceCodePrinter
277290
sourceCode.addNewLineBefore();
278291
}
279292

280-
nodeGroupsSourceCode.add(sourceCode);
293+
nodeGroupsSourceCode.addAfter(sourceCode);
281294
}
282295

283296
return nodeGroupsSourceCode;
@@ -308,15 +321,15 @@ export class SourceCodePrinter
308321

309322
if (beforeMembers.length > 0)
310323
{
311-
nodeSourceCode.add(beforeMembers);
324+
nodeSourceCode.addAfter(beforeMembers);
312325
nodeSourceCode.addNewLineAfter();
313326
}
314327

315-
nodeSourceCode.add(members);
328+
nodeSourceCode.addAfter(members);
316329

317330
if (afterMembers.length > 0)
318331
{
319-
nodeSourceCode.add(afterMembers);
332+
nodeSourceCode.addAfter(afterMembers);
320333
}
321334

322335
return nodeSourceCode;

src/tsco-cli/source-code/source-code.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ export class SourceCode
2323

2424
// #endregion Constructors
2525

26-
// #region Public Methods (12)
26+
// #region Public Methods (13)
2727

28-
public add(newSourceCode: string | SourceCode, where: "before" | "after" = "after")
28+
public addAfter(newSourceCode: string | SourceCode)
2929
{
30-
if (newSourceCode instanceof SourceCode && where === "after")
30+
if (newSourceCode instanceof SourceCode)
3131
{
3232
this.sourceCode = this.sourceCode + newSourceCode.toString();
3333
}
34-
else if (newSourceCode instanceof SourceCode && where === "before")
34+
else
3535
{
36-
this.sourceCode = newSourceCode.toString() + this.sourceCode;
36+
this.sourceCode = this.sourceCode + newSourceCode;
3737
}
38-
else if (where === "after")
38+
}
39+
40+
public addBefore(newSourceCode: string | SourceCode)
41+
{
42+
if (newSourceCode instanceof SourceCode)
3943
{
40-
this.sourceCode = this.sourceCode + newSourceCode;
44+
this.sourceCode = newSourceCode.toString() + this.sourceCode;
4145
}
42-
else if (where === "before")
46+
else
4347
{
4448
this.sourceCode = newSourceCode + this.sourceCode;
4549
}
@@ -53,13 +57,13 @@ export class SourceCode
5357

5458
this.sourceCode = comment.trimEnd();
5559
this.addNewLineAfter();
56-
this.add(temp);
60+
this.addAfter(temp);
5761
}
5862
}
5963

6064
public addNewLineAfter()
6165
{
62-
this.add(this.newLine);
66+
this.addAfter(this.newLine);
6367
}
6468

6569
public addNewLineAfterIf(condition: boolean)
@@ -72,7 +76,7 @@ export class SourceCode
7276

7377
public addNewLineBefore()
7478
{
75-
this.add(this.newLine, "before");
79+
this.addBefore(this.newLine);
7680
}
7781

7882
public addPrivateModifierIfStartingWithHash(node: PropertyNode | MethodNode | AccessorNode | GetterNode | SetterNode)
@@ -204,9 +208,9 @@ export class SourceCode
204208
this.sourceCode = region;
205209
this.addNewLineAfter();
206210
this.addNewLineAfter();
207-
this.add(code);
211+
this.addAfter(code);
208212
this.addNewLineAfter();
209-
this.add(endregion);
213+
this.addAfter(endregion);
210214
this.addNewLineAfter();
211215
}
212216

0 commit comments

Comments
 (0)