@nsis/dent 0.1.0 → 0.1.1

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.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # @nsis/dent
2
2
 
3
- > An opinionated code formatter for NSIS script
3
+ > An opinionated code formatter for NSIS scripts
4
4
 
5
- [![License](https://img.shields.io/npm/l/@nsis/dent?style=for-the-badge)]([https://github.com/idleberg/node-dent/releases](https://github.com/idleberg/node-dent/actions))
6
- [![npm](https://img.shields.io/npm/v/nsis/dent?style=for-the-badge)](https://www.npmjs.org/package/@nsis/dent)
7
- [![Build](https://img.shields.io/github/actions/workflow/status/idleberg/node-dent/default.yml?style=for-the-badge)]([https://github.com/idleberg/node-dent/releases](https://github.com/idleberg/node-dent/actions))
5
+ [![License](https://img.shields.io/github/license/idleberg/node-dent?color=blue&style=for-the-badge)](https://github.com/idleberg/node-dent/blob/main/LICENSE)
6
+ [![npm](https://img.shields.io/npm/v/@nsis/dent?style=for-the-badge)](https://www.npmjs.org/package/@nsis/dent)
7
+ [![Build](https://img.shields.io/github/actions/workflow/status/idleberg/node-dent/default.yml?style=for-the-badge)](https://github.com/idleberg/node-dent/actions)
8
8
 
9
9
  ## Installation
10
10
 
@@ -13,3 +13,4 @@
13
13
  ## License
14
14
 
15
15
  This work is licensed under [The MIT License](LICENSE)
16
+
package/dist/.mjs ADDED
@@ -0,0 +1,152 @@
1
+ // src/main.ts
2
+ import { detectNewline } from "detect-newline";
3
+ import { platform } from "node:os";
4
+
5
+ // src/rules.ts
6
+ var rules = {
7
+ indenters: [
8
+ "!if",
9
+ "!ifdef",
10
+ "!ifmacrodef",
11
+ "!ifmacrondef",
12
+ "!ifndef",
13
+ "!macro",
14
+ "${Case}",
15
+ "${Case2}",
16
+ "${Case3}",
17
+ "${Case4}",
18
+ "${Case5}",
19
+ "${CaseElse}",
20
+ "${Default}",
21
+ "${Do}",
22
+ "${DoUntil}",
23
+ "${DoWhile}",
24
+ "${For}",
25
+ "${ForEach}",
26
+ "${If}",
27
+ "${IfNot}",
28
+ "${MementoSection}",
29
+ "${MementoUnselectedSection}",
30
+ "${Select}",
31
+ "${Switch}",
32
+ "${Unless}",
33
+ "Function",
34
+ "PageEx",
35
+ "Section",
36
+ "SectionGroup"
37
+ ].map((i) => i.toLowerCase()),
38
+ dedenters: [
39
+ "!endif",
40
+ "!macroend",
41
+ "${EndIf}",
42
+ "${EndSelect}",
43
+ "${EndSwitch}",
44
+ "${EndWhile}",
45
+ "${Loop}",
46
+ "${LoopUntil}",
47
+ "${LoopWhile}",
48
+ "${MementoSectionEnd}",
49
+ "${Next}",
50
+ "${While}",
51
+ "FunctionEnd",
52
+ "PageExEnd",
53
+ "SectionEnd",
54
+ "SectionGroupEnd"
55
+ ].map((i) => i.toLowerCase()),
56
+ specialIndenters: [
57
+ "!else",
58
+ "!elseif",
59
+ "${Else}",
60
+ "${ElseIf}",
61
+ "${ElseIfNot}",
62
+ "${ElseUnless}",
63
+ "${AndIf}",
64
+ "${AndIfNot}",
65
+ "${AndUnless}",
66
+ "${OrIf}",
67
+ "${OrIfNot}",
68
+ "${OrUnless}"
69
+ ].map((i) => i.toLowerCase()),
70
+ specialDedenters: [
71
+ "${Break}"
72
+ ].map((i) => i.toLowerCase())
73
+ };
74
+
75
+ // src/main.ts
76
+ var Dent = class {
77
+ options = {
78
+ indentSize: 2,
79
+ trimEmptyLines: true,
80
+ useTabs: true
81
+ };
82
+ constructor(options = {}) {
83
+ if (options.useTabs === true && options.indentSize) {
84
+ throw Error("The indentSize option can only be mixed with useTabs");
85
+ }
86
+ if (options.useTabs === false && options.indentSize) {
87
+ if (isNaN(options.indentSize) || options.indentSize <= 0) {
88
+ throw Error("The indentSize option expects a positive integer");
89
+ }
90
+ }
91
+ this.options = {
92
+ ...this.options,
93
+ ...options
94
+ };
95
+ }
96
+ format(fileContents) {
97
+ let indentationLevel = 0;
98
+ let switchIndentationLevel = 0;
99
+ const lineEndings = this.detectEOL(fileContents);
100
+ const formattedLines = [];
101
+ const lines = this.options.trimEmptyLines === true ? fileContents.trim().replaceAll(/^(\s*\r?\n){2,}/gm, lineEndings).split(lineEndings) : fileContents.split(lineEndings);
102
+ lines.forEach((line) => {
103
+ const keyword = line.trim().split(" ").at(0) ?? "";
104
+ if (keyword.toLowerCase() === "${Switch}") {
105
+ switchIndentationLevel = indentationLevel;
106
+ }
107
+ switch (true) {
108
+ case keyword.toLowerCase() === "${EndSwitch}":
109
+ indentationLevel = switchIndentationLevel;
110
+ formattedLines.push(this.appendLine(line, indentationLevel));
111
+ indentationLevel = indentationLevel === 0 ? 0 : indentationLevel - 1;
112
+ break;
113
+ case rules.specialIndenters.includes(keyword.toLowerCase()):
114
+ formattedLines.push(this.appendLine(line, indentationLevel - 1));
115
+ break;
116
+ case rules.specialDedenters.includes(keyword.toLowerCase()):
117
+ formattedLines.push(this.appendLine(line, indentationLevel));
118
+ indentationLevel--;
119
+ break;
120
+ case rules.indenters.includes(keyword.toLowerCase()):
121
+ formattedLines.push(this.appendLine(line, indentationLevel));
122
+ indentationLevel++;
123
+ break;
124
+ case rules.dedenters.includes(keyword.toLowerCase()):
125
+ indentationLevel = indentationLevel === 0 ? 0 : indentationLevel - 1;
126
+ formattedLines.push(this.appendLine(line, indentationLevel));
127
+ break;
128
+ default:
129
+ formattedLines.push(this.appendLine(line, indentationLevel));
130
+ break;
131
+ }
132
+ });
133
+ return formattedLines.join(lineEndings);
134
+ }
135
+ appendLine(line, level) {
136
+ return line.length ? `${this.getIndentChar(this.options).repeat(level)}${line.trim()}` : "";
137
+ }
138
+ detectEOL(input) {
139
+ const newLine = detectNewline(input);
140
+ if (newLine !== void 0) {
141
+ return newLine;
142
+ } else {
143
+ return platform() === "win32" ? "\r\n" : "\n";
144
+ }
145
+ }
146
+ getIndentChar(options) {
147
+ return options.useTabs ? " " : " ".repeat(options.indentSize);
148
+ }
149
+ };
150
+ export {
151
+ Dent
152
+ };
package/dist/index.mjs ADDED
@@ -0,0 +1,153 @@
1
+ // src/main.ts
2
+ import { detectNewline } from "detect-newline";
3
+ import { platform } from "node:os";
4
+
5
+ // src/rules.ts
6
+ var rules = {
7
+ indenters: [
8
+ "!if",
9
+ "!ifdef",
10
+ "!ifmacrodef",
11
+ "!ifmacrondef",
12
+ "!ifndef",
13
+ "!macro",
14
+ "${Case}",
15
+ "${Case2}",
16
+ "${Case3}",
17
+ "${Case4}",
18
+ "${Case5}",
19
+ "${CaseElse}",
20
+ "${Default}",
21
+ "${Do}",
22
+ "${DoUntil}",
23
+ "${DoWhile}",
24
+ "${For}",
25
+ "${ForEach}",
26
+ "${If}",
27
+ "${IfNot}",
28
+ "${MementoSection}",
29
+ "${MementoUnselectedSection}",
30
+ "${Select}",
31
+ "${Switch}",
32
+ "${Unless}",
33
+ "Function",
34
+ "PageEx",
35
+ "Section",
36
+ "SectionGroup"
37
+ ].map((i) => i.toLowerCase()),
38
+ dedenters: [
39
+ "!endif",
40
+ "!macroend",
41
+ "${EndIf}",
42
+ "${EndSelect}",
43
+ "${EndSwitch}",
44
+ "${EndWhile}",
45
+ "${Loop}",
46
+ "${LoopUntil}",
47
+ "${LoopWhile}",
48
+ "${MementoSectionEnd}",
49
+ "${Next}",
50
+ "${While}",
51
+ "FunctionEnd",
52
+ "PageExEnd",
53
+ "SectionEnd",
54
+ "SectionGroupEnd"
55
+ ].map((i) => i.toLowerCase()),
56
+ // These follow indenters, but aren't indented themselves
57
+ specialIndenters: [
58
+ "!else",
59
+ "!elseif",
60
+ "${Else}",
61
+ "${ElseIf}",
62
+ "${ElseIfNot}",
63
+ "${ElseUnless}",
64
+ "${AndIf}",
65
+ "${AndIfNot}",
66
+ "${AndUnless}",
67
+ "${OrIf}",
68
+ "${OrIfNot}",
69
+ "${OrUnless}"
70
+ ].map((i) => i.toLowerCase()),
71
+ specialDedenters: [
72
+ "${Break}"
73
+ ].map((i) => i.toLowerCase())
74
+ };
75
+
76
+ // src/main.ts
77
+ var Dent = class {
78
+ options = {
79
+ indentSize: 2,
80
+ trimEmptyLines: true,
81
+ useTabs: true
82
+ };
83
+ constructor(options = {}) {
84
+ if (options.useTabs === true && options.indentSize) {
85
+ throw Error("The indentSize option can only be mixed with useTabs");
86
+ }
87
+ if (options.useTabs === false && options.indentSize) {
88
+ if (isNaN(options.indentSize) || options.indentSize <= 0) {
89
+ throw Error("The indentSize option expects a positive integer");
90
+ }
91
+ }
92
+ this.options = {
93
+ ...this.options,
94
+ ...options
95
+ };
96
+ }
97
+ format(fileContents) {
98
+ let indentationLevel = 0;
99
+ let switchIndentationLevel = 0;
100
+ const lineEndings = this.detectEOL(fileContents);
101
+ const formattedLines = [];
102
+ const lines = this.options.trimEmptyLines === true ? fileContents.trim().replaceAll(/^(\s*\r?\n){2,}/gm, lineEndings).split(lineEndings) : fileContents.split(lineEndings);
103
+ lines.forEach((line) => {
104
+ const keyword = line.trim().split(" ").at(0) ?? "";
105
+ if (keyword.toLowerCase() === "${Switch}") {
106
+ switchIndentationLevel = indentationLevel;
107
+ }
108
+ switch (true) {
109
+ case keyword.toLowerCase() === "${EndSwitch}":
110
+ indentationLevel = switchIndentationLevel;
111
+ formattedLines.push(this.appendLine(line, indentationLevel));
112
+ indentationLevel = indentationLevel === 0 ? 0 : indentationLevel - 1;
113
+ break;
114
+ case rules.specialIndenters.includes(keyword.toLowerCase()):
115
+ formattedLines.push(this.appendLine(line, indentationLevel - 1));
116
+ break;
117
+ case rules.specialDedenters.includes(keyword.toLowerCase()):
118
+ formattedLines.push(this.appendLine(line, indentationLevel));
119
+ indentationLevel--;
120
+ break;
121
+ case rules.indenters.includes(keyword.toLowerCase()):
122
+ formattedLines.push(this.appendLine(line, indentationLevel));
123
+ indentationLevel++;
124
+ break;
125
+ case rules.dedenters.includes(keyword.toLowerCase()):
126
+ indentationLevel = indentationLevel === 0 ? 0 : indentationLevel - 1;
127
+ formattedLines.push(this.appendLine(line, indentationLevel));
128
+ break;
129
+ default:
130
+ formattedLines.push(this.appendLine(line, indentationLevel));
131
+ break;
132
+ }
133
+ });
134
+ return formattedLines.join(lineEndings);
135
+ }
136
+ appendLine(line, level) {
137
+ return line.length ? `${this.getIndentChar(this.options).repeat(level)}${line.trim()}` : "";
138
+ }
139
+ detectEOL(input) {
140
+ const newLine = detectNewline(input);
141
+ if (newLine !== void 0) {
142
+ return newLine;
143
+ } else {
144
+ return platform() === "win32" ? "\r\n" : "\n";
145
+ }
146
+ }
147
+ getIndentChar(options) {
148
+ return options.useTabs ? " " : " ".repeat(options.indentSize);
149
+ }
150
+ };
151
+ export {
152
+ Dent
153
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/rules.ts","../src/main.ts"],"sourcesContent":[null,null],"names":[],"mappings":";;;AAAA;AACO,MAAM,KAAK,GAAG;AACpB,IAAA,SAAS,EAAE;QACV,KAAK;QACL,QAAQ;QACR,aAAa;QACb,cAAc;QACd,SAAS;QACT,QAAQ;QACR,SAAS;QACT,UAAU;QACV,UAAU;QACV,UAAU;QACV,UAAU;QACV,aAAa;QACb,YAAY;QACZ,OAAO;QACP,YAAY;QACZ,YAAY;QACZ,QAAQ;QACR,YAAY;QACZ,OAAO;QACP,UAAU;QACV,mBAAmB;QACnB,6BAA6B;QAC7B,WAAW;QACX,WAAW;QACX,WAAW;QACX,UAAU;QACV,QAAQ;QACR,SAAS;QACT,cAAc;KACd,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;AAC3B,IAAA,SAAS,EAAE;QACV,QAAQ;QACR,WAAW;QACX,UAAU;QACV,cAAc;QACd,cAAc;QACd,aAAa;QACb,SAAS;QACT,cAAc;QACd,cAAc;QACd,sBAAsB;QACtB,SAAS;QACT,UAAU;QACV,aAAa;QACb,WAAW;QACX,YAAY;QACZ,iBAAiB;KACjB,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;;AAE3B,IAAA,gBAAgB,EAAE;QACjB,OAAO;QACP,SAAS;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OAAO;AAC7B,kBAAE,MAAM;kBACN,IAAI,EACL;AACF,SAAA;KACD;AAEO,IAAA,aAAa,CAAC,OAAO,EAAA;QAC5B,QAAO,OAAO,CAAC,OAAO;AACrB,cAAE,IAAI;cACJ,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAC/B;KACF;AACD;;;;"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nsis/dent",
3
- "version": "0.1.0",
4
- "description": "An opinionated code formatter for NSIS script",
3
+ "version": "0.1.1",
4
+ "description": "An opinionated code formatter for NSIS scripts",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
7
  "build": "esbuild src/main.ts --bundle --platform=node --outfile=dist/index.mjs --format=esm --external:detect-newline",
@@ -16,7 +16,7 @@
16
16
  "prepare": "husky install"
17
17
  },
18
18
  "files": [
19
- "types/",
19
+ "dist/",
20
20
  "LICENSE",
21
21
  "README.md"
22
22
  ],