Skip to content

Commit 89aff9f

Browse files
authored
Merge code from TSCO CLI (#111)
1 parent 1570d1e commit 89aff9f

5 files changed

Lines changed: 25 additions & 11 deletions

File tree

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ See [TypeScript Code Organizer Command Line Interface](https://www.npmjs.com/pac
180180

181181
- **configuration is no longer compatible with 1.x.x as version 2.x.x uses a configuration file**
182182
- merged functionality with [TypeScript Class Organizer CLI](https://www.npmjs.com/package/tsco-cli)
183-
184183
- added support for configuration files
185184
- added support for organizing import statements
186185
- added ability to include/exclude files
@@ -189,11 +188,8 @@ See [TypeScript Code Organizer Command Line Interface](https://www.npmjs.com/pac
189188
- added more fine-grained configuration for module organization
190189
- added more fine-grained configuration for interface organization
191190
- added more fine-grained configuration for type alias organization
192-
193191
- added ability to create a default configuration file
194-
195192
- added support for regular expressions and globs when putting members above or below the member type group
196-
197193
- added support for sorting member type groups asc, desc or no sorting
198194

199195
### 2.0.7
@@ -219,8 +215,13 @@ See [TypeScript Code Organizer Command Line Interface](https://www.npmjs.com/pac
219215
- add support for exported enums, exported interfaces, exported classes and exported types (courtesy of [Carlos Jesús Huchim Ahumada](https://github.com/huchim))
220216
- add improved import statement grouping
221217

222-
## 2.0.12
218+
### 2.0.12
219+
223220
- add missing file namespace import grouping
224221
- add checking relative import source casing
225222
- add support for declaration dependency order resolution
226223
- fix issue with file name casing
224+
225+
### 2.0.13
226+
227+
- fix issue with access modifiers not being correctly updated

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.12",
5+
"version": "2.0.13",
66
"publisher": "aljazsim",
77
"author": {
88
"name": "aljazsim",

src/tsco-cli/helpers/string-helper.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import wcmatch from "wildcard-match";
22

33
import { space } from "../source-code/source-code-constants";
44

5-
// #region Exported Functions (3)
5+
// #region Exported Functions (4)
66

77
export function convertPascalCaseToTitleCase(value: string)
88
{
@@ -16,6 +16,11 @@ export function convertPascalCaseToTitleCase(value: string)
1616
return value;
1717
}
1818

19+
export function escapeRegex(regex: string)
20+
{
21+
return regex.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
22+
}
23+
1924
export function matchRegEx(regex: string, text: string)
2025
{
2126
if (regex && regex.length > 0)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export const endRegion = "#endregion";
1515
export const anythingRegex = ".+";
1616
export const spacesRegex = "\\s*";
1717
export const newLineRegex = `${newLine}|${cr}|${lf}`;
18+

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { PropertyNode } from "../elements/property-node";
66
import { SetterNode } from "../elements/setter-node";
77
import { AccessModifier } from "../enums/access-modifier";
88
import { WriteModifier } from "../enums/write-modifier";
9+
import { escapeRegex } from "../helpers/string-helper";
910
import { anythingRegex, endRegion, newLine, newLineRegex, space, spacesRegex, startRegion } from "./source-code-constants";
1011

1112
export class SourceCode
@@ -81,7 +82,7 @@ export class SourceCode
8182
const getAbstract = (isAbstract: boolean) => isAbstract ? "abstract " : "";
8283
const getReadOnly = (writeMode: WriteModifier) => writeMode === WriteModifier.readOnly ? "readonly " : "";
8384
const getString = (strings: string[]) => ["private"].concat(strings).filter(s => s !== "").map(s => s.trim()).join(space);
84-
const getRegex = (strings: string[]) => new RegExp(strings.filter(s => s !== "").map(s => s.trim()).join(spacesRegex));
85+
const getRegex = (strings: string[]) => new RegExp(strings.filter(s => s !== "").map(s => escapeRegex(s.trim())).join(spacesRegex));
8586
const removeHash = (name: string) => name.substring(1);
8687

8788
if (node.name.startsWith("#") && node.accessModifier === AccessModifier.private)
@@ -122,7 +123,10 @@ export class SourceCode
122123
const codeAfterDecorators = node.sourceCode.substring(codeDecoratorsEndIndex);
123124
const newNodeSourceCode = codeDecorators + codeAfterDecorators.replace(regex, replaceWith);
124125

125-
this.sourceCode = this.sourceCode.replace(node.sourceCode, newNodeSourceCode); // replace node declaration
126+
const sourceCodeBefore = this.sourceCode.substring(0, this.sourceCode.indexOf(node.sourceCode));
127+
const sourceCodeAfter = this.sourceCode.substring(this.sourceCode.indexOf(node.sourceCode) + node.sourceCode.length);
128+
129+
this.sourceCode = sourceCodeBefore + newNodeSourceCode + sourceCodeAfter; // replace node declaration
126130
this.sourceCode.replaceAll(`this.${node.name}`, `this.${removeHash(node.name)}`); // replace all references
127131
}
128132
}
@@ -135,7 +139,7 @@ export class SourceCode
135139
const getAbstract = (isAbstract: boolean) => isAbstract ? "abstract " : "";
136140
const getReadOnly = (writeMode: WriteModifier) => writeMode === WriteModifier.readOnly ? "readonly " : "";
137141
const getString = (strings: string[]) => ["public"].concat(strings).filter(s => s !== "").map(s => s.trim()).join(space);
138-
const getRegex = (strings: string[]) => new RegExp(strings.filter(s => s !== "").map(s => s.trim()).join(spacesRegex));
142+
const getRegex = (strings: string[]) => new RegExp(strings.filter(s => s !== "").map(s => escapeRegex(s.trim())).join(spacesRegex));
139143

140144
if (!node.name.startsWith("#") && node.accessModifier === null)
141145
{
@@ -175,7 +179,10 @@ export class SourceCode
175179
const codeAfterDecorators = node.sourceCode.substring(codeDecoratorsEndIndex);
176180
const newNodeSourceCode = codeDecorators + codeAfterDecorators.replace(regex, replaceWith);
177181

178-
this.sourceCode = this.sourceCode.replace(node.sourceCode, newNodeSourceCode);
182+
const sourceCodeBefore = this.sourceCode.substring(0, this.sourceCode.indexOf(node.sourceCode));
183+
const sourceCodeAfter = this.sourceCode.substring(this.sourceCode.indexOf(node.sourceCode) + node.sourceCode.length);
184+
185+
this.sourceCode = sourceCodeBefore + newNodeSourceCode + sourceCodeAfter;
179186
}
180187
}
181188
}

0 commit comments

Comments
 (0)