@suseejs/check 0.1.0 → 0.1.2

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/dist/index.cjs ADDED
@@ -0,0 +1,203 @@
1
+ "use strict";
2
+ // cSpell:disable
3
+ const path = require("node:path");
4
+ const ts = require("typescript");
5
+ const jsesm_exts = [".js", ".mjs"];
6
+ const jscjs_exts = [".cjs"];
7
+ const tsesm_exts = [".ts", ".mts"];
8
+ const tscjs_exts = [".cts"];
9
+ const jsx_exts = [".jsx", ".tsx"];
10
+ const all_exts = [
11
+ ...jscjs_exts,
12
+ ...jsesm_exts,
13
+ ...tscjs_exts,
14
+ ...tsesm_exts,
15
+ ...jsx_exts,
16
+ ];
17
+ /**
18
+ * Check types of given files and exit process if any errors are found.
19
+ * @param deps List of files to check, where each file is an object with
20
+ * `file` and `content` properties.
21
+ * @param compilerOptions TypeScript compiler options.
22
+ * @returns True if no errors are found, false otherwise.
23
+ */
24
+ function checkTypes(deps, compilerOptions) {
25
+ if (!compilerOptions.noCheck) {
26
+ console.time("types checked");
27
+ const filePaths = deps.map((i) => i.file);
28
+ let _err = false;
29
+ // Create program
30
+ const program = ts.createProgram(filePaths, compilerOptions);
31
+ // Check each file individually for immediate feedback
32
+ for (const filePath of filePaths) {
33
+ const sourceFile = program.getSourceFile(filePath);
34
+ if (!sourceFile) {
35
+ console.error(`File not found: ${path.relative(process.cwd(), filePath)}`);
36
+ process.exit(1);
37
+ }
38
+ const diagnostics = [
39
+ ...program.getSyntacticDiagnostics(sourceFile),
40
+ ...program.getSemanticDiagnostics(sourceFile),
41
+ ...program.getDeclarationDiagnostics(sourceFile),
42
+ ];
43
+ if (diagnostics.length > 0) {
44
+ const formatHost = {
45
+ getCurrentDirectory: () => process.cwd(),
46
+ getCanonicalFileName: (fileName) => fileName,
47
+ getNewLine: () => ts.sys.newLine,
48
+ };
49
+ console.error(ts.formatDiagnosticsWithColorAndContext(diagnostics, formatHost));
50
+ _err = true;
51
+ }
52
+ }
53
+ if (_err) {
54
+ process.exit(1);
55
+ }
56
+ else {
57
+ console.timeEnd("types checked");
58
+ return true;
59
+ }
60
+ }
61
+ }
62
+ /**
63
+ * Check the file extensions of given dependencies.
64
+ * @param deps List of files to check, where each file is an object with
65
+ * `file` and `content` properties.
66
+ * @returns True if no unsupported file extensions are found, false otherwise.
67
+ * @throws If unsupported file extensions are found, the function will throw an error and exit the process.
68
+ */
69
+ function checkExtGroup(deps) {
70
+ const exts = deps.map((dep) => {
71
+ return path.extname(dep.file);
72
+ });
73
+ const jsesmSet = new Set(jsesm_exts);
74
+ const jscjsSet = new Set(jscjs_exts);
75
+ const tsesmSet = new Set(tsesm_exts);
76
+ const tscjsSet = new Set(tscjs_exts);
77
+ const jsxSet = new Set(jsx_exts);
78
+ const allSet = new Set(all_exts);
79
+ const isCjs = exts.every((i) => jscjsSet.has(i)) || exts.every((i) => tscjsSet.has(i));
80
+ const isJsx = exts.every((i) => jsxSet.has(i));
81
+ const isJs = exts.every((i) => jsesmSet.has(i));
82
+ const isTs = exts.every((i) => tsesmSet.has(i));
83
+ const isBoth = isJs && isTs;
84
+ const isNone = !exts.every((i) => allSet.has(i));
85
+ if (isNone) {
86
+ console.warn("Bundler detects none Javascript or Typescript extensions in the dependencies tree.");
87
+ process.exit(1);
88
+ }
89
+ if (isJsx) {
90
+ console.warn("The package detects JSX extensions (.jsx or .tsx) in the dependencies tree, which is currently unsupported.");
91
+ process.exit(1);
92
+ }
93
+ if (isCjs) {
94
+ console.warn("The package detects commonjs extensions (.cjd or .cts) in the dependencies tree, which is currently unsupported.");
95
+ process.exit(1);
96
+ }
97
+ if (isBoth) {
98
+ console.warn("The package detects both Javascript or Typescript extensions in the dependencies tree, currently unsupported.");
99
+ process.exit(1);
100
+ }
101
+ return true;
102
+ }
103
+ /**
104
+ * Check the module format of all the given files.
105
+ * @param deps List of files to check, where each file is an object with
106
+ * `file` and `content` properties.
107
+ * @returns True if the function finishes without errors, false otherwise.
108
+ */
109
+ function checkModuleFormat(deps) {
110
+ let _esmCount = 0;
111
+ let cjsCount = 0;
112
+ let unknowCount = 0;
113
+ for (const dep of deps) {
114
+ try {
115
+ // Create a TypeScript source file
116
+ const sourceFile = ts.createSourceFile(dep.file, dep.content, ts.ScriptTarget.Latest, true);
117
+ let hasESMImports = false;
118
+ let hasCommonJS = false;
119
+ // Walk through the AST to detect module syntax
120
+ function walk(node) {
121
+ // Check for ESM import/export syntax
122
+ if (ts.isImportDeclaration(node) ||
123
+ ts.isImportEqualsDeclaration(node) ||
124
+ ts.isExportDeclaration(node) ||
125
+ ts.isExportSpecifier(node) ||
126
+ ts.isExportAssignment(node)) {
127
+ hasESMImports = true;
128
+ }
129
+ // Check for export modifier on declarations
130
+ if ((ts.isVariableStatement(node) ||
131
+ ts.isFunctionDeclaration(node) ||
132
+ ts.isInterfaceDeclaration(node) ||
133
+ ts.isTypeAliasDeclaration(node) ||
134
+ ts.isEnumDeclaration(node) ||
135
+ ts.isClassDeclaration(node)) &&
136
+ node.modifiers?.some((mod) => mod.kind === ts.SyntaxKind.ExportKeyword)) {
137
+ hasESMImports = true;
138
+ }
139
+ // Check for CommonJS require/exports
140
+ if (ts.isCallExpression(node)) {
141
+ if (ts.isIdentifier(node.expression) &&
142
+ node.expression.text === "require" &&
143
+ node.arguments.length > 0) {
144
+ hasCommonJS = true;
145
+ }
146
+ }
147
+ // Check for module.exports or exports.xxx
148
+ if (ts.isPropertyAccessExpression(node)) {
149
+ const text = node.getText(sourceFile);
150
+ if (text.startsWith("module.exports") ||
151
+ text.startsWith("exports.")) {
152
+ hasCommonJS = true;
153
+ }
154
+ }
155
+ // Continue walking the AST
156
+ ts.forEachChild(node, walk);
157
+ }
158
+ walk(sourceFile);
159
+ // Determine the module format based on what we found
160
+ if (hasESMImports && !hasCommonJS) {
161
+ _esmCount++;
162
+ }
163
+ else if (hasCommonJS && !hasESMImports) {
164
+ cjsCount++;
165
+ }
166
+ else if (hasESMImports && hasCommonJS) {
167
+ // Mixed - probably ESM with dynamic imports or similar
168
+ _esmCount++;
169
+ }
170
+ }
171
+ catch (error) {
172
+ console.error(`Error checking module format for ${dep.file} : \n ${error}`);
173
+ unknowCount++;
174
+ }
175
+ } // loop
176
+ if (unknowCount) {
177
+ console.warn("Unknown error when checking module types in the dependencies tree.");
178
+ process.exit(1);
179
+ }
180
+ if (cjsCount) {
181
+ console.warn("The package detects CommonJs format in the dependencies tree, currently unsupported.");
182
+ process.exit(1);
183
+ }
184
+ return true;
185
+ }
186
+ /**
187
+ * Check if all files in the given list have the same file extension and
188
+ * the same module format.
189
+ * @param deps List of files to check, where each file is an object with
190
+ * `file` and `content` properties.
191
+ * @returns True if all files have the same file extension and module format,
192
+ * false otherwise.
193
+ */
194
+ function fileExtensionAndFormat(deps) {
195
+ console.time("checked extension and module");
196
+ const ce = checkExtGroup(deps);
197
+ const cm = checkModuleFormat(deps);
198
+ console.timeEnd("checked extension and module");
199
+ return ce && cm;
200
+ }
201
+ const check = { checkTypes, fileExtensionAndFormat };
202
+ module.exports = check;
203
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.cts"],"names":[],"mappings":";AAAA,iBAAiB;AACjB,kCAAmC;AACnC,iCAAkC;AAOlC,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACnC,MAAM,UAAU,GAAG,CAAC,MAAM,CAAC,CAAC;AAC5B,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACnC,MAAM,UAAU,GAAG,CAAC,MAAM,CAAC,CAAC;AAC5B,MAAM,QAAQ,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAClC,MAAM,QAAQ,GAAG;IACf,GAAG,UAAU;IACb,GAAG,UAAU;IACb,GAAG,UAAU;IACb,GAAG,UAAU;IACb,GAAG,QAAQ;CACZ,CAAC;AAEF;;;;;;GAMG;AACH,SA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,CAAC;IACD,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,CAAC,IAAI,CACV,uFAAuF,CACxF,CAAC;QACF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,sBAAsB,CAAC,IAAgB;IAC9C,OAAO,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;IAC7C,MAAM,EAAE,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,EAAE,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACnC,OAAO,CAAC,OAAO,CAAC,8BAA8B,CAAC,CAAC;IAChD,OAAO,EAAE,IAAI,EAAE,CAAC;AAClB,CAAC;AAED,MAAM,KAAK,GAAG,EAAE,UAAU,EAAE,sBAAsB,EAAE,CAAC;AAErD,iBAAS,KAAK,CAAC"}
@@ -0,0 +1,28 @@
1
+ import ts = require("typescript");
2
+ interface DepsFile {
3
+ file: string;
4
+ content: string;
5
+ }
6
+ /**
7
+ * Check types of given files and exit process if any errors are found.
8
+ * @param deps List of files to check, where each file is an object with
9
+ * `file` and `content` properties.
10
+ * @param compilerOptions TypeScript compiler options.
11
+ * @returns True if no errors are found, false otherwise.
12
+ */
13
+ declare function checkTypes(deps: DepsFile[], compilerOptions: ts.CompilerOptions): true | undefined;
14
+ /**
15
+ * Check if all files in the given list have the same file extension and
16
+ * the same module format.
17
+ * @param deps List of files to check, where each file is an object with
18
+ * `file` and `content` properties.
19
+ * @returns True if all files have the same file extension and module format,
20
+ * false otherwise.
21
+ */
22
+ declare function fileExtensionAndFormat(deps: DepsFile[]): boolean;
23
+ declare const check: {
24
+ checkTypes: typeof checkTypes;
25
+ fileExtensionAndFormat: typeof fileExtensionAndFormat;
26
+ };
27
+ export = check;
28
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.cts"],"names":[],"mappings":"AAEA,OAAO,EAAE,GAAG,QAAQ,YAAY,CAAC,CAAC;AAElC,UAAU,QAAQ;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAeD;;;;;;GAMG;AACH,iBAAS,UAAU,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,eAAe,EAAE,EAAE,CAAC,eAAe,oBA0CxE;AAkKD;;;;;;;GAOG;AACH,iBAAS,sBAAsB,CAAC,IAAI,EAAE,QAAQ,EAAE,WAM/C;AAED,QAAA,MAAM,KAAK;;;CAAyC,CAAC;AAErD,SAAS,KAAK,CAAC"}
@@ -0,0 +1,28 @@
1
+ import ts from "typescript";
2
+ interface DepsFile {
3
+ file: string;
4
+ content: string;
5
+ }
6
+ /**
7
+ * Check types of given files and exit process if any errors are found.
8
+ * @param deps List of files to check, where each file is an object with
9
+ * `file` and `content` properties.
10
+ * @param compilerOptions TypeScript compiler options.
11
+ * @returns True if no errors are found, false otherwise.
12
+ */
13
+ declare function checkTypes(deps: DepsFile[], compilerOptions: ts.CompilerOptions): true | undefined;
14
+ /**
15
+ * Check if all files in the given list have the same file extension and
16
+ * the same module format.
17
+ * @param deps List of files to check, where each file is an object with
18
+ * `file` and `content` properties.
19
+ * @returns True if all files have the same file extension and module format,
20
+ * false otherwise.
21
+ */
22
+ declare function fileExtensionAndFormat(deps: DepsFile[]): boolean;
23
+ declare const check: {
24
+ checkTypes: typeof checkTypes;
25
+ fileExtensionAndFormat: typeof fileExtensionAndFormat;
26
+ };
27
+ export default check;
28
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,UAAU,QAAQ;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAeD;;;;;;GAMG;AACH,iBAAS,UAAU,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,eAAe,EAAE,EAAE,CAAC,eAAe,oBA0CxE;AAkKD;;;;;;;GAOG;AACH,iBAAS,sBAAsB,CAAC,IAAI,EAAE,QAAQ,EAAE,WAM/C;AAED,QAAA,MAAM,KAAK;;;CAAyC,CAAC;AAErD,eAAe,KAAK,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,202 @@
1
+ // cSpell:disable
2
+ import path from "node:path";
3
+ import ts from "typescript";
4
+ const jsesm_exts = [".js", ".mjs"];
5
+ const jscjs_exts = [".cjs"];
6
+ const tsesm_exts = [".ts", ".mts"];
7
+ const tscjs_exts = [".cts"];
8
+ const jsx_exts = [".jsx", ".tsx"];
9
+ const all_exts = [
10
+ ...jscjs_exts,
11
+ ...jsesm_exts,
12
+ ...tscjs_exts,
13
+ ...tsesm_exts,
14
+ ...jsx_exts,
15
+ ];
16
+ /**
17
+ * Check types of given files and exit process if any errors are found.
18
+ * @param deps List of files to check, where each file is an object with
19
+ * `file` and `content` properties.
20
+ * @param compilerOptions TypeScript compiler options.
21
+ * @returns True if no errors are found, false otherwise.
22
+ */
23
+ function checkTypes(deps, compilerOptions) {
24
+ if (!compilerOptions.noCheck) {
25
+ console.time("types checked");
26
+ const filePaths = deps.map((i) => i.file);
27
+ let _err = false;
28
+ // Create program
29
+ const program = ts.createProgram(filePaths, compilerOptions);
30
+ // Check each file individually for immediate feedback
31
+ for (const filePath of filePaths) {
32
+ const sourceFile = program.getSourceFile(filePath);
33
+ if (!sourceFile) {
34
+ console.error(`File not found: ${path.relative(process.cwd(), filePath)}`);
35
+ process.exit(1);
36
+ }
37
+ const diagnostics = [
38
+ ...program.getSyntacticDiagnostics(sourceFile),
39
+ ...program.getSemanticDiagnostics(sourceFile),
40
+ ...program.getDeclarationDiagnostics(sourceFile),
41
+ ];
42
+ if (diagnostics.length > 0) {
43
+ const formatHost = {
44
+ getCurrentDirectory: () => process.cwd(),
45
+ getCanonicalFileName: (fileName) => fileName,
46
+ getNewLine: () => ts.sys.newLine,
47
+ };
48
+ console.error(ts.formatDiagnosticsWithColorAndContext(diagnostics, formatHost));
49
+ _err = true;
50
+ }
51
+ }
52
+ if (_err) {
53
+ process.exit(1);
54
+ }
55
+ else {
56
+ console.timeEnd("types checked");
57
+ return true;
58
+ }
59
+ }
60
+ }
61
+ /**
62
+ * Check the file extensions of given dependencies.
63
+ * @param deps List of files to check, where each file is an object with
64
+ * `file` and `content` properties.
65
+ * @returns True if no unsupported file extensions are found, false otherwise.
66
+ * @throws If unsupported file extensions are found, the function will throw an error and exit the process.
67
+ */
68
+ function checkExtGroup(deps) {
69
+ const exts = deps.map((dep) => {
70
+ return path.extname(dep.file);
71
+ });
72
+ const jsesmSet = new Set(jsesm_exts);
73
+ const jscjsSet = new Set(jscjs_exts);
74
+ const tsesmSet = new Set(tsesm_exts);
75
+ const tscjsSet = new Set(tscjs_exts);
76
+ const jsxSet = new Set(jsx_exts);
77
+ const allSet = new Set(all_exts);
78
+ const isCjs = exts.every((i) => jscjsSet.has(i)) || exts.every((i) => tscjsSet.has(i));
79
+ const isJsx = exts.every((i) => jsxSet.has(i));
80
+ const isJs = exts.every((i) => jsesmSet.has(i));
81
+ const isTs = exts.every((i) => tsesmSet.has(i));
82
+ const isBoth = isJs && isTs;
83
+ const isNone = !exts.every((i) => allSet.has(i));
84
+ if (isNone) {
85
+ console.warn("Bundler detects none Javascript or Typescript extensions in the dependencies tree.");
86
+ process.exit(1);
87
+ }
88
+ if (isJsx) {
89
+ console.warn("The package detects JSX extensions (.jsx or .tsx) in the dependencies tree, which is currently unsupported.");
90
+ process.exit(1);
91
+ }
92
+ if (isCjs) {
93
+ console.warn("The package detects commonjs extensions (.cjd or .cts) in the dependencies tree, which is currently unsupported.");
94
+ process.exit(1);
95
+ }
96
+ if (isBoth) {
97
+ console.warn("The package detects both Javascript or Typescript extensions in the dependencies tree, currently unsupported.");
98
+ process.exit(1);
99
+ }
100
+ return true;
101
+ }
102
+ /**
103
+ * Check the module format of all the given files.
104
+ * @param deps List of files to check, where each file is an object with
105
+ * `file` and `content` properties.
106
+ * @returns True if the function finishes without errors, false otherwise.
107
+ */
108
+ function checkModuleFormat(deps) {
109
+ let _esmCount = 0;
110
+ let cjsCount = 0;
111
+ let unknowCount = 0;
112
+ for (const dep of deps) {
113
+ try {
114
+ // Create a TypeScript source file
115
+ const sourceFile = ts.createSourceFile(dep.file, dep.content, ts.ScriptTarget.Latest, true);
116
+ let hasESMImports = false;
117
+ let hasCommonJS = false;
118
+ // Walk through the AST to detect module syntax
119
+ function walk(node) {
120
+ // Check for ESM import/export syntax
121
+ if (ts.isImportDeclaration(node) ||
122
+ ts.isImportEqualsDeclaration(node) ||
123
+ ts.isExportDeclaration(node) ||
124
+ ts.isExportSpecifier(node) ||
125
+ ts.isExportAssignment(node)) {
126
+ hasESMImports = true;
127
+ }
128
+ // Check for export modifier on declarations
129
+ if ((ts.isVariableStatement(node) ||
130
+ ts.isFunctionDeclaration(node) ||
131
+ ts.isInterfaceDeclaration(node) ||
132
+ ts.isTypeAliasDeclaration(node) ||
133
+ ts.isEnumDeclaration(node) ||
134
+ ts.isClassDeclaration(node)) &&
135
+ node.modifiers?.some((mod) => mod.kind === ts.SyntaxKind.ExportKeyword)) {
136
+ hasESMImports = true;
137
+ }
138
+ // Check for CommonJS require/exports
139
+ if (ts.isCallExpression(node)) {
140
+ if (ts.isIdentifier(node.expression) &&
141
+ node.expression.text === "require" &&
142
+ node.arguments.length > 0) {
143
+ hasCommonJS = true;
144
+ }
145
+ }
146
+ // Check for module.exports or exports.xxx
147
+ if (ts.isPropertyAccessExpression(node)) {
148
+ const text = node.getText(sourceFile);
149
+ if (text.startsWith("module.exports") ||
150
+ text.startsWith("exports.")) {
151
+ hasCommonJS = true;
152
+ }
153
+ }
154
+ // Continue walking the AST
155
+ ts.forEachChild(node, walk);
156
+ }
157
+ walk(sourceFile);
158
+ // Determine the module format based on what we found
159
+ if (hasESMImports && !hasCommonJS) {
160
+ _esmCount++;
161
+ }
162
+ else if (hasCommonJS && !hasESMImports) {
163
+ cjsCount++;
164
+ }
165
+ else if (hasESMImports && hasCommonJS) {
166
+ // Mixed - probably ESM with dynamic imports or similar
167
+ _esmCount++;
168
+ }
169
+ }
170
+ catch (error) {
171
+ console.error(`Error checking module format for ${dep.file} : \n ${error}`);
172
+ unknowCount++;
173
+ }
174
+ } // loop
175
+ if (unknowCount) {
176
+ console.warn("Unknown error when checking module types in the dependencies tree.");
177
+ process.exit(1);
178
+ }
179
+ if (cjsCount) {
180
+ console.warn("The package detects CommonJs format in the dependencies tree, currently unsupported.");
181
+ process.exit(1);
182
+ }
183
+ return true;
184
+ }
185
+ /**
186
+ * Check if all files in the given list have the same file extension and
187
+ * the same module format.
188
+ * @param deps List of files to check, where each file is an object with
189
+ * `file` and `content` properties.
190
+ * @returns True if all files have the same file extension and module format,
191
+ * false otherwise.
192
+ */
193
+ function fileExtensionAndFormat(deps) {
194
+ console.time("checked extension and module");
195
+ const ce = checkExtGroup(deps);
196
+ const cm = checkModuleFormat(deps);
197
+ console.timeEnd("checked extension and module");
198
+ return ce && cm;
199
+ }
200
+ const check = { checkTypes, fileExtensionAndFormat };
201
+ export default check;
202
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,iBAAiB;AACjB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,MAAM,YAAY,CAAC;AAO5B,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACnC,MAAM,UAAU,GAAG,CAAC,MAAM,CAAC,CAAC;AAC5B,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACnC,MAAM,UAAU,GAAG,CAAC,MAAM,CAAC,CAAC;AAC5B,MAAM,QAAQ,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAA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H,CAAC,CAAC,OAAO;IACT,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,CAAC,IAAI,CACV,oEAAoE,CACrE,CAAC;QACF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IACD,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,CAAC,IAAI,CACV,uFAAuF,CACxF,CAAC;QACF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,sBAAsB,CAAC,IAAgB;IAC9C,OAAO,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;IAC7C,MAAM,EAAE,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,EAAE,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACnC,OAAO,CAAC,OAAO,CAAC,8BAA8B,CAAC,CAAC;IAChD,OAAO,EAAE,IAAI,EAAE,CAAC;AAClB,CAAC;AAED,MAAM,KAAK,GAAG,EAAE,UAAU,EAAE,sBAAsB,EAAE,CAAC;AAErD,eAAe,KAAK,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suseejs/check",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
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
- }