@cbnventures/nova 0.19.0 → 0.20.0
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/build/package.json +6 -3
- package/build/src/cli/generate/github/issue-template.d.ts.map +1 -1
- package/build/src/cli/generate/github/issue-template.js +4 -93
- package/build/src/cli/generate/github/issue-template.js.map +1 -1
- package/build/src/cli/generate/must-haves/agent-conventions.d.ts.map +1 -1
- package/build/src/cli/generate/must-haves/agent-conventions.js +12 -3
- package/build/src/cli/generate/must-haves/agent-conventions.js.map +1 -1
- package/build/src/cli/generate/must-haves/dotenv.d.ts +4 -7
- package/build/src/cli/generate/must-haves/dotenv.d.ts.map +1 -1
- package/build/src/cli/generate/must-haves/dotenv.js +120 -417
- package/build/src/cli/generate/must-haves/dotenv.js.map +1 -1
- package/build/src/cli/generate/must-haves/gitignore.d.ts +0 -8
- package/build/src/cli/generate/must-haves/gitignore.d.ts.map +1 -1
- package/build/src/cli/generate/must-haves/gitignore.js +7 -395
- package/build/src/cli/generate/must-haves/gitignore.js.map +1 -1
- package/build/src/cli/recipe/github/sync-identity.js +1 -1
- package/build/src/cli/recipe/github/sync-identity.js.map +1 -1
- package/build/src/cli/recipe/github/sync-policies.d.ts.map +1 -1
- package/build/src/cli/recipe/github/sync-policies.js +8 -1
- package/build/src/cli/recipe/github/sync-policies.js.map +1 -1
- package/build/src/cli/utility/changelog.d.ts +2 -0
- package/build/src/cli/utility/changelog.d.ts.map +1 -1
- package/build/src/cli/utility/changelog.js +154 -2
- package/build/src/cli/utility/changelog.js.map +1 -1
- package/build/src/cli/utility/initialize.d.ts +3 -0
- package/build/src/cli/utility/initialize.d.ts.map +1 -1
- package/build/src/cli/utility/initialize.js +305 -1
- package/build/src/cli/utility/initialize.js.map +1 -1
- package/build/src/lib/dotenv.d.ts +3 -0
- package/build/src/lib/dotenv.d.ts.map +1 -0
- package/build/src/lib/dotenv.js +66 -0
- package/build/src/lib/dotenv.js.map +1 -0
- package/build/src/lib/file-discovery.d.ts +3 -0
- package/build/src/lib/file-discovery.d.ts.map +1 -0
- package/build/src/lib/file-discovery.js +32 -0
- package/build/src/lib/file-discovery.js.map +1 -0
- package/build/src/lib/item.d.ts +3 -1
- package/build/src/lib/item.d.ts.map +1 -1
- package/build/src/lib/item.js +9 -0
- package/build/src/lib/item.js.map +1 -1
- package/build/src/lib/markdown-slug.d.ts +4 -0
- package/build/src/lib/markdown-slug.d.ts.map +1 -0
- package/build/src/lib/markdown-slug.js +25 -0
- package/build/src/lib/markdown-slug.js.map +1 -0
- package/build/src/lib/nova-config.d.ts +5 -0
- package/build/src/lib/nova-config.d.ts.map +1 -1
- package/build/src/lib/nova-config.js +135 -13
- package/build/src/lib/nova-config.js.map +1 -1
- package/build/src/lib/regex.d.ts +68 -0
- package/build/src/lib/regex.d.ts.map +1 -1
- package/build/src/lib/regex.js +70 -2
- package/build/src/lib/regex.js.map +1 -1
- package/build/src/lib/type-declaration-engine.d.ts +32 -0
- package/build/src/lib/type-declaration-engine.d.ts.map +1 -0
- package/build/src/lib/type-declaration-engine.js +770 -0
- package/build/src/lib/type-declaration-engine.js.map +1 -0
- package/build/src/lib/utility.d.ts.map +1 -1
- package/build/src/lib/utility.js +8 -8
- package/build/src/lib/utility.js.map +1 -1
- package/build/src/rules/eslint/conventions/require-kebab-case-filename.d.ts.map +1 -1
- package/build/src/rules/eslint/conventions/require-kebab-case-filename.js.map +1 -1
- package/build/src/rules/eslint/jsdoc/require-jsdoc-hierarchy.d.ts.map +1 -1
- package/build/src/rules/eslint/jsdoc/require-jsdoc-hierarchy.js +3 -3
- package/build/src/rules/eslint/jsdoc/require-jsdoc-hierarchy.js.map +1 -1
- package/build/src/rules/eslint/jsdoc/require-jsdoc-param-alignment.d.ts.map +1 -1
- package/build/src/rules/eslint/jsdoc/require-jsdoc-param-alignment.js +3 -2
- package/build/src/rules/eslint/jsdoc/require-jsdoc-param-alignment.js.map +1 -1
- package/build/src/rules/eslint/jsdoc/require-jsdoc-param-name.js +2 -2
- package/build/src/rules/eslint/jsdoc/require-jsdoc-param-name.js.map +1 -1
- package/build/src/rules/eslint/jsdoc/require-jsdoc-since.d.ts +1 -1
- package/build/src/rules/eslint/jsdoc/require-jsdoc-since.d.ts.map +1 -1
- package/build/src/rules/eslint/jsdoc/require-jsdoc-since.js +102 -6
- package/build/src/rules/eslint/jsdoc/require-jsdoc-since.js.map +1 -1
- package/build/src/rules/eslint/regex/no-regex-literals.d.ts +1 -0
- package/build/src/rules/eslint/regex/no-regex-literals.d.ts.map +1 -1
- package/build/src/rules/eslint/regex/no-regex-literals.js +39 -1
- package/build/src/rules/eslint/regex/no-regex-literals.js.map +1 -1
- package/build/src/rules/eslint/typescript/require-type-naming.js +2 -2
- package/build/src/rules/eslint/typescript/require-type-naming.js.map +1 -1
- package/build/src/rules/vitest/dotenv/register.d.ts +3 -0
- package/build/src/rules/vitest/dotenv/register.d.ts.map +1 -0
- package/build/src/rules/vitest/dotenv/register.js +21 -0
- package/build/src/rules/vitest/dotenv/register.js.map +1 -0
- package/build/src/rules/vitest/dotenv/rules.d.ts +3 -0
- package/build/src/rules/vitest/dotenv/rules.d.ts.map +1 -0
- package/build/src/rules/vitest/dotenv/rules.js +32 -0
- package/build/src/rules/vitest/dotenv/rules.js.map +1 -0
- package/build/src/rules/vitest/enable.d.ts +3 -0
- package/build/src/rules/vitest/enable.d.ts.map +1 -0
- package/build/src/rules/vitest/enable.js +7 -0
- package/build/src/rules/vitest/enable.js.map +1 -0
- package/build/src/rules/vitest/frontmatter/register.d.ts +3 -0
- package/build/src/rules/vitest/frontmatter/register.d.ts.map +1 -0
- package/build/src/rules/vitest/frontmatter/register.js +51 -0
- package/build/src/rules/vitest/frontmatter/register.js.map +1 -0
- package/build/src/rules/vitest/frontmatter/rules.d.ts +15 -0
- package/build/src/rules/vitest/frontmatter/rules.d.ts.map +1 -0
- package/build/src/rules/vitest/frontmatter/rules.js +498 -0
- package/build/src/rules/vitest/frontmatter/rules.js.map +1 -0
- package/build/src/rules/vitest/index.d.ts +8 -0
- package/build/src/rules/vitest/index.d.ts.map +1 -0
- package/build/src/rules/vitest/index.js +7 -0
- package/build/src/rules/vitest/index.js.map +1 -0
- package/build/src/rules/vitest/link/register.d.ts +3 -0
- package/build/src/rules/vitest/link/register.d.ts.map +1 -0
- package/build/src/rules/vitest/link/register.js +43 -0
- package/build/src/rules/vitest/link/register.js.map +1 -0
- package/build/src/rules/vitest/link/rules.d.ts +12 -0
- package/build/src/rules/vitest/link/rules.d.ts.map +1 -0
- package/build/src/rules/vitest/link/rules.js +313 -0
- package/build/src/rules/vitest/link/rules.js.map +1 -0
- package/build/src/rules/vitest/markdown-table/register.d.ts +3 -0
- package/build/src/rules/vitest/markdown-table/register.d.ts.map +1 -0
- package/build/src/rules/vitest/markdown-table/register.js +25 -0
- package/build/src/rules/vitest/markdown-table/register.js.map +1 -0
- package/build/src/rules/vitest/markdown-table/rules.d.ts +3 -0
- package/build/src/rules/vitest/markdown-table/rules.d.ts.map +1 -0
- package/build/src/rules/vitest/markdown-table/rules.js +99 -0
- package/build/src/rules/vitest/markdown-table/rules.js.map +1 -0
- package/build/src/rules/vitest/terminology/register.d.ts +3 -0
- package/build/src/rules/vitest/terminology/register.d.ts.map +1 -0
- package/build/src/rules/vitest/terminology/register.js +38 -0
- package/build/src/rules/vitest/terminology/register.js.map +1 -0
- package/build/src/rules/vitest/terminology/rules.d.ts +10 -0
- package/build/src/rules/vitest/terminology/rules.d.ts.map +1 -0
- package/build/src/rules/vitest/terminology/rules.js +202 -0
- package/build/src/rules/vitest/terminology/rules.js.map +1 -0
- package/build/src/rules/vitest/type-declarations/register.d.ts +3 -0
- package/build/src/rules/vitest/type-declarations/register.d.ts.map +1 -0
- package/build/src/rules/vitest/type-declarations/register.js +30 -0
- package/build/src/rules/vitest/type-declarations/register.js.map +1 -0
- package/build/src/rules/vitest/type-declarations/rules.d.ts +11 -0
- package/build/src/rules/vitest/type-declarations/rules.d.ts.map +1 -0
- package/build/src/rules/vitest/type-declarations/rules.js +666 -0
- package/build/src/rules/vitest/type-declarations/rules.js.map +1 -0
- package/build/src/toolkit/logger.d.ts.map +1 -1
- package/build/src/toolkit/logger.js +3 -1
- package/build/src/toolkit/logger.js.map +1 -1
- package/build/src/types/cli/generate/github/issue-template.d.ts +10 -30
- package/build/src/types/cli/generate/github/workflows.d.ts +7 -7
- package/build/src/types/cli/generate/must-haves/agent-conventions.d.ts +8 -1
- package/build/src/types/cli/generate/must-haves/dotenv.d.ts +79 -327
- package/build/src/types/cli/generate/must-haves/gitignore.d.ts +12 -342
- package/build/src/types/cli/recipe/github/sync-features.d.ts +1 -1
- package/build/src/types/cli/recipe/github/sync-identity.d.ts +2 -2
- package/build/src/types/cli/recipe/github/sync-policies.d.ts +3 -1
- package/build/src/types/cli/utility/changelog.d.ts +92 -0
- package/build/src/types/cli/utility/initialize.d.ts +195 -0
- package/build/src/types/lib/constants.d.ts +1 -1
- package/build/src/types/lib/dotenv.d.ts +67 -0
- package/build/src/types/lib/file-discovery.d.ts +40 -0
- package/build/src/types/lib/item.d.ts +19 -0
- package/build/src/types/lib/markdown-slug.d.ts +25 -0
- package/build/src/types/lib/nova-config.d.ts +108 -16
- package/build/src/types/lib/regex.d.ts +193 -18
- package/build/src/types/lib/type-declaration-engine.d.ts +1036 -0
- package/build/src/types/lib/utility.d.ts +2 -2
- package/build/src/types/lib/workflow-templates.d.ts +1 -1
- package/build/src/types/rules/eslint/jsdoc/require-jsdoc-hierarchy.d.ts +2 -2
- package/build/src/types/rules/eslint/jsdoc/require-jsdoc-since.d.ts +71 -1
- package/build/src/types/rules/eslint/regex/no-regex-literals.d.ts +44 -0
- package/build/src/types/rules/eslint/typescript/require-type-naming.d.ts +3 -3
- package/build/src/types/rules/vitest/dotenv/register.d.ts +31 -0
- package/build/src/types/rules/vitest/dotenv/rules.d.ts +29 -0
- package/build/src/types/rules/vitest/enable.d.ts +12 -0
- package/build/src/types/rules/vitest/frontmatter/register.d.ts +63 -0
- package/build/src/types/rules/vitest/frontmatter/rules.d.ts +430 -0
- package/build/src/types/rules/vitest/index.d.ts +399 -0
- package/build/src/types/rules/vitest/link/register.d.ts +79 -0
- package/build/src/types/rules/vitest/link/rules.d.ts +368 -0
- package/build/src/types/rules/vitest/markdown-table/register.d.ts +33 -0
- package/build/src/types/rules/vitest/markdown-table/rules.d.ts +55 -0
- package/build/src/types/rules/vitest/terminology/register.d.ts +48 -0
- package/build/src/types/rules/vitest/terminology/rules.d.ts +190 -0
- package/build/src/types/rules/vitest/type-declarations/register.d.ts +46 -0
- package/build/src/types/rules/vitest/type-declarations/rules.d.ts +820 -0
- package/build/src/types/shared.d.ts +71 -4
- package/build/src/types/tests/cli/generate/github/issue-template.test.d.ts +34 -17
- package/build/src/types/tests/cli/generate/github/workflows-helpers.test.d.ts +8 -8
- package/build/src/types/tests/cli/generate/must-haves/agent-conventions.test.d.ts +79 -11
- package/build/src/types/tests/cli/generate/must-haves/dotenv.test.d.ts +105 -15
- package/build/src/types/tests/cli/generate/must-haves/gitignore.test.d.ts +11 -20
- package/build/src/types/tests/cli/recipe/github/handle-gh-failure.test.d.ts +3 -3
- package/build/src/types/tests/cli/recipe/github/sync-features.test.d.ts +17 -17
- package/build/src/types/tests/cli/recipe/github/sync-identity.test.d.ts +69 -23
- package/build/src/types/tests/cli/recipe/github/sync-policies.test.d.ts +89 -20
- package/build/src/types/tests/cli/scaffold/docs/docusaurus.test.d.ts +4 -0
- package/build/src/types/tests/cli/utility/changelog.test.d.ts +262 -0
- package/build/src/types/tests/cli/utility/initialize.test.d.ts +31 -0
- package/build/src/types/tests/lib/dotenv.test.d.ts +95 -0
- package/build/src/types/tests/lib/file-discovery.test.d.ts +37 -0
- package/build/src/types/tests/lib/item.test.d.ts +21 -0
- package/build/src/types/tests/lib/markdown-slug.test.d.ts +24 -0
- package/build/src/types/tests/lib/nova-config.test.d.ts +263 -0
- package/build/src/types/tests/lib/regex.test.d.ts +252 -0
- package/build/src/types/tests/rules/eslint/jsdoc/require-jsdoc-since.test.d.ts +18 -0
- package/build/src/types/tests/rules/vitest/dotenv.test.d.ts +19 -0
- package/build/src/types/tests/rules/vitest/enable.test.d.ts +27 -0
- package/build/src/types/tests/rules/vitest/frontmatter.test.d.ts +8 -0
- package/build/src/types/tests/rules/vitest/link.test.d.ts +74 -0
- package/build/src/types/tests/rules/vitest/markdown-table.test.d.ts +28 -0
- package/build/src/types/tests/rules/vitest/terminology.test.d.ts +39 -0
- package/build/src/types/tests/rules/vitest/type-declarations.test.d.ts +10 -0
- package/build/src/types/tests/scaffold-version-drift.test.d.ts +42 -0
- package/build/src/types/tests/since-version.test.d.ts +137 -0
- package/build/src/types/tests/toolkit/logger.test.d.ts +45 -0
- package/build/src/types/tests/type-declarations.test.d.ts +34 -1821
- package/build/src/types/toolkit/logger.d.ts +4 -0
- package/build/templates/generators/github/workflows/check-sponsor-gated-issues/base.yml +1 -1
- package/build/templates/generators/github/workflows/publish/base.yml +1 -1
- package/build/templates/generators/github/workflows/publish/targets/aws-amplify-nextjs.yml +1 -1
- package/build/templates/generators/github/workflows/publish/targets/cloudflare-pages-docusaurus.yml +1 -1
- package/build/templates/generators/github/workflows/publish/targets/docker-hub.yml +1 -1
- package/build/templates/generators/github/workflows/publish/targets/ghcr.yml +1 -1
- package/build/templates/generators/github/workflows/publish/targets/github-action.yml +1 -1
- package/build/templates/generators/github/workflows/publish/targets/github-packages.yml +1 -1
- package/build/templates/generators/github/workflows/publish/targets/github-pages-docusaurus.yml +4 -4
- package/build/templates/generators/github/workflows/publish/targets/npm.yml +1 -1
- package/build/templates/generators/github/workflows/publish/targets/vercel-nextjs.yml +1 -1
- package/build/templates/generators/must-haves/agent-conventions/AGENTS.md +1 -2
- package/build/templates/generators/must-haves/agent-conventions/CLAUDE.md +1 -2
- package/build/templates/generators/must-haves/agent-conventions/conventions/java.md +5 -5
- package/build/templates/generators/must-haves/agent-conventions/conventions/kotlin.md +4 -4
- package/build/templates/generators/must-haves/agent-conventions/conventions/php.md +2 -2
- package/build/templates/generators/must-haves/agent-conventions/conventions/typescript.md +6 -6
- package/build/templates/generators/must-haves/dotenv/env +5 -2
- package/build/templates/generators/must-haves/dotenv/env.sample +6 -3
- package/build/templates/generators/must-haves/gitignore/gitignore +6 -0
- package/build/templates/scaffold/app/express/package.json +5 -5
- package/build/templates/scaffold/app/nextjs/package.json +7 -7
- package/build/templates/scaffold/app/vite/package.json +3 -3
- package/build/templates/scaffold/app/workers/package.json +3 -3
- package/build/templates/scaffold/docs/docusaurus/docs/intro.md +6 -0
- package/build/templates/scaffold/docs/docusaurus/package.json +11 -7
- package/build/templates/scaffold/docs/docusaurus/src/tests/frontmatter.test.ts +21 -0
- package/build/templates/scaffold/docs/docusaurus/src/tests/link.test.ts +14 -0
- package/build/templates/scaffold/docs/docusaurus/src/tests/markdown-table.test.ts +14 -0
- package/build/templates/scaffold/docs/docusaurus/src/tests/terminology.test.ts +14 -0
- package/build/templates/scaffold/docs/docusaurus/vitest.config.ts +22 -0
- package/build/templates/scaffold/docs/docusaurus/vitest.setup.ts +26 -0
- package/package.json +6 -3
- package/build/templates/generators/must-haves/agent-conventions/cursorrules +0 -55
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { relative, sep, } from 'node:path';
|
|
3
|
+
import { glob } from 'glob';
|
|
4
|
+
import { createSourceFile, forEachChild, isArrowFunction, isCallExpression, isClassDeclaration, isClassExpression, isConstructorDeclaration, isFunctionDeclaration, isFunctionExpression, isGetAccessorDeclaration, isIdentifier, isMethodDeclaration, isPrivateIdentifier, isSetAccessorDeclaration, isStringLiteral, isTypePredicateNode, isTypeReferenceNode, isVariableStatement, ScriptTarget, } from 'typescript';
|
|
5
|
+
import { LIB_REGEX_PATTERN_ANGLE_BRACKET_GENERIC, LIB_REGEX_PATTERN_CAMEL_CASE_WORDS, LIB_REGEX_PATTERN_COMPLEX_TYPE_RHS_CHARS, LIB_REGEX_PATTERN_DTS_ARRAY_ALIAS, LIB_REGEX_PATTERN_DTS_PROPERTY_LINE, LIB_REGEX_PATTERN_EMPTY_ARRAY_BRACKETS, LIB_REGEX_PATTERN_EXPORT_TYPE_NAME, LIB_REGEX_PATTERN_IMPORT_TYPE_BLOCK, LIB_REGEX_PATTERN_NON_ALPHANUMERIC_RUN, LIB_REGEX_PATTERN_REGEX_SPECIAL_CHARS, LIB_REGEX_PATTERN_SINGLE_PASCAL_TYPE, LIB_REGEX_PATTERN_TYPED_BODY_DECLARATION, } from './regex.js';
|
|
6
|
+
export const testConfig = {
|
|
7
|
+
standaloneTypeFiles: [
|
|
8
|
+
'/fetch-response.d.ts',
|
|
9
|
+
'/shared.d.ts',
|
|
10
|
+
],
|
|
11
|
+
typeRoots: [
|
|
12
|
+
'src',
|
|
13
|
+
'utils',
|
|
14
|
+
],
|
|
15
|
+
};
|
|
16
|
+
export async function discoverSourceFiles(config = {}) {
|
|
17
|
+
const packageRoot = config['packageRoot'] ?? process.cwd();
|
|
18
|
+
const typeRoots = config['typeRoots'] ?? ['src'];
|
|
19
|
+
const patterns = typeRoots.map((typeRoot) => `${typeRoot}/**/*.ts`);
|
|
20
|
+
const ignorePatterns = typeRoots.map((typeRoot) => `${typeRoot}/**/*.d.ts`);
|
|
21
|
+
const matched = await glob(patterns, {
|
|
22
|
+
cwd: packageRoot,
|
|
23
|
+
absolute: true,
|
|
24
|
+
ignore: ignorePatterns,
|
|
25
|
+
});
|
|
26
|
+
return matched.sort();
|
|
27
|
+
}
|
|
28
|
+
export async function discoverTypeFiles(config = {}) {
|
|
29
|
+
const packageRoot = config['packageRoot'] ?? process.cwd();
|
|
30
|
+
const typeRoots = config['typeRoots'] ?? ['src'];
|
|
31
|
+
const patterns = typeRoots.map((typeRoot) => `${typeRoot}/types/**/*.d.ts`);
|
|
32
|
+
const matched = await glob(patterns, {
|
|
33
|
+
cwd: packageRoot,
|
|
34
|
+
absolute: true,
|
|
35
|
+
});
|
|
36
|
+
return matched.sort();
|
|
37
|
+
}
|
|
38
|
+
export function deriveClassPrefix(filePath, config = {}) {
|
|
39
|
+
const packageRoot = config['packageRoot'] ?? process.cwd();
|
|
40
|
+
const typeRoots = config['typeRoots'] ?? ['src'];
|
|
41
|
+
const currentDirectory = packageRoot;
|
|
42
|
+
const relativePath = relative(currentDirectory, filePath);
|
|
43
|
+
let relativeCleaned = relativePath;
|
|
44
|
+
for (const typeRoot of typeRoots) {
|
|
45
|
+
relativeCleaned = relativeCleaned.replace(`${typeRoot}/types/`, '');
|
|
46
|
+
relativeCleaned = relativeCleaned.replace(`${typeRoot}/`, '');
|
|
47
|
+
}
|
|
48
|
+
const segments = relativeCleaned
|
|
49
|
+
.replace('.d.ts', '')
|
|
50
|
+
.replace('.ts', '')
|
|
51
|
+
.replace('.test', '')
|
|
52
|
+
.split(sep)
|
|
53
|
+
.join('/')
|
|
54
|
+
.split('/');
|
|
55
|
+
const camelCaseWordsPattern = new RegExp(LIB_REGEX_PATTERN_CAMEL_CASE_WORDS.source, 'g');
|
|
56
|
+
return segments.map((segment) => {
|
|
57
|
+
if (segment.includes('-') === false
|
|
58
|
+
&& segment.charAt(0) === segment.charAt(0).toUpperCase()
|
|
59
|
+
&& segment.length > 0) {
|
|
60
|
+
const words = segment.match(camelCaseWordsPattern);
|
|
61
|
+
if (words !== null) {
|
|
62
|
+
return words.map((word) => {
|
|
63
|
+
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
|
64
|
+
}).join('');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return segment.split('-').map((part) => {
|
|
68
|
+
return part.charAt(0).toUpperCase() + part.slice(1);
|
|
69
|
+
}).join('');
|
|
70
|
+
}).join('_');
|
|
71
|
+
}
|
|
72
|
+
export function extractImportedNames(lines) {
|
|
73
|
+
const importedNames = new Set();
|
|
74
|
+
let inImportBlock = false;
|
|
75
|
+
for (const line of lines) {
|
|
76
|
+
if (line.startsWith('import type') === true) {
|
|
77
|
+
inImportBlock = true;
|
|
78
|
+
const inlineMatch = line.match(LIB_REGEX_PATTERN_IMPORT_TYPE_BLOCK);
|
|
79
|
+
if (inlineMatch !== null && inlineMatch[1] !== undefined) {
|
|
80
|
+
const inlineMatchCapture = inlineMatch[1];
|
|
81
|
+
const specifiers = inlineMatchCapture.split(',').map((specifier) => specifier.trim());
|
|
82
|
+
for (const specifier of specifiers) {
|
|
83
|
+
importedNames.add(specifier);
|
|
84
|
+
}
|
|
85
|
+
inImportBlock = false;
|
|
86
|
+
}
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (inImportBlock === true) {
|
|
90
|
+
const trimmed = line.trim().replace(',', '');
|
|
91
|
+
if (trimmed.startsWith('}') === true) {
|
|
92
|
+
inImportBlock = false;
|
|
93
|
+
}
|
|
94
|
+
else if (trimmed !== '' && trimmed.startsWith('//') === false) {
|
|
95
|
+
importedNames.add(trimmed);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return importedNames;
|
|
100
|
+
}
|
|
101
|
+
export function buildDtsSections(dtsLines, sourceSections, classPrefix) {
|
|
102
|
+
const sortedSections = [...sourceSections].sort((sectionA, sectionB) => sectionB.length - sectionA.length);
|
|
103
|
+
const sectionMap = new Map();
|
|
104
|
+
const sectionOrder = [];
|
|
105
|
+
for (const line of dtsLines) {
|
|
106
|
+
if (line.startsWith('export type ') === false) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const match = line.match(LIB_REGEX_PATTERN_EXPORT_TYPE_NAME);
|
|
110
|
+
if (match === null || match[1] === undefined) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const typeName = match[1];
|
|
114
|
+
let owningSection = '';
|
|
115
|
+
for (const candidate of sortedSections) {
|
|
116
|
+
if (typeName.startsWith(`${candidate}_`) === true) {
|
|
117
|
+
owningSection = candidate;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (owningSection === '' && typeName.startsWith(`${classPrefix}_`) === true) {
|
|
122
|
+
owningSection = classPrefix;
|
|
123
|
+
}
|
|
124
|
+
if (owningSection === '') {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (sectionMap.has(owningSection) === false) {
|
|
128
|
+
sectionMap.set(owningSection, {
|
|
129
|
+
prefix: owningSection,
|
|
130
|
+
typeLines: [],
|
|
131
|
+
});
|
|
132
|
+
sectionOrder.push(owningSection);
|
|
133
|
+
}
|
|
134
|
+
const section = sectionMap.get(owningSection);
|
|
135
|
+
section['typeLines'].push(line);
|
|
136
|
+
}
|
|
137
|
+
return sectionOrder.map((prefix) => sectionMap.get(prefix));
|
|
138
|
+
}
|
|
139
|
+
export function extractReferencedTypes(line, classPrefix) {
|
|
140
|
+
const match = line.match(LIB_REGEX_PATTERN_EXPORT_TYPE_NAME);
|
|
141
|
+
if (match === null || match[1] === undefined) {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
const typeName = match[1];
|
|
145
|
+
const rightSide = line.slice(line.indexOf('=') + 1);
|
|
146
|
+
const referencedTypes = [];
|
|
147
|
+
const typePattern = new RegExp(`${classPrefix}_\\w+`, 'g');
|
|
148
|
+
let typeMatch = typePattern.exec(rightSide);
|
|
149
|
+
while (typeMatch !== null) {
|
|
150
|
+
const referencedType = typeMatch[0];
|
|
151
|
+
if (referencedType !== typeName) {
|
|
152
|
+
referencedTypes.push(referencedType);
|
|
153
|
+
}
|
|
154
|
+
typeMatch = typePattern.exec(rightSide);
|
|
155
|
+
}
|
|
156
|
+
return referencedTypes;
|
|
157
|
+
}
|
|
158
|
+
export function deriveDtsMapping(typeRoots) {
|
|
159
|
+
return {
|
|
160
|
+
sourceToDts: (file) => {
|
|
161
|
+
let mapped = file;
|
|
162
|
+
for (const typeRoot of typeRoots) {
|
|
163
|
+
if (file.includes(`/${typeRoot}/`) === true) {
|
|
164
|
+
mapped = file.replace(`/${typeRoot}/`, `/${typeRoot}/types/`);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return mapped.replace('.ts', '.d.ts');
|
|
169
|
+
},
|
|
170
|
+
dtsToSource: (file) => {
|
|
171
|
+
return file.replace('/types/', '/').replace('.d.ts', '.ts');
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
export function deriveSourcePath(dtsPath) {
|
|
176
|
+
return dtsPath.replace('/types/', '/').replace('.d.ts', '.ts');
|
|
177
|
+
}
|
|
178
|
+
export async function fileExists(filePath) {
|
|
179
|
+
try {
|
|
180
|
+
await readFile(filePath, 'utf-8');
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
export function extractTypeNames(typeLines) {
|
|
188
|
+
const typeNames = [];
|
|
189
|
+
for (const line of typeLines) {
|
|
190
|
+
const match = line.match(LIB_REGEX_PATTERN_EXPORT_TYPE_NAME);
|
|
191
|
+
if (match !== null && match[1] !== undefined) {
|
|
192
|
+
typeNames.push(match[1]);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return typeNames;
|
|
196
|
+
}
|
|
197
|
+
export function findFirstOccurrence(sourceLines, typeName) {
|
|
198
|
+
let inImportBlock = false;
|
|
199
|
+
for (let i = 0; i < sourceLines.length; i += 1) {
|
|
200
|
+
const line = sourceLines[i];
|
|
201
|
+
if (line === undefined) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (line.startsWith('import ') === true || line.startsWith('import type') === true) {
|
|
205
|
+
inImportBlock = true;
|
|
206
|
+
}
|
|
207
|
+
if (inImportBlock === true) {
|
|
208
|
+
if (line.includes(' from ') === true) {
|
|
209
|
+
inImportBlock = false;
|
|
210
|
+
}
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const typeNamePattern = new RegExp(`\\b${typeName}\\b`);
|
|
214
|
+
if (typeNamePattern.test(line) === true) {
|
|
215
|
+
return i;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return -1;
|
|
219
|
+
}
|
|
220
|
+
export function extractObjectTypes(lines, classPrefix) {
|
|
221
|
+
const objectTypes = [];
|
|
222
|
+
const typeLineMap = new Map();
|
|
223
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
224
|
+
const line = lines[i];
|
|
225
|
+
if (line === undefined) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const typeDefMatch = line.match(new RegExp(`^export type (${classPrefix}\\w+)`));
|
|
229
|
+
if (typeDefMatch !== null && typeDefMatch[1] !== undefined) {
|
|
230
|
+
typeLineMap.set(typeDefMatch[1], i);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
234
|
+
const line2 = lines[i];
|
|
235
|
+
if (line2 === undefined) {
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const objectMatch = line2.match(new RegExp(`^export type (${classPrefix}\\w+) = (?:Readonly<)?\\{$`));
|
|
239
|
+
if (objectMatch === null || objectMatch[1] === undefined) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const properties = [];
|
|
243
|
+
for (let j = i + 1; j < lines.length; j += 1) {
|
|
244
|
+
const propertyLine = lines[j];
|
|
245
|
+
if (propertyLine === undefined) {
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
const trimmedProperty = propertyLine.trim();
|
|
249
|
+
if (trimmedProperty.startsWith('}') === true) {
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
const propertyMatch = trimmedProperty.match(LIB_REGEX_PATTERN_DTS_PROPERTY_LINE);
|
|
253
|
+
if (propertyMatch !== null
|
|
254
|
+
&& propertyMatch[1] !== undefined
|
|
255
|
+
&& propertyMatch[2] !== undefined) {
|
|
256
|
+
const propertyKey = propertyMatch[1];
|
|
257
|
+
const propertyValueType = propertyMatch[2];
|
|
258
|
+
const typeLineIndex = typeLineMap.get(propertyValueType) ?? -1;
|
|
259
|
+
properties.push({
|
|
260
|
+
key: propertyKey,
|
|
261
|
+
valueType: propertyValueType,
|
|
262
|
+
typeLineIndex,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const objectName = objectMatch[1];
|
|
267
|
+
objectTypes.push({
|
|
268
|
+
name: objectName,
|
|
269
|
+
lineIndex: i,
|
|
270
|
+
properties,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return objectTypes;
|
|
274
|
+
}
|
|
275
|
+
const sourceFileCache = new Map();
|
|
276
|
+
export function parseSourceFile(filePath, content) {
|
|
277
|
+
const cached = sourceFileCache.get(filePath);
|
|
278
|
+
if (cached !== undefined && cached['content'] === content) {
|
|
279
|
+
return cached['sourceFile'];
|
|
280
|
+
}
|
|
281
|
+
const sourceFile = createSourceFile(filePath, content, ScriptTarget.Latest, true);
|
|
282
|
+
sourceFileCache.set(filePath, {
|
|
283
|
+
content, sourceFile,
|
|
284
|
+
});
|
|
285
|
+
return sourceFile;
|
|
286
|
+
}
|
|
287
|
+
export function extractFunctionParams(filePath, content) {
|
|
288
|
+
const sourceFile = parseSourceFile(filePath, content);
|
|
289
|
+
const params = [];
|
|
290
|
+
function visit(node) {
|
|
291
|
+
let parameterList = undefined;
|
|
292
|
+
if (isFunctionDeclaration(node) === true
|
|
293
|
+
|| isMethodDeclaration(node) === true
|
|
294
|
+
|| isConstructorDeclaration(node) === true) {
|
|
295
|
+
parameterList = node['parameters'];
|
|
296
|
+
}
|
|
297
|
+
else if (isVariableStatement(node) === true) {
|
|
298
|
+
for (const decl of node.declarationList.declarations) {
|
|
299
|
+
if (decl.initializer !== undefined
|
|
300
|
+
&& (isArrowFunction(decl.initializer) === true
|
|
301
|
+
|| isFunctionExpression(decl.initializer) === true)) {
|
|
302
|
+
parameterList = decl.initializer.parameters;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (parameterList !== undefined) {
|
|
307
|
+
for (const param of parameterList) {
|
|
308
|
+
const paramNode = param;
|
|
309
|
+
if (paramNode['name'] !== undefined
|
|
310
|
+
&& isIdentifier(paramNode['name']) === true
|
|
311
|
+
&& paramNode['type'] !== undefined
|
|
312
|
+
&& isTypeReferenceNode(paramNode['type']) === true
|
|
313
|
+
&& isIdentifier(paramNode['type']['typeName']) === true) {
|
|
314
|
+
const lineNumber = sourceFile.getLineAndCharacterOfPosition((param).getStart()).line + 1;
|
|
315
|
+
const paramRecord = {
|
|
316
|
+
paramName: paramNode['name']['text'],
|
|
317
|
+
typeName: paramNode['type']['typeName']['text'],
|
|
318
|
+
lineNumber,
|
|
319
|
+
};
|
|
320
|
+
params.push(paramRecord);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
forEachChild(node, visit);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
forEachChild(sourceFile, visit);
|
|
328
|
+
return params;
|
|
329
|
+
}
|
|
330
|
+
export function extractBodyDeclarations(lines) {
|
|
331
|
+
const declarations = [];
|
|
332
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
333
|
+
const line = lines[i];
|
|
334
|
+
if (line === undefined) {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const match = line.match(LIB_REGEX_PATTERN_TYPED_BODY_DECLARATION);
|
|
338
|
+
if (match !== null
|
|
339
|
+
&& match[1] !== undefined
|
|
340
|
+
&& match[2] !== undefined
|
|
341
|
+
&& match[3] !== undefined) {
|
|
342
|
+
const declaration = {
|
|
343
|
+
keyword: match[1],
|
|
344
|
+
varName: match[2],
|
|
345
|
+
typeName: match[3],
|
|
346
|
+
lineNumber: i + 1,
|
|
347
|
+
};
|
|
348
|
+
declarations.push(declaration);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return declarations;
|
|
352
|
+
}
|
|
353
|
+
export function extractTopLevelIdentifiers(filePath, content) {
|
|
354
|
+
const sourceFile = parseSourceFile(filePath, content);
|
|
355
|
+
const identifiers = [];
|
|
356
|
+
function getLine(node) {
|
|
357
|
+
return sourceFile.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
358
|
+
}
|
|
359
|
+
forEachChild(sourceFile, (node) => {
|
|
360
|
+
if (isClassDeclaration(node) === true && node.name !== undefined) {
|
|
361
|
+
const identifier = {
|
|
362
|
+
name: node.name.text,
|
|
363
|
+
kind: 'class',
|
|
364
|
+
lineNumber: getLine(node),
|
|
365
|
+
};
|
|
366
|
+
identifiers.push(identifier);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
if (isFunctionDeclaration(node) === true && node.name !== undefined) {
|
|
370
|
+
const identifier2 = {
|
|
371
|
+
name: node.name.text,
|
|
372
|
+
kind: 'function',
|
|
373
|
+
lineNumber: getLine(node),
|
|
374
|
+
};
|
|
375
|
+
identifiers.push(identifier2);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (isVariableStatement(node) === true) {
|
|
379
|
+
for (const decl of node.declarationList.declarations) {
|
|
380
|
+
if (decl.name !== undefined
|
|
381
|
+
&& isIdentifier(decl.name) === true
|
|
382
|
+
&& decl.initializer !== undefined
|
|
383
|
+
&& (isArrowFunction(decl.initializer) === true
|
|
384
|
+
|| isFunctionExpression(decl.initializer) === true
|
|
385
|
+
|| isClassExpression(decl.initializer) === true)) {
|
|
386
|
+
const identifier3 = {
|
|
387
|
+
name: decl.name.text,
|
|
388
|
+
kind: 'const',
|
|
389
|
+
lineNumber: getLine(decl),
|
|
390
|
+
};
|
|
391
|
+
identifiers.push(identifier3);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return;
|
|
396
|
+
});
|
|
397
|
+
return identifiers;
|
|
398
|
+
}
|
|
399
|
+
export function buildSourceSectionMap(filePath, content, classPrefix) {
|
|
400
|
+
const sourceFile = parseSourceFile(filePath, content);
|
|
401
|
+
const sectionMap = new Map();
|
|
402
|
+
const maxDepth = 5000;
|
|
403
|
+
function getLine(node) {
|
|
404
|
+
return sourceFile.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
405
|
+
}
|
|
406
|
+
function pascalCase(name) {
|
|
407
|
+
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
408
|
+
}
|
|
409
|
+
let currentDepth = 0;
|
|
410
|
+
function tagAllChildren(node, section) {
|
|
411
|
+
if (currentDepth >= maxDepth) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
currentDepth += 1;
|
|
415
|
+
forEachChild(node, (child) => visit(child, section));
|
|
416
|
+
currentDepth -= 1;
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
function visit(node, section) {
|
|
420
|
+
if (isClassDeclaration(node) === true && node.name !== undefined) {
|
|
421
|
+
const newSection = `${section}_${pascalCase(node.name.text)}`;
|
|
422
|
+
sectionMap.set(getLine(node), newSection);
|
|
423
|
+
tagAllChildren(node, newSection);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
const accessor = node;
|
|
427
|
+
if ((isMethodDeclaration(node) === true
|
|
428
|
+
|| isGetAccessorDeclaration(node) === true
|
|
429
|
+
|| isSetAccessorDeclaration(node) === true)
|
|
430
|
+
&& accessor.name !== undefined
|
|
431
|
+
&& (isIdentifier(accessor.name) === true
|
|
432
|
+
|| isPrivateIdentifier(accessor.name) === true)) {
|
|
433
|
+
const rawMethodName = accessor.name.text;
|
|
434
|
+
const cleanMethodName = (rawMethodName.startsWith('#') === true) ? rawMethodName.slice(1) : rawMethodName;
|
|
435
|
+
const newSection2 = `${section}_${pascalCase(cleanMethodName)}`;
|
|
436
|
+
sectionMap.set(getLine(node), newSection2);
|
|
437
|
+
tagAllChildren(node, newSection2);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (isConstructorDeclaration(node) === true) {
|
|
441
|
+
const newSection3 = `${section}_Constructor`;
|
|
442
|
+
sectionMap.set(getLine(node), newSection3);
|
|
443
|
+
tagAllChildren(node, newSection3);
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (isFunctionDeclaration(node) === true && node.name !== undefined) {
|
|
447
|
+
const newSection4 = `${section}_${pascalCase(node.name.text)}`;
|
|
448
|
+
sectionMap.set(getLine(node), newSection4);
|
|
449
|
+
tagAllChildren(node, newSection4);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
if (isVariableStatement(node) === true) {
|
|
453
|
+
let hasInitializer = false;
|
|
454
|
+
let subSection = section;
|
|
455
|
+
for (const decl of node.declarationList.declarations) {
|
|
456
|
+
if (decl.name !== undefined
|
|
457
|
+
&& isIdentifier(decl.name) === true
|
|
458
|
+
&& decl.initializer !== undefined
|
|
459
|
+
&& (isArrowFunction(decl.initializer) === true
|
|
460
|
+
|| isFunctionExpression(decl.initializer) === true
|
|
461
|
+
|| isClassExpression(decl.initializer) === true)) {
|
|
462
|
+
hasInitializer = true;
|
|
463
|
+
subSection = `${section}_${pascalCase(decl.name.text)}`;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
sectionMap.set(getLine(node), section);
|
|
467
|
+
if (hasInitializer === true) {
|
|
468
|
+
for (const decl of node.declarationList.declarations) {
|
|
469
|
+
if (decl.initializer === undefined) {
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (isArrowFunction(decl.initializer) === true || isFunctionExpression(decl.initializer) === true) {
|
|
473
|
+
if (decl.initializer.body !== undefined) {
|
|
474
|
+
tagAllChildren(decl.initializer.body, subSection);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
else if (isClassExpression(decl.initializer) === true) {
|
|
478
|
+
tagAllChildren(decl.initializer, subSection);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
tagAllChildren(node, section);
|
|
484
|
+
}
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
if (isCallExpression(node) === true
|
|
488
|
+
&& node.arguments.length >= 2
|
|
489
|
+
&& node.arguments[0] !== undefined
|
|
490
|
+
&& isStringLiteral(node.arguments[0]) === true) {
|
|
491
|
+
let callbackArg = undefined;
|
|
492
|
+
for (let argIndex = 1; argIndex < node.arguments.length; argIndex += 1) {
|
|
493
|
+
const arg = node.arguments[argIndex];
|
|
494
|
+
if (arg !== undefined
|
|
495
|
+
&& (isArrowFunction(arg) === true
|
|
496
|
+
|| isFunctionExpression(arg) === true)) {
|
|
497
|
+
callbackArg = arg;
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
if (callbackArg !== undefined) {
|
|
502
|
+
const stringArg = node.arguments[0];
|
|
503
|
+
const chunk = parseDescribeString(stringArg.text);
|
|
504
|
+
if (chunk !== '') {
|
|
505
|
+
const newSection5 = `${section}_${chunk}`;
|
|
506
|
+
sectionMap.set(getLine(node), section);
|
|
507
|
+
tagAllChildren(callbackArg, newSection5);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
sectionMap.set(getLine(node), section);
|
|
513
|
+
tagAllChildren(node, section);
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
forEachChild(sourceFile, (child) => visit(child, classPrefix));
|
|
517
|
+
return sectionMap;
|
|
518
|
+
}
|
|
519
|
+
export function parseDescribeString(input) {
|
|
520
|
+
const pieces = input
|
|
521
|
+
.split(LIB_REGEX_PATTERN_NON_ALPHANUMERIC_RUN)
|
|
522
|
+
.filter((piece) => piece.length > 0)
|
|
523
|
+
.map((piece) => piece.charAt(0).toUpperCase() + piece.slice(1));
|
|
524
|
+
return pieces.join('');
|
|
525
|
+
}
|
|
526
|
+
export function stripUnderscorePrefix(input) {
|
|
527
|
+
if (input.startsWith('_') === true) {
|
|
528
|
+
return input.slice(1);
|
|
529
|
+
}
|
|
530
|
+
return input;
|
|
531
|
+
}
|
|
532
|
+
export function checkTypeNameUniqueness(declarations) {
|
|
533
|
+
const violations = [];
|
|
534
|
+
const seen = new Map();
|
|
535
|
+
for (const declaration of declarations) {
|
|
536
|
+
const existing = seen.get(declaration['typeName']);
|
|
537
|
+
if (existing !== undefined) {
|
|
538
|
+
const violation = `Two declarations would produce the same type name '${declaration['typeName']}'. First: line ${existing['lineNumber']} ('${existing['name']}'); duplicate: line ${declaration['lineNumber']} ('${declaration['name']}'). Rename one of the declarations so the type names differ.`;
|
|
539
|
+
violations.push(violation);
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
542
|
+
seen.set(declaration['typeName'], {
|
|
543
|
+
name: declaration['name'],
|
|
544
|
+
lineNumber: declaration['lineNumber'],
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return violations;
|
|
549
|
+
}
|
|
550
|
+
export function detectInlineTypedCallbacks(filePath, content) {
|
|
551
|
+
const sourceFile = parseSourceFile(filePath, content);
|
|
552
|
+
const callbacks = [];
|
|
553
|
+
function visit(node) {
|
|
554
|
+
if ((isArrowFunction(node) === true
|
|
555
|
+
|| isFunctionExpression(node) === true)
|
|
556
|
+
&& node.parent !== undefined) {
|
|
557
|
+
let isFunctionTypedConst = false;
|
|
558
|
+
let walker = node.parent;
|
|
559
|
+
while (walker !== undefined) {
|
|
560
|
+
if (isVariableStatement(walker) === true) {
|
|
561
|
+
isFunctionTypedConst = true;
|
|
562
|
+
break;
|
|
563
|
+
}
|
|
564
|
+
if (walker['kind'] !== undefined
|
|
565
|
+
&& (isCallExpression(walker) === true
|
|
566
|
+
|| isArrowFunction(walker) === true
|
|
567
|
+
|| isFunctionExpression(walker) === true)) {
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
walker = walker['parent'];
|
|
571
|
+
}
|
|
572
|
+
if (isFunctionTypedConst === false) {
|
|
573
|
+
for (const param of node.parameters) {
|
|
574
|
+
const paramNode = param;
|
|
575
|
+
if (paramNode['name'] !== undefined
|
|
576
|
+
&& isIdentifier(paramNode['name']) === true
|
|
577
|
+
&& paramNode['type'] !== undefined
|
|
578
|
+
&& isTypeReferenceNode(paramNode['type']) === true
|
|
579
|
+
&& isIdentifier(paramNode['type']['typeName']) === true) {
|
|
580
|
+
const lineNumber = sourceFile.getLineAndCharacterOfPosition(param.getStart()).line + 1;
|
|
581
|
+
const callback = {
|
|
582
|
+
paramName: paramNode['name']['text'],
|
|
583
|
+
typeName: paramNode['type']['typeName']['text'],
|
|
584
|
+
lineNumber,
|
|
585
|
+
};
|
|
586
|
+
callbacks.push(callback);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
forEachChild(node, visit);
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
forEachChild(sourceFile, visit);
|
|
595
|
+
return callbacks;
|
|
596
|
+
}
|
|
597
|
+
export function extractArrayTypes(lines) {
|
|
598
|
+
const arrayTypes = [];
|
|
599
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
600
|
+
const line = lines[i];
|
|
601
|
+
if (line === undefined) {
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
const match = line.match(LIB_REGEX_PATTERN_DTS_ARRAY_ALIAS);
|
|
605
|
+
if (match !== null
|
|
606
|
+
&& match[1] !== undefined
|
|
607
|
+
&& match[2] !== undefined) {
|
|
608
|
+
const arrayType = {
|
|
609
|
+
arrayTypeName: match[1],
|
|
610
|
+
elementTypeName: match[2],
|
|
611
|
+
lineIndex: i,
|
|
612
|
+
};
|
|
613
|
+
arrayTypes.push(arrayType);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
return arrayTypes;
|
|
617
|
+
}
|
|
618
|
+
export function extractFunctionReturns(filePath, content) {
|
|
619
|
+
const sourceFile = parseSourceFile(filePath, content);
|
|
620
|
+
const returnRecords = [];
|
|
621
|
+
function visit(node) {
|
|
622
|
+
if (isFunctionDeclaration(node) === true
|
|
623
|
+
|| isMethodDeclaration(node) === true
|
|
624
|
+
|| isConstructorDeclaration(node) === true) {
|
|
625
|
+
const fnNode = node;
|
|
626
|
+
const typeNode = fnNode['type'];
|
|
627
|
+
if (typeNode !== undefined) {
|
|
628
|
+
let returnType = '';
|
|
629
|
+
let typeGuardFlag = false;
|
|
630
|
+
if (isTypePredicateNode(typeNode) === true) {
|
|
631
|
+
typeGuardFlag = true;
|
|
632
|
+
const predicateNode = typeNode;
|
|
633
|
+
if (predicateNode['type'] !== undefined
|
|
634
|
+
&& isTypeReferenceNode(predicateNode['type']) === true
|
|
635
|
+
&& isIdentifier(predicateNode['type']['typeName']) === true) {
|
|
636
|
+
returnType = predicateNode['type']['typeName']['text'];
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
else if (isTypeReferenceNode(typeNode) === true && isIdentifier(typeNode['typeName']) === true) {
|
|
640
|
+
returnType = typeNode['typeName']['text'];
|
|
641
|
+
}
|
|
642
|
+
if (returnType !== '') {
|
|
643
|
+
const lineNumber = sourceFile.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
644
|
+
const returnRecord = {
|
|
645
|
+
returnType,
|
|
646
|
+
isTypeGuard: typeGuardFlag,
|
|
647
|
+
lineNumber,
|
|
648
|
+
};
|
|
649
|
+
returnRecords.push(returnRecord);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
forEachChild(node, visit);
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
forEachChild(sourceFile, visit);
|
|
657
|
+
return returnRecords;
|
|
658
|
+
}
|
|
659
|
+
export function isAliasToForeignType(typeName, dtsContent, classPrefix) {
|
|
660
|
+
const escapedTypeName = typeName.replace(new RegExp(LIB_REGEX_PATTERN_REGEX_SPECIAL_CHARS.source, 'g'), '\\$&');
|
|
661
|
+
const pattern = new RegExp(`^export type ${escapedTypeName}\\s*=\\s*([^;]+);`, 'm');
|
|
662
|
+
const match = dtsContent.match(pattern);
|
|
663
|
+
if (match === null || match[1] === undefined) {
|
|
664
|
+
return false;
|
|
665
|
+
}
|
|
666
|
+
let rhs = match[1].trim();
|
|
667
|
+
rhs = rhs.replace(new RegExp(LIB_REGEX_PATTERN_ANGLE_BRACKET_GENERIC.source, 'g'), '');
|
|
668
|
+
rhs = rhs.replace(new RegExp(LIB_REGEX_PATTERN_EMPTY_ARRAY_BRACKETS.source, 'g'), '');
|
|
669
|
+
if (LIB_REGEX_PATTERN_COMPLEX_TYPE_RHS_CHARS.test(rhs) === true) {
|
|
670
|
+
return false;
|
|
671
|
+
}
|
|
672
|
+
const typeMatch = rhs.trim().match(LIB_REGEX_PATTERN_SINGLE_PASCAL_TYPE);
|
|
673
|
+
if (typeMatch === null || typeMatch[1] === undefined) {
|
|
674
|
+
return false;
|
|
675
|
+
}
|
|
676
|
+
const leftmostType = typeMatch[1];
|
|
677
|
+
if (leftmostType.includes('_') === false) {
|
|
678
|
+
return false;
|
|
679
|
+
}
|
|
680
|
+
if (leftmostType.startsWith(`${classPrefix}_`) === true) {
|
|
681
|
+
return false;
|
|
682
|
+
}
|
|
683
|
+
return true;
|
|
684
|
+
}
|
|
685
|
+
export function isLocallyDefined(typeName, dtsContent) {
|
|
686
|
+
const escapedTypeName = typeName.replace(new RegExp(LIB_REGEX_PATTERN_REGEX_SPECIAL_CHARS.source, 'g'), '\\$&');
|
|
687
|
+
const pattern = new RegExp(`^export type ${escapedTypeName}\\b`, 'm');
|
|
688
|
+
return pattern.test(dtsContent);
|
|
689
|
+
}
|
|
690
|
+
export function isReservedSuffix(typeName) {
|
|
691
|
+
return (typeName.endsWith('_Returns') === true
|
|
692
|
+
|| typeName.endsWith('_TypeGuard') === true
|
|
693
|
+
|| typeName.endsWith('_Return') === true
|
|
694
|
+
|| typeName.endsWith('Returns') === true
|
|
695
|
+
|| typeName.endsWith('TypeGuard') === true
|
|
696
|
+
|| typeName.endsWith('Return') === true);
|
|
697
|
+
}
|
|
698
|
+
export function validateLeaf(varName, typeName, sourceSection, classPrefix) {
|
|
699
|
+
const stripped = stripUnderscorePrefix(varName);
|
|
700
|
+
const titleVar = stripped.charAt(0).toUpperCase() + stripped.slice(1);
|
|
701
|
+
const expectedSection = `${sourceSection}_${titleVar}`;
|
|
702
|
+
const expectedClass = `${classPrefix}_${titleVar}`;
|
|
703
|
+
if (typeName === expectedSection || typeName === expectedClass) {
|
|
704
|
+
return null;
|
|
705
|
+
}
|
|
706
|
+
let actualLeaf = typeName;
|
|
707
|
+
if (typeName.startsWith(`${sourceSection}_`) === true) {
|
|
708
|
+
actualLeaf = typeName.slice(sourceSection.length + 1);
|
|
709
|
+
}
|
|
710
|
+
else if (typeName.startsWith(`${classPrefix}_`) === true) {
|
|
711
|
+
actualLeaf = typeName.slice(classPrefix.length + 1);
|
|
712
|
+
}
|
|
713
|
+
return {
|
|
714
|
+
actualLeaf,
|
|
715
|
+
expectedLeaf: titleVar,
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
export function validateReturnType(returnType, typeGuardFlag) {
|
|
719
|
+
if (returnType.endsWith('_Return') === true || returnType.endsWith('Return') === true) {
|
|
720
|
+
return `Return type '${returnType}' uses banned singular 'Return' suffix; rename to use 'Returns' (plural).`;
|
|
721
|
+
}
|
|
722
|
+
if (typeGuardFlag === true) {
|
|
723
|
+
if (returnType.endsWith('_TypeGuard') === false && returnType.endsWith('TypeGuard') === false) {
|
|
724
|
+
return `Type-guard return type '${returnType}' must end in 'TypeGuard'.`;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
else if (returnType.endsWith('_Returns') === false && returnType.endsWith('Returns') === false) {
|
|
728
|
+
if (returnType.endsWith('_TypeGuard') === true || returnType.endsWith('TypeGuard') === true) {
|
|
729
|
+
return `Return type '${returnType}' ends in 'TypeGuard' but the function does not use 'value is T' form; either rename to '_Returns' or rewrite as a type guard.`;
|
|
730
|
+
}
|
|
731
|
+
return `Function return type '${returnType}' must end in 'Returns' (plural).`;
|
|
732
|
+
}
|
|
733
|
+
return null;
|
|
734
|
+
}
|
|
735
|
+
export function runRulePipeline(filePath, sourceContent, dtsContent, classPrefix) {
|
|
736
|
+
const sectionMap = buildSourceSectionMap(filePath, sourceContent, classPrefix);
|
|
737
|
+
const lines = sourceContent.split('\n');
|
|
738
|
+
const declarations = extractBodyDeclarations(lines);
|
|
739
|
+
const callbacks = detectInlineTypedCallbacks(filePath, sourceContent);
|
|
740
|
+
const fnReturnRecords = extractFunctionReturns(filePath, sourceContent);
|
|
741
|
+
const violations = [];
|
|
742
|
+
for (const callback of callbacks) {
|
|
743
|
+
violations.push(`7.4: ${callback['paramName']}: ${callback['typeName']}`);
|
|
744
|
+
}
|
|
745
|
+
for (const declaration of declarations) {
|
|
746
|
+
const sourceSection = sectionMap.get(declaration['lineNumber']) ?? classPrefix;
|
|
747
|
+
if (isReservedSuffix(declaration['typeName']) === true) {
|
|
748
|
+
violations.push(`7.3: ${declaration['varName']}: ${declaration['typeName']}`);
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
const leafResult = validateLeaf(declaration['varName'], declaration['typeName'], sourceSection, classPrefix);
|
|
752
|
+
if (leafResult !== null) {
|
|
753
|
+
violations.push(`7.1: ${declaration['varName']}: ${declaration['typeName']}`);
|
|
754
|
+
}
|
|
755
|
+
if (isLocallyDefined(declaration['typeName'], dtsContent) === false) {
|
|
756
|
+
violations.push(`7.2-not-defined: ${declaration['typeName']}`);
|
|
757
|
+
}
|
|
758
|
+
else if (isAliasToForeignType(declaration['typeName'], dtsContent, classPrefix) === true) {
|
|
759
|
+
violations.push(`7.2-alias: ${declaration['typeName']}`);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
for (const fnReturn of fnReturnRecords) {
|
|
763
|
+
const reason = validateReturnType(fnReturn['returnType'], fnReturn['isTypeGuard']);
|
|
764
|
+
if (reason !== null) {
|
|
765
|
+
violations.push(`7.5/6/7: ${fnReturn['returnType']}`);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return violations;
|
|
769
|
+
}
|
|
770
|
+
//# sourceMappingURL=type-declaration-engine.js.map
|