@suseejs/check 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suseejs/check",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Types check",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -46,5 +46,11 @@
46
46
  },
47
47
  "publishConfig": {
48
48
  "access": "public"
49
- }
49
+ },
50
+ "files": [
51
+ "dist",
52
+ "LICENSE",
53
+ "README.md",
54
+ "package.json"
55
+ ]
50
56
  }
@@ -1,6 +0,0 @@
1
- {
2
- "cSpell.words": [
3
- "suseejs",
4
- "tcolor"
5
- ]
6
- }
package/src/index.cts DELETED
@@ -1,244 +0,0 @@
1
- // cSpell:disable
2
- import path = require("node:path");
3
- import ts = require("typescript");
4
-
5
- interface DepsFile {
6
- file: string;
7
- content: string;
8
- }
9
-
10
- const jsesm_exts = [".js", ".mjs"];
11
- const jscjs_exts = [".cjs"];
12
- const tsesm_exts = [".ts", ".mts"];
13
- const tscjs_exts = [".cts"];
14
- const jsx_exts = [".jsx", ".tsx"];
15
- const all_exts = [
16
- ...jscjs_exts,
17
- ...jsesm_exts,
18
- ...tscjs_exts,
19
- ...tsesm_exts,
20
- ...jsx_exts,
21
- ];
22
-
23
- /**
24
- * Check types of given files and exit process if any errors are found.
25
- * @param deps List of files to check, where each file is an object with
26
- * `file` and `content` properties.
27
- * @param compilerOptions TypeScript compiler options.
28
- * @returns True if no errors are found, false otherwise.
29
- */
30
- function checkTypes(deps: DepsFile[], compilerOptions: ts.CompilerOptions) {
31
- if (!compilerOptions.noCheck) {
32
- console.time("types checked");
33
- const filePaths = deps.map((i) => i.file);
34
- let _err = false;
35
- // Create program
36
- const program = ts.createProgram(filePaths, compilerOptions);
37
- // Check each file individually for immediate feedback
38
- for (const filePath of filePaths) {
39
- const sourceFile = program.getSourceFile(filePath);
40
- if (!sourceFile) {
41
- console.error(
42
- `File not found: ${path.relative(process.cwd(), filePath)}`,
43
- );
44
- process.exit(1);
45
- }
46
-
47
- const diagnostics = [
48
- ...program.getSyntacticDiagnostics(sourceFile),
49
- ...program.getSemanticDiagnostics(sourceFile),
50
- ...program.getDeclarationDiagnostics(sourceFile),
51
- ];
52
-
53
- if (diagnostics.length > 0) {
54
- const formatHost: ts.FormatDiagnosticsHost = {
55
- getCurrentDirectory: () => process.cwd(),
56
- getCanonicalFileName: (fileName) => fileName,
57
- getNewLine: () => ts.sys.newLine,
58
- };
59
- console.error(
60
- ts.formatDiagnosticsWithColorAndContext(diagnostics, formatHost),
61
- );
62
- _err = true;
63
- }
64
- }
65
- if (_err) {
66
- process.exit(1);
67
- } else {
68
- console.timeEnd("types checked");
69
- return true;
70
- }
71
- }
72
- }
73
-
74
- /**
75
- * Check if all files in the given list have the same file extension.
76
- * @param deps List of files to check, where each file is an object with
77
- * `file` and `content` properties.
78
- * @returns An object with the following properties:
79
- * - `isNone`: True if none of the files have any of the supported
80
- * extensions (i.e., not .js, .jsx, .ts, .tsx, .cts, .mts,
81
- * .cjs).
82
- * - `isJsx`: True if all files have .jsx or .tsx extensions.
83
- * - `isCjs`: True if all files have .js or .cjs extensions.
84
- * - `isBoth`: True if all files have both .js and .ts extensions.
85
- * - `isJs`: True if all files have .js or .jsx extensions.
86
- * - `isTs`: True if all files have .ts or .tsx extensions.
87
- */
88
- function checkExtGroup(deps: DepsFile[]) {
89
- const exts = deps.map((dep) => {
90
- return path.extname(dep.file);
91
- });
92
- const jsesmSet = new Set(jsesm_exts);
93
- const jscjsSet = new Set(jscjs_exts);
94
- const tsesmSet = new Set(tsesm_exts);
95
- const tscjsSet = new Set(tscjs_exts);
96
- const jsxSet = new Set(jsx_exts);
97
- const allSet = new Set(all_exts);
98
- const isCjs =
99
- exts.every((i) => jscjsSet.has(i)) || exts.every((i) => tscjsSet.has(i));
100
- const isJsx = exts.every((i) => jsxSet.has(i));
101
- const isJs = exts.every((i) => jsesmSet.has(i));
102
- const isTs = exts.every((i) => tsesmSet.has(i));
103
- const isBoth = isJs && isTs;
104
- const isNone = !exts.every((i) => allSet.has(i));
105
- return {
106
- isNone,
107
- isJsx,
108
- isCjs,
109
- isBoth,
110
- isJs,
111
- isTs,
112
- };
113
- }
114
-
115
- /**
116
- * Check the module format of all the given files.
117
- * @param deps List of files to check, where each file is an object with
118
- * `file` and `content` properties.
119
- * @returns An object with the following properties:
120
- * - `unknowCount`: Number of files that could not be checked due to
121
- * errors.
122
- * - `cjsCount`: Number of files that have CommonJS module syntax.
123
- */
124
- function checkModuleFormat(deps: DepsFile[]) {
125
- let _esmCount = 0;
126
- let cjsCount = 0;
127
- let unknowCount = 0;
128
- for (const dep of deps) {
129
- try {
130
- // Create a TypeScript source file
131
- const sourceFile = ts.createSourceFile(
132
- dep.file,
133
- dep.content,
134
- ts.ScriptTarget.Latest,
135
- true,
136
- );
137
-
138
- let hasESMImports = false;
139
- let hasCommonJS = false;
140
- // Walk through the AST to detect module syntax
141
- function walk(node: ts.Node) {
142
- // Check for ESM import/export syntax
143
- if (
144
- ts.isImportDeclaration(node) ||
145
- ts.isImportEqualsDeclaration(node) ||
146
- ts.isExportDeclaration(node) ||
147
- ts.isExportSpecifier(node) ||
148
- ts.isExportAssignment(node)
149
- ) {
150
- hasESMImports = true;
151
- }
152
-
153
- // Check for export modifier on declarations
154
- if (
155
- (ts.isVariableStatement(node) ||
156
- ts.isFunctionDeclaration(node) ||
157
- ts.isInterfaceDeclaration(node) ||
158
- ts.isTypeAliasDeclaration(node) ||
159
- ts.isEnumDeclaration(node) ||
160
- ts.isClassDeclaration(node)) &&
161
- node.modifiers?.some(
162
- (mod) => mod.kind === ts.SyntaxKind.ExportKeyword,
163
- )
164
- ) {
165
- hasESMImports = true;
166
- }
167
-
168
- // Check for CommonJS require/exports
169
- if (ts.isCallExpression(node)) {
170
- if (
171
- ts.isIdentifier(node.expression) &&
172
- node.expression.text === "require" &&
173
- node.arguments.length > 0
174
- ) {
175
- hasCommonJS = true;
176
- }
177
- }
178
-
179
- // Check for module.exports or exports.xxx
180
- if (ts.isPropertyAccessExpression(node)) {
181
- const text = node.getText(sourceFile);
182
- if (
183
- text.startsWith("module.exports") ||
184
- text.startsWith("exports.")
185
- ) {
186
- hasCommonJS = true;
187
- }
188
- }
189
-
190
- // Continue walking the AST
191
- ts.forEachChild(node, walk);
192
- }
193
- walk(sourceFile);
194
-
195
- // Determine the module format based on what we found
196
- if (hasESMImports && !hasCommonJS) {
197
- _esmCount++;
198
- } else if (hasCommonJS && !hasESMImports) {
199
- cjsCount++;
200
- } else if (hasESMImports && hasCommonJS) {
201
- // Mixed - probably ESM with dynamic imports or similar
202
- _esmCount++;
203
- }
204
- } catch (error) {
205
- console.error(
206
- `Error checking module format for ${dep.file} : \n ${error}`,
207
- );
208
- unknowCount++;
209
- }
210
- } // loop
211
- //
212
- return { unknowCount, cjsCount };
213
- }
214
-
215
- /**
216
- * Check TypeScript files for errors and determine their module format.
217
- * @param deps List of files to check, where each file is an object with
218
- * `file` and `content` properties.
219
- * @param compilerOptions TypeScript compiler options.
220
- * @returns An object with the following properties:
221
- * - `isNone`: True if none of the files have any of the supported
222
- * extensions (i.e., not .js, .jsx, .ts, .tsx, .cts, .mts, .cjs).
223
- * - `isJsx`: True if all files have .jsx or .tsx extensions.
224
- * - `isCjs`: True if all files have .js or .cjs extensions.
225
- * - `isBoth`: True if all files have both .js and .ts extensions.
226
- * - `isJs`: True if all files have .js or .jsx extensions.
227
- * - `isTs`: True if all files have .ts or .tsx extensions.
228
- * - `unknowCount`: The number of files that could not be checked.
229
- * - `cjsCount`: The number of files that use CommonJS modules.
230
- * - `esmCount`: The number of files that use ES modules.
231
- */
232
- const check = (deps: DepsFile[], compilerOptions: ts.CompilerOptions) => {
233
- try {
234
- checkTypes(deps, compilerOptions);
235
- } catch (err) {
236
- if (err) throw new Error("Error when type checking");
237
- }
238
-
239
- const ce = checkExtGroup(deps);
240
- const cm = checkModuleFormat(deps);
241
- return { ...ce, ...cm };
242
- };
243
-
244
- export = check;
package/src/index.ts DELETED
@@ -1,244 +0,0 @@
1
- // cSpell:disable
2
- import path from "node:path";
3
- import ts from "typescript";
4
-
5
- interface DepsFile {
6
- file: string;
7
- content: string;
8
- }
9
-
10
- const jsesm_exts = [".js", ".mjs"];
11
- const jscjs_exts = [".cjs"];
12
- const tsesm_exts = [".ts", ".mts"];
13
- const tscjs_exts = [".cts"];
14
- const jsx_exts = [".jsx", ".tsx"];
15
- const all_exts = [
16
- ...jscjs_exts,
17
- ...jsesm_exts,
18
- ...tscjs_exts,
19
- ...tsesm_exts,
20
- ...jsx_exts,
21
- ];
22
-
23
- /**
24
- * Check types of given files and exit process if any errors are found.
25
- * @param deps List of files to check, where each file is an object with
26
- * `file` and `content` properties.
27
- * @param compilerOptions TypeScript compiler options.
28
- * @returns True if no errors are found, false otherwise.
29
- */
30
- function checkTypes(deps: DepsFile[], compilerOptions: ts.CompilerOptions) {
31
- if (!compilerOptions.noCheck) {
32
- console.time("types checked");
33
- const filePaths = deps.map((i) => i.file);
34
- let _err = false;
35
- // Create program
36
- const program = ts.createProgram(filePaths, compilerOptions);
37
- // Check each file individually for immediate feedback
38
- for (const filePath of filePaths) {
39
- const sourceFile = program.getSourceFile(filePath);
40
- if (!sourceFile) {
41
- console.error(
42
- `File not found: ${path.relative(process.cwd(), filePath)}`,
43
- );
44
- process.exit(1);
45
- }
46
-
47
- const diagnostics = [
48
- ...program.getSyntacticDiagnostics(sourceFile),
49
- ...program.getSemanticDiagnostics(sourceFile),
50
- ...program.getDeclarationDiagnostics(sourceFile),
51
- ];
52
-
53
- if (diagnostics.length > 0) {
54
- const formatHost: ts.FormatDiagnosticsHost = {
55
- getCurrentDirectory: () => process.cwd(),
56
- getCanonicalFileName: (fileName) => fileName,
57
- getNewLine: () => ts.sys.newLine,
58
- };
59
- console.error(
60
- ts.formatDiagnosticsWithColorAndContext(diagnostics, formatHost),
61
- );
62
- _err = true;
63
- }
64
- }
65
- if (_err) {
66
- process.exit(1);
67
- } else {
68
- console.timeEnd("types checked");
69
- return true;
70
- }
71
- }
72
- }
73
-
74
- /**
75
- * Check if all files in the given list have the same file extension.
76
- * @param deps List of files to check, where each file is an object with
77
- * `file` and `content` properties.
78
- * @returns An object with the following properties:
79
- * - `isNone`: True if none of the files have any of the supported
80
- * extensions (i.e., not .js, .jsx, .ts, .tsx, .cts, .mts,
81
- * .cjs).
82
- * - `isJsx`: True if all files have .jsx or .tsx extensions.
83
- * - `isCjs`: True if all files have .js or .cjs extensions.
84
- * - `isBoth`: True if all files have both .js and .ts extensions.
85
- * - `isJs`: True if all files have .js or .jsx extensions.
86
- * - `isTs`: True if all files have .ts or .tsx extensions.
87
- */
88
- function checkExtGroup(deps: DepsFile[]) {
89
- const exts = deps.map((dep) => {
90
- return path.extname(dep.file);
91
- });
92
- const jsesmSet = new Set(jsesm_exts);
93
- const jscjsSet = new Set(jscjs_exts);
94
- const tsesmSet = new Set(tsesm_exts);
95
- const tscjsSet = new Set(tscjs_exts);
96
- const jsxSet = new Set(jsx_exts);
97
- const allSet = new Set(all_exts);
98
- const isCjs =
99
- exts.every((i) => jscjsSet.has(i)) || exts.every((i) => tscjsSet.has(i));
100
- const isJsx = exts.every((i) => jsxSet.has(i));
101
- const isJs = exts.every((i) => jsesmSet.has(i));
102
- const isTs = exts.every((i) => tsesmSet.has(i));
103
- const isBoth = isJs && isTs;
104
- const isNone = !exts.every((i) => allSet.has(i));
105
- return {
106
- isNone,
107
- isJsx,
108
- isCjs,
109
- isBoth,
110
- isJs,
111
- isTs,
112
- };
113
- }
114
-
115
- /**
116
- * Check the module format of all the given files.
117
- * @param deps List of files to check, where each file is an object with
118
- * `file` and `content` properties.
119
- * @returns An object with the following properties:
120
- * - `unknowCount`: Number of files that could not be checked due to
121
- * errors.
122
- * - `cjsCount`: Number of files that have CommonJS module syntax.
123
- */
124
- function checkModuleFormat(deps: DepsFile[]) {
125
- let _esmCount = 0;
126
- let cjsCount = 0;
127
- let unknowCount = 0;
128
- for (const dep of deps) {
129
- try {
130
- // Create a TypeScript source file
131
- const sourceFile = ts.createSourceFile(
132
- dep.file,
133
- dep.content,
134
- ts.ScriptTarget.Latest,
135
- true,
136
- );
137
-
138
- let hasESMImports = false;
139
- let hasCommonJS = false;
140
- // Walk through the AST to detect module syntax
141
- function walk(node: ts.Node) {
142
- // Check for ESM import/export syntax
143
- if (
144
- ts.isImportDeclaration(node) ||
145
- ts.isImportEqualsDeclaration(node) ||
146
- ts.isExportDeclaration(node) ||
147
- ts.isExportSpecifier(node) ||
148
- ts.isExportAssignment(node)
149
- ) {
150
- hasESMImports = true;
151
- }
152
-
153
- // Check for export modifier on declarations
154
- if (
155
- (ts.isVariableStatement(node) ||
156
- ts.isFunctionDeclaration(node) ||
157
- ts.isInterfaceDeclaration(node) ||
158
- ts.isTypeAliasDeclaration(node) ||
159
- ts.isEnumDeclaration(node) ||
160
- ts.isClassDeclaration(node)) &&
161
- node.modifiers?.some(
162
- (mod) => mod.kind === ts.SyntaxKind.ExportKeyword,
163
- )
164
- ) {
165
- hasESMImports = true;
166
- }
167
-
168
- // Check for CommonJS require/exports
169
- if (ts.isCallExpression(node)) {
170
- if (
171
- ts.isIdentifier(node.expression) &&
172
- node.expression.text === "require" &&
173
- node.arguments.length > 0
174
- ) {
175
- hasCommonJS = true;
176
- }
177
- }
178
-
179
- // Check for module.exports or exports.xxx
180
- if (ts.isPropertyAccessExpression(node)) {
181
- const text = node.getText(sourceFile);
182
- if (
183
- text.startsWith("module.exports") ||
184
- text.startsWith("exports.")
185
- ) {
186
- hasCommonJS = true;
187
- }
188
- }
189
-
190
- // Continue walking the AST
191
- ts.forEachChild(node, walk);
192
- }
193
- walk(sourceFile);
194
-
195
- // Determine the module format based on what we found
196
- if (hasESMImports && !hasCommonJS) {
197
- _esmCount++;
198
- } else if (hasCommonJS && !hasESMImports) {
199
- cjsCount++;
200
- } else if (hasESMImports && hasCommonJS) {
201
- // Mixed - probably ESM with dynamic imports or similar
202
- _esmCount++;
203
- }
204
- } catch (error) {
205
- console.error(
206
- `Error checking module format for ${dep.file} : \n ${error}`,
207
- );
208
- unknowCount++;
209
- }
210
- } // loop
211
- //
212
- return { unknowCount, cjsCount };
213
- }
214
-
215
- /**
216
- * Check TypeScript files for errors and determine their module format.
217
- * @param deps List of files to check, where each file is an object with
218
- * `file` and `content` properties.
219
- * @param compilerOptions TypeScript compiler options.
220
- * @returns An object with the following properties:
221
- * - `isNone`: True if none of the files have any of the supported
222
- * extensions (i.e., not .js, .jsx, .ts, .tsx, .cts, .mts, .cjs).
223
- * - `isJsx`: True if all files have .jsx or .tsx extensions.
224
- * - `isCjs`: True if all files have .js or .cjs extensions.
225
- * - `isBoth`: True if all files have both .js and .ts extensions.
226
- * - `isJs`: True if all files have .js or .jsx extensions.
227
- * - `isTs`: True if all files have .ts or .tsx extensions.
228
- * - `unknowCount`: The number of files that could not be checked.
229
- * - `cjsCount`: The number of files that use CommonJS modules.
230
- * - `esmCount`: The number of files that use ES modules.
231
- */
232
- const check = (deps: DepsFile[], compilerOptions: ts.CompilerOptions) => {
233
- try {
234
- checkTypes(deps, compilerOptions);
235
- } catch (err) {
236
- if (err) throw new Error("Error when type checking");
237
- }
238
-
239
- const ce = checkExtGroup(deps);
240
- const cm = checkModuleFormat(deps);
241
- return { ...ce, ...cm };
242
- };
243
-
244
- export default check;
package/tsconfig.json DELETED
@@ -1,44 +0,0 @@
1
- {
2
- // Visit https://aka.ms/tsconfig to read more about this file
3
- "compilerOptions": {
4
- // File Layout
5
- "rootDir": "./src",
6
- "outDir": "./dist",
7
-
8
- // Environment Settings
9
- // See also https://aka.ms/tsconfig/module
10
- "module": "nodenext",
11
- "target": "esnext",
12
- //"types": [],
13
- // For nodejs:
14
- "lib": ["esnext"],
15
- "types": ["node"],
16
- // and npm install -D @types/node
17
-
18
- // Other Outputs
19
- "sourceMap": true,
20
- "declaration": true,
21
- "declarationMap": true,
22
-
23
- // Stricter Typechecking Options
24
- "noUncheckedIndexedAccess": true,
25
- "exactOptionalPropertyTypes": true,
26
-
27
- // Style Options
28
- // "noImplicitReturns": true,
29
- // "noImplicitOverride": true,
30
- // "noUnusedLocals": true,
31
- // "noUnusedParameters": true,
32
- // "noFallthroughCasesInSwitch": true,
33
- // "noPropertyAccessFromIndexSignature": true,
34
-
35
- // Recommended Options
36
- "strict": true,
37
- "jsx": "react-jsx",
38
- "verbatimModuleSyntax": true,
39
- "isolatedModules": true,
40
- "noUncheckedSideEffectImports": true,
41
- "moduleDetection": "force",
42
- "skipLibCheck": true
43
- }
44
- }