@nsis/dent 0.2.2 → 0.3.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
@@ -13,11 +13,11 @@
13
13
  ## Usage
14
14
 
15
15
  ```ts
16
- import { Dent } from '@nsis/dent';
16
+ import { createFormatter } from '@nsis/dent';
17
17
 
18
- const dent = new Dent(/* user options */);
18
+ const format = createFormatter(/* user options */);
19
19
 
20
- dent.format(`
20
+ format(`
21
21
  # Look ma, no indentation
22
22
  Name "Demo"
23
23
  Section
package/dist/index.mjs CHANGED
@@ -75,31 +75,25 @@ var rules = {
75
75
 
76
76
  // src/main.ts
77
77
  var defaultIndentation = 2;
78
- var Dent = class {
79
- options;
80
- constructor(options = {}) {
81
- this.options = {
82
- endOfLines: platform() === "win32" ? "crlf" : "lf",
83
- indentSize: defaultIndentation,
84
- trimEmptyLines: true,
85
- useTabs: true,
86
- ...options
87
- };
88
- if (options.useTabs === true && options.indentSize) {
89
- throw Error("The indentSize option can only be mixed with useTabs");
90
- }
91
- if (options.useTabs === false && options.indentSize) {
92
- if (isNaN(options.indentSize) || options.indentSize <= 0) {
93
- throw Error("The indentSize option expects a positive integer");
94
- }
78
+ function createFormatter(options = {}) {
79
+ const mergedOptions = {
80
+ endOfLines: platform() === "win32" ? "crlf" : "lf",
81
+ indentSize: defaultIndentation,
82
+ trimEmptyLines: true,
83
+ useTabs: true,
84
+ ...options
85
+ };
86
+ if (mergedOptions.useTabs === false && mergedOptions.indentSize) {
87
+ if (isNaN(mergedOptions.indentSize) || mergedOptions.indentSize <= 0) {
88
+ throw Error("The indentSize option expects a positive integer");
95
89
  }
96
90
  }
97
- format(fileContents) {
91
+ function format(fileContents) {
98
92
  let indentationLevel = 0;
99
93
  let switchIndentationLevel = 0;
100
- const lineEndings = this.detectEOL(fileContents);
94
+ const lineEndings = detectEOL(fileContents);
101
95
  const formattedLines = [];
102
- const lines = this.options.trimEmptyLines === true ? fileContents.trim().replaceAll(/^(\s*\r?\n){2,}/gm, lineEndings).split(lineEndings) : fileContents.split(lineEndings);
96
+ const lines = mergedOptions.trimEmptyLines === true ? fileContents.trim().replaceAll(/^(\s*\r?\n){2,}/gm, lineEndings).split(lineEndings) : fileContents.split(lineEndings);
103
97
  lines.forEach((line) => {
104
98
  const keyword = line.trim().split(" ").at(0) ?? "";
105
99
  if (keyword.toLowerCase() === "${Switch}") {
@@ -108,37 +102,37 @@ var Dent = class {
108
102
  switch (true) {
109
103
  case keyword.toLowerCase() === "${EndSwitch}":
110
104
  indentationLevel = switchIndentationLevel;
111
- formattedLines.push(this.appendLine(line, indentationLevel));
105
+ formattedLines.push(appendLine(line, indentationLevel));
112
106
  indentationLevel = indentationLevel === 0 ? 0 : indentationLevel - 1;
113
107
  break;
114
108
  case rules.specialIndenters.includes(keyword.toLowerCase()):
115
- formattedLines.push(this.appendLine(line, indentationLevel - 1));
109
+ formattedLines.push(appendLine(line, indentationLevel - 1));
116
110
  break;
117
111
  case rules.specialDedenters.includes(keyword.toLowerCase()):
118
- formattedLines.push(this.appendLine(line, indentationLevel));
112
+ formattedLines.push(appendLine(line, indentationLevel));
119
113
  indentationLevel--;
120
114
  break;
121
115
  case rules.indenters.includes(keyword.toLowerCase()):
122
- formattedLines.push(this.appendLine(line, indentationLevel));
116
+ formattedLines.push(appendLine(line, indentationLevel));
123
117
  indentationLevel++;
124
118
  break;
125
119
  case rules.dedenters.includes(keyword.toLowerCase()):
126
120
  indentationLevel = indentationLevel === 0 ? 0 : indentationLevel - 1;
127
- formattedLines.push(this.appendLine(line, indentationLevel));
121
+ formattedLines.push(appendLine(line, indentationLevel));
128
122
  break;
129
123
  default:
130
- formattedLines.push(this.appendLine(line, indentationLevel));
124
+ formattedLines.push(appendLine(line, indentationLevel));
131
125
  break;
132
126
  }
133
127
  });
134
128
  return formattedLines.join(lineEndings);
135
129
  }
136
- appendLine(line, level) {
137
- return line.length ? `${this.getIndentChar().repeat(level)}${line.trim()}` : "";
130
+ function appendLine(line, level) {
131
+ return line.length ? `${getIndentChar(level)}${line.trim()}` : "";
138
132
  }
139
- detectEOL(input) {
140
- if (this.options.endOfLines) {
141
- return this.options.endOfLines === "crlf" ? "\r\n" : "\n";
133
+ function detectEOL(input) {
134
+ if (mergedOptions.endOfLines) {
135
+ return mergedOptions.endOfLines === "crlf" ? "\r\n" : "\n";
142
136
  }
143
137
  const newLine = detectNewline(input);
144
138
  if (newLine !== void 0) {
@@ -147,10 +141,11 @@ var Dent = class {
147
141
  return platform() === "win32" ? "\r\n" : "\n";
148
142
  }
149
143
  }
150
- getIndentChar() {
151
- return this.options.useTabs ? " ".repeat(this.options.indentSize || defaultIndentation) : " ".repeat(this.options.indentSize || defaultIndentation);
144
+ function getIndentChar(level) {
145
+ return (mergedOptions.useTabs ? " ".repeat(mergedOptions.indentSize || defaultIndentation) : " ".repeat(mergedOptions.indentSize || defaultIndentation)).repeat(level);
152
146
  }
153
- };
147
+ return format;
148
+ }
154
149
  export {
155
- Dent
150
+ createFormatter
156
151
  };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@nsis/dent",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "description": "An opinionated code formatter for NSIS scripts",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
- "build": "esbuild src/main.ts --bundle --platform=node --outfile=dist/index.mjs --format=esm --external:detect-newline",
7
+ "build": "esbuild src/main.ts --bundle --platform=node --outfile=dist/index.mjs --format=esm --external:detect-newline --external:globby",
8
8
  "dev": "npm run start",
9
9
  "fix": "eslint --fix ./src",
10
10
  "lint:json": "eslint ./*.json --ignore-path .gitignore",
@@ -12,21 +12,19 @@
12
12
  "lint": "npm-run-all --parallel lint:*",
13
13
  "prepack": "npm run build",
14
14
  "start": "npm run build -- --watch",
15
- "test": "uvu tests",
15
+ "test": "uvu -r tsm tests",
16
16
  "prepare": "husky install"
17
17
  },
18
18
  "files": [
19
19
  "dist/",
20
+ "types/",
20
21
  "LICENSE",
22
+ "package.json",
21
23
  "README.md"
22
24
  ],
23
25
  "type": "module",
24
- "types": "./src/main.ts",
25
- "exports": {
26
- ".": {
27
- "import": "./dist/index.mjs"
28
- }
29
- },
26
+ "typings": "types/",
27
+ "exports": "./dist/index.mjs",
30
28
  "engines": {
31
29
  "node": "^14.13.1 || >=16.0.0"
32
30
  },
@@ -39,21 +37,21 @@
39
37
  "formatter"
40
38
  ],
41
39
  "dependencies": {
42
- "detect-newline": "^4.0.0",
43
- "globby": "^13.1.4"
40
+ "detect-newline": "^4.0.0"
44
41
  },
45
42
  "devDependencies": {
46
43
  "@types/node": "^20.2.1",
47
- "@typescript-eslint/eslint-plugin": "^5.59.6",
48
- "@typescript-eslint/parser": "^5.59.6",
49
- "esbuild": "^0.17.19",
50
- "eslint": "^8.41.0",
44
+ "@typescript-eslint/eslint-plugin": "^6.0.0",
45
+ "@typescript-eslint/parser": "^6.0.0",
46
+ "esbuild": "^0.18.13",
47
+ "eslint": "^8.45.0",
51
48
  "eslint-plugin-json": "^3.1.0",
52
49
  "husky": "^8.0.3",
53
- "lint-staged": "^13.2.2",
54
- "npm-run-all2": "^6.0.5",
55
- "tslib": "^2.5.2",
56
- "typescript": "^5.0.4",
50
+ "lint-staged": "^13.2.3",
51
+ "npm-run-all2": "^6.0.6",
52
+ "tslib": "^2.6.0",
53
+ "tsm": "^2.3.0",
54
+ "typescript": "^5.1.6",
57
55
  "uvu": "^0.5.6"
58
56
  },
59
57
  "lint-staged": {
@@ -0,0 +1,14 @@
1
+ declare namespace NsisDent {
2
+ function createFormatter(options?: Options): (fileContents: string) => string;
3
+
4
+ type Options = {
5
+ endOfLines?: 'crlf' | 'lf';
6
+ indentSize?: number;
7
+ trimEmptyLines?: boolean;
8
+ useTabs?: boolean;
9
+ }
10
+ }
11
+
12
+ export = NsisDent;
13
+ export as namespace NsisDent;
14
+
package/dist/.mjs DELETED
@@ -1,152 +0,0 @@
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
- };
@@ -1 +0,0 @@
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;QACT,SAAS;QACT,WAAW;QACX,cAAc;QACd,eAAe;QACf,UAAU;QACV,aAAa;QACb,cAAc;QACd,SAAS;QACT,YAAY;QACZ,aAAa;KACb,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;AAC3B,IAAA,gBAAgB,EAAE;QACjB,UAAU;KACV,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,WA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