ts-codemod-lib 2.2.1 → 2.2.3
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/cmd/append-as-const.mjs +51 -48
- package/dist/cmd/append-as-const.mjs.map +1 -1
- package/dist/cmd/convert-interface-to-type.mjs +51 -48
- package/dist/cmd/convert-interface-to-type.mjs.map +1 -1
- package/dist/cmd/convert-to-readonly.mjs +51 -48
- package/dist/cmd/convert-to-readonly.mjs.map +1 -1
- package/dist/cmd/replace-any-with-unknown.mjs +51 -48
- package/dist/cmd/replace-any-with-unknown.mjs.map +1 -1
- package/dist/cmd/replace-record-with-unknown-record.mjs +51 -48
- package/dist/cmd/replace-record-with-unknown-record.mjs.map +1 -1
- package/dist/cmd/run-transformer-cli.d.mts.map +1 -1
- package/dist/cmd/run-transformer-cli.mjs +138 -127
- package/dist/cmd/run-transformer-cli.mjs.map +1 -1
- package/dist/functions/ast-transformers/append-as-const.d.mts.map +1 -1
- package/dist/functions/ast-transformers/append-as-const.mjs +205 -191
- package/dist/functions/ast-transformers/append-as-const.mjs.map +1 -1
- package/dist/functions/ast-transformers/convert-interface-to-type.mjs +62 -82
- package/dist/functions/ast-transformers/convert-interface-to-type.mjs.map +1 -1
- package/dist/functions/ast-transformers/convert-to-readonly.d.mts.map +1 -1
- package/dist/functions/ast-transformers/convert-to-readonly.mjs +742 -834
- package/dist/functions/ast-transformers/convert-to-readonly.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/compare-union-types.d.mts.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/compare-union-types.mjs +2 -14
- package/dist/functions/ast-transformers/readonly-transformer-helpers/compare-union-types.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/constants.d.mts +2 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/constants.d.mts.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/constants.mjs +8 -8
- package/dist/functions/ast-transformers/readonly-transformer-helpers/constants.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/group-union-types.d.mts.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/group-union-types.mjs +51 -62
- package/dist/functions/ast-transformers/readonly-transformer-helpers/group-union-types.mjs.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.d.mts +12 -12
- package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.d.mts.map +1 -1
- package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.mjs +16 -14
- package/dist/functions/ast-transformers/readonly-transformer-helpers/readonly-context.mjs.map +1 -1
- package/dist/functions/ast-transformers/replace-any-with-unknown.mjs +25 -27
- package/dist/functions/ast-transformers/replace-any-with-unknown.mjs.map +1 -1
- package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs +149 -208
- package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs.map +1 -1
- package/dist/functions/ast-transformers/transform-source-code.d.mts.map +1 -1
- package/dist/functions/ast-transformers/transform-source-code.mjs +70 -72
- package/dist/functions/ast-transformers/transform-source-code.mjs.map +1 -1
- package/dist/functions/constants/ignore-comment-text.d.mts +2 -2
- package/dist/functions/constants/ignore-comment-text.d.mts.map +1 -1
- package/dist/functions/constants/ignore-comment-text.mjs +8 -8
- package/dist/functions/constants/ignore-comment-text.mjs.map +1 -1
- package/dist/functions/functions/has-disable-next-line-comment.d.mts.map +1 -1
- package/dist/functions/functions/has-disable-next-line-comment.mjs +33 -65
- package/dist/functions/functions/has-disable-next-line-comment.mjs.map +1 -1
- package/dist/functions/functions/is-as-const-node.d.mts +1 -1
- package/dist/functions/functions/is-as-const-node.d.mts.map +1 -1
- package/dist/functions/functions/is-as-const-node.mjs +15 -23
- package/dist/functions/functions/is-as-const-node.mjs.map +1 -1
- package/dist/functions/functions/is-atomic-type-node.d.mts.map +1 -1
- package/dist/functions/functions/is-atomic-type-node.mjs +22 -36
- package/dist/functions/functions/is-atomic-type-node.mjs.map +1 -1
- package/dist/functions/functions/is-conditional-type-distribution-guard.d.mts.map +1 -1
- package/dist/functions/functions/is-conditional-type-distribution-guard.mjs +9 -33
- package/dist/functions/functions/is-conditional-type-distribution-guard.mjs.map +1 -1
- package/dist/functions/functions/is-readonly-node.d.mts.map +1 -1
- package/dist/functions/functions/is-readonly-node.mjs +11 -21
- package/dist/functions/functions/is-readonly-node.mjs.map +1 -1
- package/dist/functions/functions/is-spread-parameter-node.d.mts.map +1 -1
- package/dist/functions/functions/is-spread-parameter-node.mjs +2 -4
- package/dist/functions/functions/is-spread-parameter-node.mjs.map +1 -1
- package/dist/functions/functions/remove-parentheses.d.mts.map +1 -1
- package/dist/functions/functions/remove-parentheses.mjs +1 -4
- package/dist/functions/functions/remove-parentheses.mjs.map +1 -1
- package/dist/functions/functions/should-avoid-parentheses-for-readonly.d.mts.map +1 -1
- package/dist/functions/functions/should-avoid-parentheses-for-readonly.mjs +7 -24
- package/dist/functions/functions/should-avoid-parentheses-for-readonly.mjs.map +1 -1
- package/dist/functions/functions/unwrap-readonly.d.mts.map +1 -1
- package/dist/functions/functions/unwrap-readonly.mjs +1 -3
- package/dist/functions/functions/unwrap-readonly.mjs.map +1 -1
- package/dist/functions/functions/wrap-with-parentheses.d.mts.map +1 -1
- package/dist/functions/functions/wrap-with-parentheses.mjs +26 -51
- package/dist/functions/functions/wrap-with-parentheses.mjs.map +1 -1
- package/dist/functions/utils/replace-with-debug.d.mts.map +1 -1
- package/dist/functions/utils/replace-with-debug.mjs +2 -2
- package/dist/functions/utils/replace-with-debug.mjs.map +1 -1
- package/package.json +40 -29
- package/src/cmd/append-as-const.mts +3 -3
- package/src/cmd/convert-interface-to-type.mts +3 -3
- package/src/cmd/convert-to-readonly.mts +3 -3
- package/src/cmd/replace-any-with-unknown.mts +3 -3
- package/src/cmd/replace-record-with-unknown-record.mts +3 -3
- package/src/cmd/run-transformer-cli.mts +21 -17
- package/src/functions/ast-transformers/convert-interface-to-type.mts +1 -1
- package/src/functions/ast-transformers/convert-to-readonly.mts +64 -35
- package/src/functions/ast-transformers/convert-to-readonly.test.mts +67 -1
- package/src/functions/ast-transformers/readonly-transformer-helpers/readonly-context.mts +6 -6
- package/src/functions/ast-transformers/replace-record-with-unknown-record.mts +4 -4
- package/src/functions/functions/has-disable-next-line-comment.mts +5 -5
- package/src/functions/functions/wrap-with-parentheses.mts +17 -23
|
@@ -2,84 +2,82 @@ import { Arr } from 'ts-data-forge';
|
|
|
2
2
|
import * as tsm from 'ts-morph';
|
|
3
3
|
|
|
4
4
|
const extractFileIgnoreTransformers = (code) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
regex: /\/\*\s*transform-ignore\s*(.*?)\s*\*\//u,
|
|
22
|
-
},
|
|
23
|
-
];
|
|
24
|
-
for (const { regex } of patterns) {
|
|
25
|
-
const match = regex.exec(code);
|
|
26
|
-
if (match !== null) {
|
|
27
|
-
const targetTransformers = match[1]?.trim() ?? '';
|
|
28
|
-
// Empty means ignore all transformers
|
|
29
|
-
if (targetTransformers === '') {
|
|
30
|
-
return [];
|
|
31
|
-
}
|
|
32
|
-
// Parse comma-separated transformer names
|
|
33
|
-
return targetTransformers.split(',').map((name) => name.trim());
|
|
34
|
-
}
|
|
5
|
+
const patterns = [
|
|
6
|
+
{
|
|
7
|
+
prefix: "transformer-ignore",
|
|
8
|
+
regex: /\/\*\s*transformer-ignore\s*(.*?)\s*\*\//u
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
prefix: "ts-codemod-ignore",
|
|
12
|
+
regex: /\/\*\s*ts-codemod-ignore\s*(.*?)\s*\*\//u
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
prefix: "codemod-ignore",
|
|
16
|
+
regex: /\/\*\s*codemod-ignore\s*(.*?)\s*\*\//u
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
prefix: "transform-ignore",
|
|
20
|
+
regex: /\/\*\s*transform-ignore\s*(.*?)\s*\*\//u
|
|
35
21
|
}
|
|
36
|
-
|
|
22
|
+
];
|
|
23
|
+
for (const { regex } of patterns) {
|
|
24
|
+
const match = regex.exec(code);
|
|
25
|
+
if (match !== null) {
|
|
26
|
+
const targetTransformers = match[1]?.trim() ?? "";
|
|
27
|
+
if (targetTransformers === "") {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
return targetTransformers.split(",").map((name) => name.trim());
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return [];
|
|
37
34
|
};
|
|
38
35
|
const shouldSkipFile = (code, transformerName) => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
// Check if the transformer is in the ignore list
|
|
60
|
-
return ignoredTransformers.includes(transformerName);
|
|
36
|
+
const ignoredTransformers = extractFileIgnoreTransformers(code);
|
|
37
|
+
const patterns = [
|
|
38
|
+
/\/\*\s*transformer-ignore\s*.*?\s*\*\//u,
|
|
39
|
+
/\/\*\s*ts-codemod-ignore\s*.*?\s*\*\//u,
|
|
40
|
+
/\/\*\s*codemod-ignore\s*.*?\s*\*\//u,
|
|
41
|
+
/\/\*\s*transform-ignore\s*.*?\s*\*\//u
|
|
42
|
+
];
|
|
43
|
+
const hasFileIgnoreComment = patterns.some((regex) => regex.test(code));
|
|
44
|
+
if (Arr.isArrayOfLength(ignoredTransformers, 0) && !hasFileIgnoreComment) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (Arr.isArrayOfLength(ignoredTransformers, 0)) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
if (transformerName === void 0) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return ignoredTransformers.includes(transformerName);
|
|
61
54
|
};
|
|
62
55
|
const transformSourceCode = (code, isTsx, transformers, debug = false) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
56
|
+
const project = new tsm.Project({
|
|
57
|
+
useInMemoryFileSystem: true,
|
|
58
|
+
compilerOptions: {
|
|
59
|
+
jsx: isTsx ? tsm.ts.JsxEmit.React : void 0,
|
|
60
|
+
target: tsm.ts.ScriptTarget.ESNext,
|
|
61
|
+
module: tsm.ts.ModuleKind.ESNext
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
const sourceAst = project.createSourceFile(
|
|
65
|
+
`source.${isTsx ? "tsx" : "ts"}`,
|
|
66
|
+
code
|
|
67
|
+
);
|
|
68
|
+
for (const transformer of transformers) {
|
|
69
|
+
const transformerName = transformer.name;
|
|
70
|
+
if (shouldSkipFile(code, transformerName)) {
|
|
71
|
+
if (debug) {
|
|
72
|
+
console.debug(
|
|
73
|
+
`skipped by ignore-file comment for transformer: ${transformerName}`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
continue;
|
|
81
77
|
}
|
|
82
|
-
|
|
78
|
+
transformer.transform(sourceAst);
|
|
79
|
+
}
|
|
80
|
+
return sourceAst.getFullText();
|
|
83
81
|
};
|
|
84
82
|
|
|
85
83
|
export { transformSourceCode };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform-source-code.mjs","sources":["../../../src/functions/ast-transformers/transform-source-code.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIA,MAAM,
|
|
1
|
+
{"version":3,"file":"transform-source-code.mjs","sources":["../../../src/functions/ast-transformers/transform-source-code.mts"],"sourcesContent":["import { Arr } from 'ts-data-forge';\nimport * as tsm from 'ts-morph';\nimport { type TsMorphTransformer } from './types.mjs';\n\nconst extractFileIgnoreTransformers = (code: string): readonly string[] => {\n // Try to find any of the supported file-level ignore comment prefixes\n const patterns = [\n {\n prefix: 'transformer-ignore',\n regex: /\\/\\*\\s*transformer-ignore\\s*(.*?)\\s*\\*\\//u,\n },\n {\n prefix: 'ts-codemod-ignore',\n regex: /\\/\\*\\s*ts-codemod-ignore\\s*(.*?)\\s*\\*\\//u,\n },\n {\n prefix: 'codemod-ignore',\n regex: /\\/\\*\\s*codemod-ignore\\s*(.*?)\\s*\\*\\//u,\n },\n {\n prefix: 'transform-ignore',\n regex: /\\/\\*\\s*transform-ignore\\s*(.*?)\\s*\\*\\//u,\n },\n ] as const;\n\n for (const { regex } of patterns) {\n const match = regex.exec(code);\n\n if (match !== null) {\n const targetTransformers = match[1]?.trim() ?? '';\n\n // Empty means ignore all transformers\n if (targetTransformers === '') {\n return [];\n }\n\n // Parse comma-separated transformer names\n return targetTransformers.split(',').map((name) => name.trim());\n }\n }\n\n return [];\n};\n\nconst shouldSkipFile = (\n code: string,\n transformerName: string | undefined,\n): boolean => {\n const ignoredTransformers = extractFileIgnoreTransformers(code);\n\n // If no file-level ignore comment found, don't skip\n const patterns = [\n /\\/\\*\\s*transformer-ignore\\s*.*?\\s*\\*\\//u,\n /\\/\\*\\s*ts-codemod-ignore\\s*.*?\\s*\\*\\//u,\n /\\/\\*\\s*codemod-ignore\\s*.*?\\s*\\*\\//u,\n /\\/\\*\\s*transform-ignore\\s*.*?\\s*\\*\\//u,\n ] as const;\n\n const hasFileIgnoreComment = patterns.some((regex) => regex.test(code));\n\n if (Arr.isArrayOfLength(ignoredTransformers, 0) && !hasFileIgnoreComment) {\n return false;\n }\n\n // Empty array means ignore all transformers (file-level ignore without specific transformers)\n if (Arr.isArrayOfLength(ignoredTransformers, 0)) {\n return true;\n }\n\n // If transformer name is not specified, don't skip\n if (transformerName === undefined) {\n return false;\n }\n\n // Check if the transformer is in the ignore list\n return ignoredTransformers.includes(transformerName);\n};\n\nexport const transformSourceCode = (\n code: string,\n isTsx: boolean,\n transformers: readonly TsMorphTransformer[],\n debug: boolean = false,\n): string => {\n const project = new tsm.Project({\n useInMemoryFileSystem: true,\n compilerOptions: {\n jsx: isTsx ? tsm.ts.JsxEmit.React : undefined,\n target: tsm.ts.ScriptTarget.ESNext,\n module: tsm.ts.ModuleKind.ESNext,\n },\n });\n\n const sourceAst = project.createSourceFile(\n `source.${isTsx ? 'tsx' : 'ts'}`,\n code,\n );\n\n for (const transformer of transformers) {\n const transformerName = transformer.name;\n\n if (shouldSkipFile(code, transformerName)) {\n if (debug) {\n console.debug(\n `skipped by ignore-file comment for transformer: ${transformerName}`,\n );\n }\n\n continue;\n }\n\n transformer.transform(sourceAst);\n }\n\n return sourceAst.getFullText();\n};\n"],"names":[],"mappings":";;;AAIA,MAAM,6BAAA,GAAgC,CAAC,IAAA,KAAoC;AAEzE,EAAA,MAAM,QAAA,GAAW;AAAA,IACf;AAAA,MACE,MAAA,EAAQ,oBAAA;AAAA,MACR,KAAA,EAAO;AAAA,KACT;AAAA,IACA;AAAA,MACE,MAAA,EAAQ,mBAAA;AAAA,MACR,KAAA,EAAO;AAAA,KACT;AAAA,IACA;AAAA,MACE,MAAA,EAAQ,gBAAA;AAAA,MACR,KAAA,EAAO;AAAA,KACT;AAAA,IACA;AAAA,MACE,MAAA,EAAQ,kBAAA;AAAA,MACR,KAAA,EAAO;AAAA;AACT,GACF;AAEA,EAAA,KAAA,MAAW,EAAE,KAAA,EAAM,IAAK,QAAA,EAAU;AAChC,IAAA,MAAM,KAAA,GAAQ,KAAA,CAAM,IAAA,CAAK,IAAI,CAAA;AAE7B,IAAA,IAAI,UAAU,IAAA,EAAM;AAClB,MAAA,MAAM,kBAAA,GAAqB,KAAA,CAAM,CAAC,CAAA,EAAG,MAAK,IAAK,EAAA;AAG/C,MAAA,IAAI,uBAAuB,EAAA,EAAI;AAC7B,QAAA,OAAO,EAAC;AAAA,MACV;AAGA,MAAA,OAAO,kBAAA,CAAmB,MAAM,GAAG,CAAA,CAAE,IAAI,CAAC,IAAA,KAAS,IAAA,CAAK,IAAA,EAAM,CAAA;AAAA,IAChE;AAAA,EACF;AAEA,EAAA,OAAO,EAAC;AACV,CAAA;AAEA,MAAM,cAAA,GAAiB,CACrB,IAAA,EACA,eAAA,KACY;AACZ,EAAA,MAAM,mBAAA,GAAsB,8BAA8B,IAAI,CAAA;AAG9D,EAAA,MAAM,QAAA,GAAW;AAAA,IACf,yCAAA;AAAA,IACA,wCAAA;AAAA,IACA,qCAAA;AAAA,IACA;AAAA,GACF;AAEA,EAAA,MAAM,oBAAA,GAAuB,SAAS,IAAA,CAAK,CAAC,UAAU,KAAA,CAAM,IAAA,CAAK,IAAI,CAAC,CAAA;AAEtE,EAAA,IAAI,IAAI,eAAA,CAAgB,mBAAA,EAAqB,CAAC,CAAA,IAAK,CAAC,oBAAA,EAAsB;AACxE,IAAA,OAAO,KAAA;AAAA,EACT;AAGA,EAAA,IAAI,GAAA,CAAI,eAAA,CAAgB,mBAAA,EAAqB,CAAC,CAAA,EAAG;AAC/C,IAAA,OAAO,IAAA;AAAA,EACT;AAGA,EAAA,IAAI,oBAAoB,MAAA,EAAW;AACjC,IAAA,OAAO,KAAA;AAAA,EACT;AAGA,EAAA,OAAO,mBAAA,CAAoB,SAAS,eAAe,CAAA;AACrD,CAAA;AAEO,MAAM,sBAAsB,CACjC,IAAA,EACA,KAAA,EACA,YAAA,EACA,QAAiB,KAAA,KACN;AACX,EAAA,MAAM,OAAA,GAAU,IAAI,GAAA,CAAI,OAAA,CAAQ;AAAA,IAC9B,qBAAA,EAAuB,IAAA;AAAA,IACvB,eAAA,EAAiB;AAAA,MACf,GAAA,EAAK,KAAA,GAAQ,GAAA,CAAI,EAAA,CAAG,QAAQ,KAAA,GAAQ,MAAA;AAAA,MACpC,MAAA,EAAQ,GAAA,CAAI,EAAA,CAAG,YAAA,CAAa,MAAA;AAAA,MAC5B,MAAA,EAAQ,GAAA,CAAI,EAAA,CAAG,UAAA,CAAW;AAAA;AAC5B,GACD,CAAA;AAED,EAAA,MAAM,YAAY,OAAA,CAAQ,gBAAA;AAAA,IACxB,CAAA,OAAA,EAAU,KAAA,GAAQ,KAAA,GAAQ,IAAI,CAAA,CAAA;AAAA,IAC9B;AAAA,GACF;AAEA,EAAA,KAAA,MAAW,eAAe,YAAA,EAAc;AACtC,IAAA,MAAM,kBAAkB,WAAA,CAAY,IAAA;AAEpC,IAAA,IAAI,cAAA,CAAe,IAAA,EAAM,eAAe,CAAA,EAAG;AACzC,MAAA,IAAI,KAAA,EAAO;AACT,QAAA,OAAA,CAAQ,KAAA;AAAA,UACN,mDAAmD,eAAe,CAAA;AAAA,SACpE;AAAA,MACF;AAEA,MAAA;AAAA,IACF;AAEA,IAAA,WAAA,CAAY,UAAU,SAAS,CAAA;AAAA,EACjC;AAEA,EAAA,OAAO,UAAU,WAAA,EAAY;AAC/B;;;;"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const IGNORE_LINE_COMMENT_PREFIXES: readonly [
|
|
2
|
-
export declare const IGNORE_FILE_COMMENT_PREFIXES: readonly [
|
|
1
|
+
export declare const IGNORE_LINE_COMMENT_PREFIXES: readonly ['transformer-ignore-next-line', 'ts-codemod-ignore-next-line', 'codemod-ignore-next-line', 'transform-ignore-next-line'];
|
|
2
|
+
export declare const IGNORE_FILE_COMMENT_PREFIXES: readonly ['transformer-ignore', 'ts-codemod-ignore', 'codemod-ignore', 'transform-ignore'];
|
|
3
3
|
//# sourceMappingURL=ignore-comment-text.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ignore-comment-text.d.mts","sourceRoot":"","sources":["../../../src/functions/constants/ignore-comment-text.mts"],"names":[],"mappings":"AAAA,eAAO,MAAM,4BAA4B,
|
|
1
|
+
{"version":3,"file":"ignore-comment-text.d.mts","sourceRoot":"","sources":["../../../src/functions/constants/ignore-comment-text.mts"],"names":[],"mappings":"AAAA,eAAO,MAAM,4BAA4B,YACvC,8BAA8B,EAC9B,6BAA6B,EAC7B,0BAA0B,EAC1B,4BAA4B,CACpB,CAAC;AAEX,eAAO,MAAM,4BAA4B,YACvC,oBAAoB,EACpB,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,CACV,CAAC"}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
const IGNORE_LINE_COMMENT_PREFIXES = [
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
"transformer-ignore-next-line",
|
|
3
|
+
"ts-codemod-ignore-next-line",
|
|
4
|
+
"codemod-ignore-next-line",
|
|
5
|
+
"transform-ignore-next-line"
|
|
6
6
|
];
|
|
7
7
|
const IGNORE_FILE_COMMENT_PREFIXES = [
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
"transformer-ignore",
|
|
9
|
+
"ts-codemod-ignore",
|
|
10
|
+
"codemod-ignore",
|
|
11
|
+
"transform-ignore"
|
|
12
12
|
];
|
|
13
13
|
|
|
14
14
|
export { IGNORE_FILE_COMMENT_PREFIXES, IGNORE_LINE_COMMENT_PREFIXES };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ignore-comment-text.mjs","sources":["../../../src/functions/constants/ignore-comment-text.mts"],"sourcesContent":[
|
|
1
|
+
{"version":3,"file":"ignore-comment-text.mjs","sources":["../../../src/functions/constants/ignore-comment-text.mts"],"sourcesContent":["export const IGNORE_LINE_COMMENT_PREFIXES = [\n 'transformer-ignore-next-line',\n 'ts-codemod-ignore-next-line',\n 'codemod-ignore-next-line',\n 'transform-ignore-next-line',\n] as const;\n\nexport const IGNORE_FILE_COMMENT_PREFIXES = [\n 'transformer-ignore',\n 'ts-codemod-ignore',\n 'codemod-ignore',\n 'transform-ignore',\n] as const;\n"],"names":[],"mappings":"AAAO,MAAM,4BAAA,GAA+B;AAAA,EAC1C,8BAAA;AAAA,EACA,6BAAA;AAAA,EACA,0BAAA;AAAA,EACA;AACF;AAEO,MAAM,4BAAA,GAA+B;AAAA,EAC1C,oBAAA;AAAA,EACA,mBAAA;AAAA,EACA,gBAAA;AAAA,EACA;AACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"has-disable-next-line-comment.d.mts","sourceRoot":"","sources":["../../../src/functions/functions/has-disable-next-line-comment.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAGhC;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,yBAAyB,
|
|
1
|
+
{"version":3,"file":"has-disable-next-line-comment.d.mts","sourceRoot":"","sources":["../../../src/functions/functions/has-disable-next-line-comment.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAGhC;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,yBAAyB,SAC9B,GAAG,CAAC,IAAI,oBACI,MAAM,KACvB,OAwEF,CAAC"}
|
|
@@ -1,74 +1,42 @@
|
|
|
1
1
|
import * as tsm from 'ts-morph';
|
|
2
2
|
import { IGNORE_LINE_COMMENT_PREFIXES } from '../constants/ignore-comment-text.mjs';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Checks if a given ts-morph Node is immediately preceded by an ignore-next-line comment,
|
|
6
|
-
* optionally filtered by transformer name(s).
|
|
7
|
-
*
|
|
8
|
-
* @param node - The ts-morph Node to check.
|
|
9
|
-
* @param transformerName - Optional transformer name to check for specific ignore directive.
|
|
10
|
-
* If provided, only ignores if the comment specifies this transformer.
|
|
11
|
-
* Supports ESLint-style syntax like:
|
|
12
|
-
* - `// transformer-ignore-next-line` (ignores all transformers)
|
|
13
|
-
* - `// transformer-ignore-next-line append-as-const` (specific transformer)
|
|
14
|
-
* - `// transformer-ignore-next-line append-as-const, replace-any-with-unknown` (multiple transformers)
|
|
15
|
-
* - Also accepts `ts-codemod-ignore-next-line`, `codemod-ignore-next-line`, `transform-ignore-next-line`
|
|
16
|
-
* @returns True if the node is preceded by the ignore comment on the immediately previous line, false otherwise.
|
|
17
|
-
*/
|
|
18
4
|
const hasDisableNextLineComment = (node, transformerName) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
.trim();
|
|
46
|
-
// If no transformer name specified, check if comment applies to all transformers
|
|
47
|
-
if (transformerName === undefined) {
|
|
48
|
-
return true;
|
|
49
|
-
}
|
|
50
|
-
// If no specific transformers listed in comment, it applies to all
|
|
51
|
-
if (afterPrefix === '') {
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
// Check if the transformer name is in the comma-separated list
|
|
55
|
-
const targetTransformers = afterPrefix
|
|
56
|
-
.split(',')
|
|
57
|
-
.map((name) => name.trim());
|
|
58
|
-
return targetTransformers.includes(transformerName);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
// If we found *any* comment on the preceding line, but it wasn't
|
|
62
|
-
// the correct ignore comment, then the node is not ignored by
|
|
63
|
-
// a comment on the *immediately* preceding line. Stop checking further back.
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
// If the comment ends before the preceding line, stop checking further back.
|
|
67
|
-
if (commentEndLine < nodeStartLine - 1) {
|
|
68
|
-
break;
|
|
5
|
+
const nodeStartLine = node.getStartLineNumber();
|
|
6
|
+
if (nodeStartLine <= 1) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const sourceFile = node.getSourceFile();
|
|
10
|
+
const leadingCommentRanges = node.getLeadingCommentRanges();
|
|
11
|
+
for (let mut_i = leadingCommentRanges.length - 1; mut_i >= 0; mut_i--) {
|
|
12
|
+
const commentRange = leadingCommentRanges[mut_i];
|
|
13
|
+
const commentEndPos = commentRange.getEnd();
|
|
14
|
+
const commentEndLine = sourceFile.getLineAndColumnAtPos(commentEndPos).line;
|
|
15
|
+
if (nodeStartLine === commentEndLine + 1) {
|
|
16
|
+
if (commentRange.getKind() === tsm.SyntaxKind.SingleLineCommentTrivia) {
|
|
17
|
+
const commentText = commentRange.getText().trim();
|
|
18
|
+
const matchedPrefix = IGNORE_LINE_COMMENT_PREFIXES.find(
|
|
19
|
+
(prefix) => commentText.includes(prefix)
|
|
20
|
+
);
|
|
21
|
+
if (matchedPrefix !== void 0) {
|
|
22
|
+
if (transformerName === void 0) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
const afterPrefix = commentText.slice(commentText.indexOf(matchedPrefix)).replace(matchedPrefix, "").trim();
|
|
26
|
+
if (afterPrefix === "") {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
const targetTransformers = afterPrefix.split(",").map((name) => name.trim());
|
|
30
|
+
return targetTransformers.includes(transformerName);
|
|
69
31
|
}
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
70
34
|
}
|
|
71
|
-
|
|
35
|
+
if (commentEndLine < nodeStartLine - 1) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
72
40
|
};
|
|
73
41
|
|
|
74
42
|
export { hasDisableNextLineComment };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"has-disable-next-line-comment.mjs","sources":["../../../src/functions/functions/has-disable-next-line-comment.mts"],"sourcesContent":[null],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"has-disable-next-line-comment.mjs","sources":["../../../src/functions/functions/has-disable-next-line-comment.mts"],"sourcesContent":["import * as tsm from 'ts-morph';\nimport { IGNORE_LINE_COMMENT_PREFIXES } from '../constants/index.mjs';\n\n/**\n * Checks if a given ts-morph Node is immediately preceded by an ignore-next-line comment,\n * optionally filtered by transformer name(s).\n *\n * @param node - The ts-morph Node to check.\n * @param transformerName - Optional transformer name to check for specific ignore directive.\n * If provided, only ignores if the comment specifies this transformer.\n * Supports ESLint-style syntax like:\n * - `// transformer-ignore-next-line` (ignores all transformers)\n * - `// transformer-ignore-next-line append-as-const` (specific transformer)\n * - `// transformer-ignore-next-line append-as-const, replace-any-with-unknown` (multiple transformers)\n * - Also accepts `ts-codemod-ignore-next-line`, `codemod-ignore-next-line`, `transform-ignore-next-line`\n * @returns True if the node is preceded by the ignore comment on the immediately previous line, false otherwise.\n */\nexport const hasDisableNextLineComment = (\n node: tsm.Node,\n transformerName?: string,\n): boolean => {\n const nodeStartLine = node.getStartLineNumber();\n\n // Cannot be ignored if it's on the first line\n if (nodeStartLine <= 1) {\n return false;\n }\n\n const sourceFile = node.getSourceFile(); // Get the SourceFile\n\n const leadingCommentRanges = node.getLeadingCommentRanges();\n\n // Iterate backwards through comments as the closest one is most relevant\n for (let mut_i = leadingCommentRanges.length - 1; mut_i >= 0; mut_i--) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const commentRange = leadingCommentRanges[mut_i]!;\n\n // Get the end position and convert it to line number\n const commentEndPos = commentRange.getEnd();\n\n const commentEndLine = sourceFile.getLineAndColumnAtPos(commentEndPos).line;\n\n // Check if the comment is on the immediately preceding line\n if (nodeStartLine === commentEndLine + 1) {\n // Check if it's a single-line comment containing the specific ignore text\n if (commentRange.getKind() === tsm.SyntaxKind.SingleLineCommentTrivia) {\n const commentText = commentRange.getText().trim();\n\n // Check if the comment contains any of the supported ignore prefixes\n const matchedPrefix = IGNORE_LINE_COMMENT_PREFIXES.find((prefix) =>\n commentText.includes(prefix),\n );\n\n if (matchedPrefix !== undefined) {\n // If no transformer name specified, check if comment applies to all transformers\n if (transformerName === undefined) {\n return true;\n }\n\n // Extract the part after the prefix\n const afterPrefix = commentText\n .slice(commentText.indexOf(matchedPrefix))\n .replace(matchedPrefix, '')\n .trim();\n\n // If no specific transformers listed in comment, it applies to all\n if (afterPrefix === '') {\n return true;\n }\n\n // Check if the transformer name is in the comma-separated list\n const targetTransformers = afterPrefix\n .split(',')\n .map((name) => name.trim());\n\n return targetTransformers.includes(transformerName);\n }\n }\n\n // If we found *any* comment on the preceding line, but it wasn't\n // the correct ignore comment, then the node is not ignored by\n // a comment on the *immediately* preceding line. Stop checking further back.\n return false;\n }\n\n // If the comment ends before the preceding line, stop checking further back.\n if (commentEndLine < nodeStartLine - 1) {\n break;\n }\n }\n\n return false;\n};\n"],"names":[],"mappings":";;;AAiBO,MAAM,yBAAA,GAA4B,CACvC,IAAA,EACA,eAAA,KACY;AACZ,EAAA,MAAM,aAAA,GAAgB,KAAK,kBAAA,EAAmB;AAG9C,EAAA,IAAI,iBAAiB,CAAA,EAAG;AACtB,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,MAAM,UAAA,GAAa,KAAK,aAAA,EAAc;AAEtC,EAAA,MAAM,oBAAA,GAAuB,KAAK,uBAAA,EAAwB;AAG1D,EAAA,KAAA,IAAS,QAAQ,oBAAA,CAAqB,MAAA,GAAS,CAAA,EAAG,KAAA,IAAS,GAAG,KAAA,EAAA,EAAS;AAErE,IAAA,MAAM,YAAA,GAAe,qBAAqB,KAAK,CAAA;AAG/C,IAAA,MAAM,aAAA,GAAgB,aAAa,MAAA,EAAO;AAE1C,IAAA,MAAM,cAAA,GAAiB,UAAA,CAAW,qBAAA,CAAsB,aAAa,CAAA,CAAE,IAAA;AAGvE,IAAA,IAAI,aAAA,KAAkB,iBAAiB,CAAA,EAAG;AAExC,MAAA,IAAI,YAAA,CAAa,OAAA,EAAQ,KAAM,GAAA,CAAI,WAAW,uBAAA,EAAyB;AACrE,QAAA,MAAM,WAAA,GAAc,YAAA,CAAa,OAAA,EAAQ,CAAE,IAAA,EAAK;AAGhD,QAAA,MAAM,gBAAgB,4BAAA,CAA6B,IAAA;AAAA,UAAK,CAAC,MAAA,KACvD,WAAA,CAAY,QAAA,CAAS,MAAM;AAAA,SAC7B;AAEA,QAAA,IAAI,kBAAkB,MAAA,EAAW;AAE/B,UAAA,IAAI,oBAAoB,MAAA,EAAW;AACjC,YAAA,OAAO,IAAA;AAAA,UACT;AAGA,UAAA,MAAM,WAAA,GAAc,WAAA,CACjB,KAAA,CAAM,WAAA,CAAY,OAAA,CAAQ,aAAa,CAAC,CAAA,CACxC,OAAA,CAAQ,aAAA,EAAe,EAAE,CAAA,CACzB,IAAA,EAAK;AAGR,UAAA,IAAI,gBAAgB,EAAA,EAAI;AACtB,YAAA,OAAO,IAAA;AAAA,UACT;AAGA,UAAA,MAAM,kBAAA,GAAqB,WAAA,CACxB,KAAA,CAAM,GAAG,CAAA,CACT,IAAI,CAAC,IAAA,KAAS,IAAA,CAAK,IAAA,EAAM,CAAA;AAE5B,UAAA,OAAO,kBAAA,CAAmB,SAAS,eAAe,CAAA;AAAA,QACpD;AAAA,MACF;AAKA,MAAA,OAAO,KAAA;AAAA,IACT;AAGA,IAAA,IAAI,cAAA,GAAiB,gBAAgB,CAAA,EAAG;AACtC,MAAA;AAAA,IACF;AAAA,EACF;AAEA,EAAA,OAAO,KAAA;AACT;;;;"}
|
|
@@ -2,7 +2,7 @@ import * as ts from 'ts-morph';
|
|
|
2
2
|
export declare const isAsConstNode: (node: ts.Node) => node is ts.AsExpression & Readonly<{
|
|
3
3
|
type: ts.TypeReferenceNode & Readonly<{
|
|
4
4
|
typeName: ts.Identifier & Readonly<{
|
|
5
|
-
text:
|
|
5
|
+
text: 'const';
|
|
6
6
|
}>;
|
|
7
7
|
typeArguments: undefined;
|
|
8
8
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-as-const-node.d.mts","sourceRoot":"","sources":["../../../src/functions/functions/is-as-const-node.mts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,UAAU,CAAC;AAE/B,eAAO,MAAM,aAAa,
|
|
1
|
+
{"version":3,"file":"is-as-const-node.d.mts","sourceRoot":"","sources":["../../../src/functions/functions/is-as-const-node.mts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,UAAU,CAAC;AAE/B,eAAO,MAAM,aAAa,SAClB,EAAE,CAAC,IAAI,KACZ,IAAI,IAAI,EAAE,CAAC,YAAY,GACxB,QAAQ,CAAC;IACP,IAAI,EAAE,EAAE,CAAC,iBAAiB,GACxB,QAAQ,CAAC;QACP,QAAQ,EAAE,EAAE,CAAC,UAAU,GACrB,QAAQ,CAAC;YACP,IAAI,EAAE,OAAO,CAAC;SACf,CAAC,CAAC;QACL,aAAa,EAAE,SAAS,CAAC;KAC1B,CAAC,CAAC;CACN,CAgCF,CAAC"}
|
|
@@ -2,29 +2,21 @@ import { Arr } from 'ts-data-forge';
|
|
|
2
2
|
import * as tsm from 'ts-morph';
|
|
3
3
|
|
|
4
4
|
const isAsConstNode = (node) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (!typeNameNode.isKind(tsm.SyntaxKind.Identifier)) {
|
|
21
|
-
// 'as const' uses a simple Identifier 'const', not a QualifiedName
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
// 6. Check if the Identifier's text is 'const'
|
|
25
|
-
// and that there are no type arguments (as const doesn't have them)
|
|
26
|
-
return (typeNameNode.getText() === 'const' &&
|
|
27
|
-
Arr.isArrayOfLength(typeNode.getTypeArguments(), 0));
|
|
5
|
+
if (!node.isKind(tsm.SyntaxKind.AsExpression)) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
const typeNode = node.getTypeNode();
|
|
9
|
+
if (typeNode === void 0) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
if (!typeNode.isKind(tsm.SyntaxKind.TypeReference)) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
const typeNameNode = typeNode.getTypeName();
|
|
16
|
+
if (!typeNameNode.isKind(tsm.SyntaxKind.Identifier)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return typeNameNode.getText() === "const" && Arr.isArrayOfLength(typeNode.getTypeArguments(), 0);
|
|
28
20
|
};
|
|
29
21
|
|
|
30
22
|
export { isAsConstNode };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-as-const-node.mjs","sources":["../../../src/functions/functions/is-as-const-node.mts"],"sourcesContent":[
|
|
1
|
+
{"version":3,"file":"is-as-const-node.mjs","sources":["../../../src/functions/functions/is-as-const-node.mts"],"sourcesContent":["import { Arr } from 'ts-data-forge';\nimport * as ts from 'ts-morph';\n\nexport const isAsConstNode = (\n node: ts.Node,\n): node is ts.AsExpression &\n Readonly<{\n type: ts.TypeReferenceNode &\n Readonly<{\n typeName: ts.Identifier &\n Readonly<{\n text: 'const';\n }>;\n typeArguments: undefined;\n }>;\n }> => {\n if (!node.isKind(ts.SyntaxKind.AsExpression)) {\n return false;\n }\n\n // 2. Get the 'type' node from the AsExpression\n const typeNode = node.getTypeNode();\n\n if (typeNode === undefined) {\n return false; // Should have a type node for 'as const'\n }\n\n // 3. Check if the 'type' node is a TypeReference\n if (!typeNode.isKind(ts.SyntaxKind.TypeReference)) {\n return false;\n }\n\n // 4. Get the 'typeName' from the TypeReference\n const typeNameNode = typeNode.getTypeName();\n\n // 5. Check if the 'typeName' is an Identifier\n if (!typeNameNode.isKind(ts.SyntaxKind.Identifier)) {\n // 'as const' uses a simple Identifier 'const', not a QualifiedName\n return false;\n }\n\n // 6. Check if the Identifier's text is 'const'\n // and that there are no type arguments (as const doesn't have them)\n return (\n typeNameNode.getText() === 'const' &&\n Arr.isArrayOfLength(typeNode.getTypeArguments(), 0)\n );\n};\n"],"names":["ts"],"mappings":";;;AAGO,MAAM,aAAA,GAAgB,CAC3B,IAAA,KAWM;AACN,EAAA,IAAI,CAAC,IAAA,CAAK,MAAA,CAAOA,GAAA,CAAG,UAAA,CAAW,YAAY,CAAA,EAAG;AAC5C,IAAA,OAAO,KAAA;AAAA,EACT;AAGA,EAAA,MAAM,QAAA,GAAW,KAAK,WAAA,EAAY;AAElC,EAAA,IAAI,aAAa,MAAA,EAAW;AAC1B,IAAA,OAAO,KAAA;AAAA,EACT;AAGA,EAAA,IAAI,CAAC,QAAA,CAAS,MAAA,CAAOA,GAAA,CAAG,UAAA,CAAW,aAAa,CAAA,EAAG;AACjD,IAAA,OAAO,KAAA;AAAA,EACT;AAGA,EAAA,MAAM,YAAA,GAAe,SAAS,WAAA,EAAY;AAG1C,EAAA,IAAI,CAAC,YAAA,CAAa,MAAA,CAAOA,GAAA,CAAG,UAAA,CAAW,UAAU,CAAA,EAAG;AAElD,IAAA,OAAO,KAAA;AAAA,EACT;AAIA,EAAA,OACE,YAAA,CAAa,SAAQ,KAAM,OAAA,IAC3B,IAAI,eAAA,CAAgB,QAAA,CAAS,gBAAA,EAAiB,EAAG,CAAC,CAAA;AAEtD;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-atomic-type-node.d.mts","sourceRoot":"","sources":["../../../src/functions/functions/is-atomic-type-node.mts"],"names":[],"mappings":"AACA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAkBlD,MAAM,MAAM,cAAc,GAAG,GAAG,CAAC,IAAI,GACnC,QAAQ,CACJ,GAAG,CAAC,eAAe,GACnB,GAAG,CAAC,uBAAuB,GAC3B,CAAC,GAAG,CAAC,QAAQ,GAAG;IACd,IAAI,EACA,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,cAAc,GAC7B,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,gBAAgB,GAC/B,GAAG,CAAC,UAAU,CAAC,WAAW,GAC1B,GAAG,CAAC,UAAU,CAAC,UAAU,GACzB,GAAG,CAAC,UAAU,CAAC,cAAc,GAC7B,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,YAAY,CAAC;CACjC,CAAC,CACL,CAAC;AAEJ;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,
|
|
1
|
+
{"version":3,"file":"is-atomic-type-node.d.mts","sourceRoot":"","sources":["../../../src/functions/functions/is-atomic-type-node.mts"],"names":[],"mappings":"AACA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAkBlD,MAAM,MAAM,cAAc,GAAG,GAAG,CAAC,IAAI,GACnC,QAAQ,CACJ,GAAG,CAAC,eAAe,GACnB,GAAG,CAAC,uBAAuB,GAC3B,CAAC,GAAG,CAAC,QAAQ,GAAG;IACd,IAAI,EACA,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,cAAc,GAC7B,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,gBAAgB,GAC/B,GAAG,CAAC,UAAU,CAAC,WAAW,GAC1B,GAAG,CAAC,UAAU,CAAC,UAAU,GACzB,GAAG,CAAC,UAAU,CAAC,cAAc,GAC7B,GAAG,CAAC,UAAU,CAAC,aAAa,GAC5B,GAAG,CAAC,UAAU,CAAC,YAAY,CAAC;CACjC,CAAC,CACL,CAAC;AAEJ;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,SACrB,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,KAC3B,IAAI,IAAI,cAmBV,CAAC"}
|
|
@@ -1,45 +1,31 @@
|
|
|
1
1
|
import { ISet } from 'ts-data-forge';
|
|
2
2
|
import * as tsm from 'ts-morph';
|
|
3
3
|
|
|
4
|
-
// Define the set of SyntaxKinds that represent primitive type keywords
|
|
5
4
|
const atomicTypeKeywordKinds = ISet.create([
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
5
|
+
tsm.SyntaxKind.StringKeyword,
|
|
6
|
+
tsm.SyntaxKind.BooleanKeyword,
|
|
7
|
+
tsm.SyntaxKind.NumberKeyword,
|
|
8
|
+
tsm.SyntaxKind.BigIntKeyword,
|
|
9
|
+
tsm.SyntaxKind.SymbolKeyword,
|
|
10
|
+
tsm.SyntaxKind.UndefinedKeyword,
|
|
11
|
+
tsm.SyntaxKind.VoidKeyword,
|
|
12
|
+
tsm.SyntaxKind.AnyKeyword,
|
|
13
|
+
tsm.SyntaxKind.UnknownKeyword,
|
|
14
|
+
tsm.SyntaxKind.ObjectKeyword,
|
|
15
|
+
// Note: 'object' is sometimes considered primitive in TS type system context
|
|
16
|
+
tsm.SyntaxKind.NeverKeyword
|
|
17
17
|
]);
|
|
18
|
-
/**
|
|
19
|
-
* Checks if a given ts-morph node represents a primitive type node.
|
|
20
|
-
* This includes keyword types (string, number, etc.), literal types (null, "abc", 123),
|
|
21
|
-
* and template literal types.
|
|
22
|
-
*
|
|
23
|
-
* @param node - The ts-morph node to check.
|
|
24
|
-
* @returns True if the node represents a primitive type node, false otherwise.
|
|
25
|
-
* Acts as a type guard.
|
|
26
|
-
*/
|
|
27
18
|
const isAtomicTypeNode = (node) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// if (ts.Node.isTypeNode(node) && primitiveKeywordKinds.has(node.getKind())) {
|
|
39
|
-
if (atomicTypeKeywordKinds.has(node.getKind())) {
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
return false;
|
|
19
|
+
if (node.isKind(tsm.SyntaxKind.LiteralType)) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
if (node.isKind(tsm.SyntaxKind.TemplateLiteralType)) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
if (atomicTypeKeywordKinds.has(node.getKind())) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
43
29
|
};
|
|
44
30
|
|
|
45
31
|
export { isAtomicTypeNode };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-atomic-type-node.mjs","sources":["../../../src/functions/functions/is-atomic-type-node.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIA;AACA,MAAM,sBAAsB,GAAG,IAAI,CAAC,MAAM,CAAiB;IACzD,GAAG,CAAC,UAAU,CAAC,aAAa;IAC5B,GAAG,CAAC,UAAU,CAAC,cAAc;IAC7B,GAAG,CAAC,UAAU,CAAC,aAAa;IAC5B,GAAG,CAAC,UAAU,CAAC,aAAa;IAC5B,GAAG,CAAC,UAAU,CAAC,aAAa;IAC5B,GAAG,CAAC,UAAU,CAAC,gBAAgB;IAC/B,GAAG,CAAC,UAAU,CAAC,WAAW;IAC1B,GAAG,CAAC,UAAU,CAAC,UAAU;IACzB,GAAG,CAAC,UAAU,CAAC,cAAc;AAC7B,IAAA,GAAG,CAAC,UAAU,CAAC,aAAa;IAC5B,GAAG,CAAC,UAAU,CAAC,YAAY;AAC5B,CAAA,CAAC;AAuBF;;;;;;;;AAQG;AACI,MAAM,gBAAgB,GAAG,CAC9B,IAA4B,KACF;;IAE1B,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE;AAC3C,QAAA,OAAO,IAAI;IACb;;IAGA,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,mBAAmB,CAAC,EAAE;AACnD,QAAA,OAAO,IAAI;IACb;;;;IAKA,IAAI,sBAAsB,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE;AAC9C,QAAA,OAAO,IAAI;IACb;AAEA,IAAA,OAAO,KAAK;AACd;;;;"}
|
|
1
|
+
{"version":3,"file":"is-atomic-type-node.mjs","sources":["../../../src/functions/functions/is-atomic-type-node.mts"],"sourcesContent":["import { ISet } from 'ts-data-forge';\nimport * as tsm from 'ts-morph';\nimport { type DeepReadonly } from 'ts-type-forge';\n\n// Define the set of SyntaxKinds that represent primitive type keywords\nconst atomicTypeKeywordKinds = ISet.create<tsm.SyntaxKind>([\n tsm.SyntaxKind.StringKeyword,\n tsm.SyntaxKind.BooleanKeyword,\n tsm.SyntaxKind.NumberKeyword,\n tsm.SyntaxKind.BigIntKeyword,\n tsm.SyntaxKind.SymbolKeyword,\n tsm.SyntaxKind.UndefinedKeyword,\n tsm.SyntaxKind.VoidKeyword,\n tsm.SyntaxKind.AnyKeyword,\n tsm.SyntaxKind.UnknownKeyword,\n tsm.SyntaxKind.ObjectKeyword, // Note: 'object' is sometimes considered primitive in TS type system context\n tsm.SyntaxKind.NeverKeyword,\n]);\n\n// transformer-ignore-next-line convert-to-readonly\nexport type AtomicTypeNode = tsm.Node &\n Readonly<\n | tsm.LiteralTypeNode\n | tsm.TemplateLiteralTypeNode\n | (tsm.TypeNode & {\n kind:\n | tsm.SyntaxKind.StringKeyword\n | tsm.SyntaxKind.BooleanKeyword\n | tsm.SyntaxKind.NumberKeyword\n | tsm.SyntaxKind.BigIntKeyword\n | tsm.SyntaxKind.SymbolKeyword\n | tsm.SyntaxKind.UndefinedKeyword\n | tsm.SyntaxKind.VoidKeyword\n | tsm.SyntaxKind.AnyKeyword\n | tsm.SyntaxKind.UnknownKeyword\n | tsm.SyntaxKind.ObjectKeyword\n | tsm.SyntaxKind.NeverKeyword;\n })\n >;\n\n/**\n * Checks if a given ts-morph node represents a primitive type node.\n * This includes keyword types (string, number, etc.), literal types (null, \"abc\", 123),\n * and template literal types.\n *\n * @param node - The ts-morph node to check.\n * @returns True if the node represents a primitive type node, false otherwise.\n * Acts as a type guard.\n */\nexport const isAtomicTypeNode = (\n node: DeepReadonly<tsm.Node>,\n): node is AtomicTypeNode => {\n // Check for literal types (null, \"aaa\", 1.23, 456n, true, false)\n if (node.isKind(tsm.SyntaxKind.LiteralType)) {\n return true;\n }\n\n // Check for template literal types (`abc${expr}def`)\n if (node.isKind(tsm.SyntaxKind.TemplateLiteralType)) {\n return true;\n }\n\n // Check if it's a TypeNode and its kind is one of the primitive keywords\n // Node.isTypeNode(node) ensures we only check nodes that represent types\n // if (ts.Node.isTypeNode(node) && primitiveKeywordKinds.has(node.getKind())) {\n if (atomicTypeKeywordKinds.has(node.getKind())) {\n return true;\n }\n\n return false;\n};\n\nif (import.meta.vitest !== undefined) {\n // Helper to get a specific type node from source code\n const getTypeNodeFromSource = (\n sourceCode: string,\n typeName: string,\n ): tsm.TypeNode => {\n const project = new tsm.Project({ useInMemoryFileSystem: true });\n\n const sourceFile = project.createSourceFile('test.ts', sourceCode);\n\n const typeAlias = sourceFile.getTypeAliasOrThrow(typeName);\n\n return typeAlias.getTypeNodeOrThrow();\n };\n\n // Helper to get the first node of a specific kind from source code\n const getFirstNodeOfKind = <T extends tsm.Node>(\n sourceCode: string,\n kind: tsm.SyntaxKind,\n ): T | undefined => {\n const project = new tsm.Project({ useInMemoryFileSystem: true });\n\n const sourceFile = project.createSourceFile('test.ts', sourceCode);\n\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n return sourceFile.getFirstDescendantByKind(kind) as T | undefined;\n };\n\n describe('isAtomicTypeNode', () => {\n describe('positive cases', () => {\n test.each([\n {\n name: 'string',\n code: 'type Test = string;',\n kind: tsm.SyntaxKind.StringKeyword,\n },\n {\n name: 'number',\n code: 'type Test = number;',\n kind: tsm.SyntaxKind.NumberKeyword,\n },\n {\n name: 'boolean',\n code: 'type Test = boolean;',\n kind: tsm.SyntaxKind.BooleanKeyword,\n },\n {\n name: 'bigint',\n code: 'type Test = bigint;',\n kind: tsm.SyntaxKind.BigIntKeyword,\n },\n {\n name: 'symbol',\n code: 'type Test = symbol;',\n kind: tsm.SyntaxKind.SymbolKeyword,\n },\n {\n name: 'undefined',\n code: 'type Test = undefined;',\n kind: tsm.SyntaxKind.UndefinedKeyword,\n },\n {\n name: 'void',\n code: 'type Test = void;',\n kind: tsm.SyntaxKind.VoidKeyword,\n },\n {\n name: 'any',\n code: 'type Test = any;',\n kind: tsm.SyntaxKind.AnyKeyword,\n },\n {\n name: 'unknown',\n code: 'type Test = unknown;',\n kind: tsm.SyntaxKind.UnknownKeyword,\n },\n {\n name: 'never',\n code: 'type Test = never;',\n kind: tsm.SyntaxKind.NeverKeyword,\n },\n {\n name: 'object',\n code: 'type Test = object;',\n kind: tsm.SyntaxKind.ObjectKeyword,\n }, // Included based on original code\n {\n name: 'null',\n code: 'type Test = null;',\n kind: tsm.SyntaxKind.LiteralType,\n },\n {\n name: 'string literal (\"hello\")',\n code: 'type Test = \"hello\";',\n kind: tsm.SyntaxKind.LiteralType,\n },\n {\n name: 'number literal (123)',\n code: 'type Test = 123;',\n kind: tsm.SyntaxKind.LiteralType,\n },\n {\n name: 'bigint literal (123n)',\n code: 'type Test = 123n;',\n kind: tsm.SyntaxKind.LiteralType,\n },\n {\n name: 'boolean literal (true)',\n code: 'type Test = true;',\n kind: tsm.SyntaxKind.LiteralType,\n },\n {\n name: 'boolean literal (false)',\n code: 'type Test = false;',\n kind: tsm.SyntaxKind.LiteralType,\n },\n {\n name: 'TemplateLiteral',\n // eslint-disable-next-line no-template-curly-in-string\n code: 'type Test = `a${string}b`;',\n kind: tsm.SyntaxKind.TemplateLiteralType,\n },\n ])('$name', ({ code, kind }) => {\n const node = getTypeNodeFromSource(code, 'Test');\n\n expect(node.getKind()).toBe(kind);\n\n assert.isTrue(isAtomicTypeNode(node));\n });\n });\n\n describe('negative cases', () => {\n test.each([\n {\n name: 'Date TypeReference',\n code: 'type Test = Date;',\n kind: tsm.SyntaxKind.TypeReference,\n },\n {\n name: 'Array Type',\n code: 'type Test = number[];',\n kind: tsm.SyntaxKind.ArrayType,\n },\n {\n name: 'Tuple Type',\n code: 'type Test = [string, number];',\n kind: tsm.SyntaxKind.TupleType,\n },\n {\n name: 'Type Literal',\n code: 'type Test = { a: number };',\n kind: tsm.SyntaxKind.TypeLiteral,\n },\n {\n name: 'Readonly Type Operator',\n code: 'type Test = readonly string[];',\n kind: tsm.SyntaxKind.TypeOperator,\n },\n {\n name: 'Union Type',\n code: 'type Test = string | number;',\n kind: tsm.SyntaxKind.UnionType,\n },\n {\n name: 'Intersection Type',\n code: 'type Test = A & B;',\n kind: tsm.SyntaxKind.IntersectionType,\n },\n {\n name: 'Mapped Type',\n code: 'type Test = { [K in keyof T]: T[K] };',\n kind: tsm.SyntaxKind.MappedType,\n }, // Needs T defined\n {\n name: 'Parenthesized Type',\n code: 'type Test = (string);',\n kind: tsm.SyntaxKind.ParenthesizedType,\n },\n {\n name: 'Conditional Type',\n code: 'type Test = T extends U ? X : Y;',\n kind: tsm.SyntaxKind.ConditionalType,\n }, // Needs T, U, X, Y\n {\n name: 'Indexed Access Type',\n code: 'type Test = T[K];',\n kind: tsm.SyntaxKind.IndexedAccessType,\n }, // Needs T, K\n {\n name: 'Type Query (typeof)',\n code: 'type Test = typeof myVar;',\n kind: tsm.SyntaxKind.TypeQuery,\n },\n ])('$name', ({ code, kind }) => {\n const node = getTypeNodeFromSource(code, 'Test');\n\n expect(node.getKind()).toBe(kind); // Verify node type\n\n assert.isFalse(isAtomicTypeNode(node));\n });\n\n test.each([\n {\n name: 'Type Predicate',\n code: 'function isString(x: any): x is string { return typeof x === \"string\"; }',\n kind: tsm.SyntaxKind.TypePredicate,\n }, // Node is part of function sig\n {\n name: 'This Type',\n code: 'class C { method(): this {} }',\n kind: tsm.SyntaxKind.ThisType,\n }, // Node is part of method sig\n {\n name: 'Infer Type',\n code: 'type Test<T> = T extends Promise<infer R> ? R : T;',\n kind: tsm.SyntaxKind.InferType,\n }, // Needs context\n ])('$name (not a type node)', ({ code, kind }) => {\n const node = getFirstNodeOfKind(code, kind);\n\n expect(node).toBeDefined();\n\n if (node === undefined) {\n throw new Error('Node should be defined');\n }\n\n assert.isFalse(isAtomicTypeNode(node));\n });\n });\n });\n}\n"],"names":[],"mappings":";;;AAKA,MAAM,sBAAA,GAAyB,KAAK,MAAA,CAAuB;AAAA,EACzD,IAAI,UAAA,CAAW,aAAA;AAAA,EACf,IAAI,UAAA,CAAW,cAAA;AAAA,EACf,IAAI,UAAA,CAAW,aAAA;AAAA,EACf,IAAI,UAAA,CAAW,aAAA;AAAA,EACf,IAAI,UAAA,CAAW,aAAA;AAAA,EACf,IAAI,UAAA,CAAW,gBAAA;AAAA,EACf,IAAI,UAAA,CAAW,WAAA;AAAA,EACf,IAAI,UAAA,CAAW,UAAA;AAAA,EACf,IAAI,UAAA,CAAW,cAAA;AAAA,EACf,IAAI,UAAA,CAAW,aAAA;AAAA;AAAA,EACf,IAAI,UAAA,CAAW;AACjB,CAAC,CAAA;AAgCM,MAAM,gBAAA,GAAmB,CAC9B,IAAA,KAC2B;AAE3B,EAAA,IAAI,IAAA,CAAK,MAAA,CAAO,GAAA,CAAI,UAAA,CAAW,WAAW,CAAA,EAAG;AAC3C,IAAA,OAAO,IAAA;AAAA,EACT;AAGA,EAAA,IAAI,IAAA,CAAK,MAAA,CAAO,GAAA,CAAI,UAAA,CAAW,mBAAmB,CAAA,EAAG;AACnD,IAAA,OAAO,IAAA;AAAA,EACT;AAKA,EAAA,IAAI,sBAAA,CAAuB,GAAA,CAAI,IAAA,CAAK,OAAA,EAAS,CAAA,EAAG;AAC9C,IAAA,OAAO,IAAA;AAAA,EACT;AAEA,EAAA,OAAO,KAAA;AACT;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-conditional-type-distribution-guard.d.mts","sourceRoot":"","sources":["../../../src/functions/functions/is-conditional-type-distribution-guard.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,kCAAkC,
|
|
1
|
+
{"version":3,"file":"is-conditional-type-distribution-guard.d.mts","sourceRoot":"","sources":["../../../src/functions/functions/is-conditional-type-distribution-guard.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,kCAAkC,SAAU,GAAG,CAAC,IAAI,KAAG,OAsBnE,CAAC"}
|