@kubb/parser
Advanced tools
Comparing version 1.15.0-canary.20231026T164947 to 1.15.0-canary.20231026T182143
@@ -1,111 +0,3 @@ | ||
import ts from 'typescript'; | ||
import ts, { PrinterOptions } from 'typescript'; | ||
type ArrayTwoOrMore<T> = { | ||
0: T; | ||
1: T; | ||
} & Array<T>; | ||
declare const modifiers: { | ||
readonly async: ts.ModifierToken<ts.SyntaxKind.AsyncKeyword>; | ||
readonly export: ts.ModifierToken<ts.SyntaxKind.ExportKeyword>; | ||
readonly const: ts.ModifierToken<ts.SyntaxKind.ConstKeyword>; | ||
readonly static: ts.ModifierToken<ts.SyntaxKind.StaticKeyword>; | ||
}; | ||
declare function createQuestionToken(token?: boolean | ts.QuestionToken): ts.PunctuationToken<ts.SyntaxKind.QuestionToken> | undefined; | ||
declare function createIntersectionDeclaration({ nodes, withParentheses, }: { | ||
nodes: ArrayTwoOrMore<ts.TypeNode>; | ||
withParentheses?: boolean; | ||
}): ts.TypeNode | null; | ||
/** | ||
* Minimum nodes length of 2 | ||
* @example `string & number` | ||
*/ | ||
declare function createTupleDeclaration({ nodes, withParentheses }: { | ||
nodes: ArrayTwoOrMore<ts.TypeNode>; | ||
withParentheses?: boolean; | ||
}): ts.TypeNode | null; | ||
/** | ||
* Minimum nodes length of 2 | ||
* @example `string | number` | ||
*/ | ||
declare function createUnionDeclaration({ nodes, withParentheses }: { | ||
nodes: ArrayTwoOrMore<ts.TypeNode>; | ||
withParentheses?: boolean; | ||
}): ts.TypeNode | null; | ||
declare function createPropertySignature({ readOnly, modifiers, name, questionToken, type, }: { | ||
readOnly?: boolean; | ||
modifiers?: Array<ts.Modifier>; | ||
name: ts.PropertyName | string; | ||
questionToken?: ts.QuestionToken | boolean; | ||
type?: ts.TypeNode; | ||
}): ts.PropertySignature; | ||
declare function createParameterSignature(name: string | ts.BindingName, { modifiers, dotDotDotToken, questionToken, type, initializer, }: { | ||
decorators?: Array<ts.Decorator>; | ||
modifiers?: Array<ts.Modifier>; | ||
dotDotDotToken?: ts.DotDotDotToken; | ||
questionToken?: ts.QuestionToken | boolean; | ||
type?: ts.TypeNode; | ||
initializer?: ts.Expression; | ||
}): ts.ParameterDeclaration; | ||
declare function createJSDoc({ comments }: { | ||
comments: string[]; | ||
}): ts.JSDoc; | ||
/** | ||
* @link https://github.com/microsoft/TypeScript/issues/44151 | ||
*/ | ||
declare function appendJSDocToNode<TNode extends ts.Node>({ node, comments }: { | ||
node: TNode; | ||
comments: Array<string | undefined>; | ||
}): TNode; | ||
declare function createIndexSignature(type: ts.TypeNode, { modifiers, indexName, indexType, }?: { | ||
indexName?: string; | ||
indexType?: ts.TypeNode; | ||
decorators?: Array<ts.Decorator>; | ||
modifiers?: Array<ts.Modifier>; | ||
}): ts.IndexSignatureDeclaration; | ||
declare function createTypeAliasDeclaration({ modifiers, name, typeParameters, type, }: { | ||
modifiers?: Array<ts.Modifier>; | ||
name: string | ts.Identifier; | ||
typeParameters?: Array<ts.TypeParameterDeclaration>; | ||
type: ts.TypeNode; | ||
}): ts.TypeAliasDeclaration; | ||
/** | ||
* In { propertyName: string; name?: string } is `name` being used to make the type more unique when multiple same names are used. | ||
* @example `import { Pet as Cat } from './Pet'` | ||
*/ | ||
declare function createImportDeclaration({ name, path, isTypeOnly, }: { | ||
name: string | Array<ts.Identifier | string | { | ||
propertyName: string; | ||
name?: string; | ||
}>; | ||
path: string; | ||
isTypeOnly?: boolean; | ||
}): ts.ImportDeclaration; | ||
declare function createExportDeclaration({ path, asAlias, isTypeOnly, name, }: { | ||
path: string; | ||
asAlias?: boolean; | ||
isTypeOnly?: boolean; | ||
name?: string | Array<ts.Identifier | string>; | ||
}): ts.ExportDeclaration; | ||
declare function createEnumDeclaration({ type, name, typeName, enums, }: { | ||
/** | ||
* @default `'enum'` | ||
*/ | ||
type?: 'enum' | 'asConst' | 'asPascalConst'; | ||
/** | ||
* Enum name in camelCase. | ||
*/ | ||
name: string; | ||
/** | ||
* Enum name in PascalCase. | ||
*/ | ||
typeName: string; | ||
enums: [key: string | number, value: string | number | boolean][]; | ||
}): ts.EnumDeclaration[] | (ts.TypeAliasDeclaration | ts.VariableStatement)[]; | ||
declare function createOmitDeclaration({ keys, type, nonNullable }: { | ||
keys: Array<string> | string; | ||
type: ts.TypeNode; | ||
nonNullable?: boolean; | ||
}): ts.TypeReferenceNode; | ||
type ParseResult = { | ||
@@ -117,4 +9,8 @@ ast: ts.Node; | ||
declare function print(elements: ts.Node | Array<ts.Node | undefined> | null, baseName?: string): string; | ||
type Options = { | ||
source?: string; | ||
baseName?: string; | ||
} & PrinterOptions; | ||
declare function print(elements: ts.Node | Array<ts.Node | undefined> | null, { source, baseName, removeComments, noEmitHelpers, newLine }?: Options): string; | ||
export { ArrayTwoOrMore, appendJSDocToNode, createEnumDeclaration, createExportDeclaration, createImportDeclaration, createIndexSignature, createIntersectionDeclaration, createJSDoc, createOmitDeclaration, createParameterSignature, createPropertySignature, createQuestionToken, createTupleDeclaration, createTypeAliasDeclaration, createUnionDeclaration, modifiers, parse, print }; | ||
export { parse, print }; |
@@ -6,287 +6,5 @@ import { createRequire } from 'module'; | ||
var { factory } = ts; | ||
var modifiers = { | ||
async: factory.createModifier(ts.SyntaxKind.AsyncKeyword), | ||
export: factory.createModifier(ts.SyntaxKind.ExportKeyword), | ||
const: factory.createModifier(ts.SyntaxKind.ConstKeyword), | ||
static: factory.createModifier(ts.SyntaxKind.StaticKeyword) | ||
}; | ||
function isValidIdentifier(str) { | ||
if (!str.length || str.trim() !== str) { | ||
return false; | ||
} | ||
const node = ts.parseIsolatedEntityName(str, ts.ScriptTarget.Latest); | ||
return !!node && node.kind === ts.SyntaxKind.Identifier && ts.identifierToKeywordKind(node.kind) === void 0; | ||
} | ||
function propertyName(name) { | ||
if (typeof name === "string") { | ||
return isValidIdentifier(name) ? factory.createIdentifier(name) : factory.createStringLiteral(name); | ||
} | ||
return name; | ||
} | ||
var questionToken = factory.createToken(ts.SyntaxKind.QuestionToken); | ||
function createQuestionToken(token) { | ||
if (!token) { | ||
return void 0; | ||
} | ||
if (token === true) { | ||
return questionToken; | ||
} | ||
return token; | ||
} | ||
function createIntersectionDeclaration({ | ||
nodes, | ||
withParentheses | ||
}) { | ||
if (!nodes.length) { | ||
return null; | ||
} | ||
if (nodes.length === 1) { | ||
return nodes[0]; | ||
} | ||
const node = factory.createIntersectionTypeNode(nodes); | ||
if (withParentheses) { | ||
return factory.createParenthesizedType(node); | ||
} | ||
return node; | ||
} | ||
function createTupleDeclaration({ nodes, withParentheses }) { | ||
if (!nodes.length) { | ||
return null; | ||
} | ||
if (nodes.length === 1) { | ||
return nodes[0]; | ||
} | ||
const node = factory.createTupleTypeNode(nodes); | ||
if (withParentheses) { | ||
return factory.createParenthesizedType(node); | ||
} | ||
return node; | ||
} | ||
function createUnionDeclaration({ nodes, withParentheses }) { | ||
if (!nodes.length) { | ||
return null; | ||
} | ||
if (nodes.length === 1) { | ||
return nodes[0]; | ||
} | ||
const node = factory.createUnionTypeNode(nodes); | ||
if (withParentheses) { | ||
return factory.createParenthesizedType(node); | ||
} | ||
return node; | ||
} | ||
function createPropertySignature({ | ||
readOnly, | ||
modifiers: modifiers2 = [], | ||
name, | ||
questionToken: questionToken2, | ||
type | ||
}) { | ||
return factory.createPropertySignature( | ||
[...modifiers2, readOnly ? factory.createToken(ts.SyntaxKind.ReadonlyKeyword) : void 0].filter(Boolean), | ||
propertyName(name), | ||
createQuestionToken(questionToken2), | ||
type | ||
); | ||
} | ||
function createParameterSignature(name, { | ||
modifiers: modifiers2, | ||
dotDotDotToken, | ||
questionToken: questionToken2, | ||
type, | ||
initializer | ||
}) { | ||
return factory.createParameterDeclaration(modifiers2, dotDotDotToken, name, createQuestionToken(questionToken2), type, initializer); | ||
} | ||
function createJSDoc({ comments }) { | ||
return factory.createJSDocComment( | ||
factory.createNodeArray( | ||
comments?.map((comment, i) => { | ||
if (i === comments.length - 1) { | ||
return factory.createJSDocText(comment); | ||
} | ||
return factory.createJSDocText(`${comment} | ||
`); | ||
}) | ||
) | ||
); | ||
} | ||
function appendJSDocToNode({ node, comments }) { | ||
const filteredComments = comments.filter(Boolean); | ||
if (!filteredComments.length) { | ||
return node; | ||
} | ||
const text = filteredComments.reduce((acc = "", comment = "") => { | ||
return `${acc} | ||
* ${comment}`; | ||
}, "*"); | ||
return ts.addSyntheticLeadingComment({ ...node }, ts.SyntaxKind.MultiLineCommentTrivia, `${text || "*"} | ||
`, true); | ||
} | ||
function createIndexSignature(type, { | ||
modifiers: modifiers2, | ||
indexName = "key", | ||
indexType = factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword) | ||
} = {}) { | ||
return factory.createIndexSignature(modifiers2, [createParameterSignature(indexName, { type: indexType })], type); | ||
} | ||
function createTypeAliasDeclaration({ | ||
modifiers: modifiers2, | ||
name, | ||
typeParameters, | ||
type | ||
}) { | ||
return factory.createTypeAliasDeclaration(modifiers2, name, typeParameters, type); | ||
} | ||
function createImportDeclaration({ | ||
name, | ||
path, | ||
isTypeOnly = false | ||
}) { | ||
if (!Array.isArray(name)) { | ||
return factory.createImportDeclaration( | ||
void 0, | ||
factory.createImportClause(isTypeOnly, factory.createIdentifier(name), void 0), | ||
factory.createStringLiteral(path), | ||
void 0 | ||
); | ||
} | ||
return factory.createImportDeclaration( | ||
void 0, | ||
factory.createImportClause( | ||
isTypeOnly, | ||
void 0, | ||
factory.createNamedImports( | ||
name.map((propertyName2) => { | ||
if (typeof propertyName2 === "object") { | ||
const obj = propertyName2; | ||
if (obj.name) { | ||
return factory.createImportSpecifier(false, factory.createIdentifier(obj.propertyName), factory.createIdentifier(obj.name)); | ||
} | ||
return factory.createImportSpecifier(false, void 0, factory.createIdentifier(obj.propertyName)); | ||
} | ||
return factory.createImportSpecifier(false, void 0, typeof propertyName2 === "string" ? factory.createIdentifier(propertyName2) : propertyName2); | ||
}) | ||
) | ||
), | ||
factory.createStringLiteral(path), | ||
void 0 | ||
); | ||
} | ||
function createExportDeclaration({ | ||
path, | ||
asAlias, | ||
isTypeOnly = false, | ||
name | ||
}) { | ||
if (name && !Array.isArray(name) && !asAlias) { | ||
throw new Error("When using `name` as string, `asAlias` should be true"); | ||
} | ||
if (!Array.isArray(name)) { | ||
const parsedName = name?.match(/^\d/) ? `_${name?.slice(1)}` : name; | ||
return factory.createExportDeclaration( | ||
void 0, | ||
isTypeOnly, | ||
asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : void 0, | ||
factory.createStringLiteral(path), | ||
void 0 | ||
); | ||
} | ||
return factory.createExportDeclaration( | ||
void 0, | ||
isTypeOnly, | ||
factory.createNamedExports( | ||
name.map((propertyName2) => { | ||
return factory.createExportSpecifier(false, void 0, typeof propertyName2 === "string" ? factory.createIdentifier(propertyName2) : propertyName2); | ||
}) | ||
), | ||
factory.createStringLiteral(path), | ||
void 0 | ||
); | ||
} | ||
function createEnumDeclaration({ | ||
type = "enum", | ||
name, | ||
typeName, | ||
enums | ||
}) { | ||
if (type === "enum") { | ||
return [ | ||
factory.createEnumDeclaration( | ||
[factory.createToken(ts.SyntaxKind.ExportKeyword)], | ||
factory.createIdentifier(typeName), | ||
enums.map(([key, value]) => { | ||
let initializer = factory.createStringLiteral(`${value?.toString()}`); | ||
if (typeof value === "number") { | ||
initializer = factory.createNumericLiteral(value); | ||
} | ||
if (typeof value === "boolean") { | ||
initializer = value ? factory.createTrue() : factory.createFalse(); | ||
} | ||
if (typeof key === "number") { | ||
return factory.createEnumMember(factory.createStringLiteral(`${typeName}_${key}`), initializer); | ||
} | ||
return factory.createEnumMember(factory.createStringLiteral(`${key}`), initializer); | ||
}) | ||
) | ||
]; | ||
} | ||
const identifierName = type === "asPascalConst" ? typeName : name; | ||
return [ | ||
factory.createVariableStatement( | ||
[factory.createToken(ts.SyntaxKind.ExportKeyword)], | ||
factory.createVariableDeclarationList( | ||
[ | ||
factory.createVariableDeclaration( | ||
factory.createIdentifier(identifierName), | ||
void 0, | ||
void 0, | ||
factory.createAsExpression( | ||
factory.createObjectLiteralExpression( | ||
enums.map(([key, value]) => { | ||
let initializer = factory.createStringLiteral(`${value?.toString()}`); | ||
if (typeof value === "number") { | ||
initializer = factory.createNumericLiteral(value); | ||
} | ||
if (typeof value === "boolean") { | ||
initializer = value ? factory.createTrue() : factory.createFalse(); | ||
} | ||
return factory.createPropertyAssignment(factory.createStringLiteral(`${key}`), initializer); | ||
}), | ||
true | ||
), | ||
factory.createTypeReferenceNode(factory.createIdentifier("const"), void 0) | ||
) | ||
) | ||
], | ||
ts.NodeFlags.Const | ||
) | ||
), | ||
factory.createTypeAliasDeclaration( | ||
[factory.createToken(ts.SyntaxKind.ExportKeyword)], | ||
factory.createIdentifier(typeName), | ||
void 0, | ||
factory.createIndexedAccessTypeNode( | ||
factory.createParenthesizedType(factory.createTypeQueryNode(factory.createIdentifier(identifierName), void 0)), | ||
factory.createTypeOperatorNode(ts.SyntaxKind.KeyOfKeyword, factory.createTypeQueryNode(factory.createIdentifier(identifierName), void 0)) | ||
) | ||
) | ||
]; | ||
} | ||
function createOmitDeclaration({ keys, type, nonNullable }) { | ||
const node = nonNullable ? factory.createTypeReferenceNode(factory.createIdentifier("NonNullable"), [type]) : type; | ||
if (Array.isArray(keys)) { | ||
return factory.createTypeReferenceNode(factory.createIdentifier("Omit"), [ | ||
node, | ||
factory.createUnionTypeNode( | ||
keys.map((key) => { | ||
return factory.createLiteralTypeNode(factory.createStringLiteral(key)); | ||
}) | ||
) | ||
]); | ||
} | ||
return factory.createTypeReferenceNode(factory.createIdentifier("Omit"), [node, factory.createLiteralTypeNode(factory.createStringLiteral(keys))]); | ||
} | ||
var { factory: factory2 } = ts; | ||
function print(elements, baseName = "print.ts") { | ||
function print(elements, { source = "", baseName = "print.ts", removeComments, noEmitHelpers, newLine } = {}) { | ||
const printer = ts.createPrinter({ omitTrailingSemicolon: false, newLine: newLine || ts.NewLineKind.LineFeed, removeComments, noEmitHelpers }); | ||
const sourceFile = ts.createSourceFile(baseName, source, ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS); | ||
let nodes = []; | ||
@@ -297,11 +15,9 @@ if (!elements) { | ||
if (Array.isArray(elements)) { | ||
nodes = elements; | ||
nodes = elements.filter(Boolean); | ||
} else { | ||
nodes = [elements]; | ||
nodes = [elements].filter(Boolean); | ||
} | ||
const nodesArray = factory2.createNodeArray(nodes.filter(Boolean)); | ||
const sourceFile = ts.createSourceFile(baseName, "", ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS); | ||
const printer = ts.createPrinter({ omitTrailingSemicolon: false, newLine: ts.NewLineKind.LineFeed }); | ||
const outputFile = printer.printList(ts.ListFormat.MultiLine, nodesArray, sourceFile); | ||
return outputFile; | ||
const outputFile = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile); | ||
const outputSource = printer.printFile(sourceFile); | ||
return [outputFile, outputSource].filter(Boolean).join("\n"); | ||
} | ||
@@ -317,4 +33,4 @@ | ||
export { appendJSDocToNode, createEnumDeclaration, createExportDeclaration, createImportDeclaration, createIndexSignature, createIntersectionDeclaration, createJSDoc, createOmitDeclaration, createParameterSignature, createPropertySignature, createQuestionToken, createTupleDeclaration, createTypeAliasDeclaration, createUnionDeclaration, modifiers, parse, print }; | ||
export { parse, print }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@kubb/parser", | ||
"version": "1.15.0-canary.20231026T164947", | ||
"version": "1.15.0-canary.20231026T182143", | ||
"description": "Generator parser", | ||
@@ -26,2 +26,7 @@ "keywords": [ | ||
}, | ||
"./factory": { | ||
"import": "./dist/factory.js", | ||
"require": "./dist/factory.cjs", | ||
"default": "./dist/factory.cjs" | ||
}, | ||
"./package.json": "./package.json", | ||
@@ -35,3 +40,2 @@ "./*": "./*" | ||
"dist", | ||
"schemas", | ||
"!/**/**.test.**", | ||
@@ -38,0 +42,0 @@ "!/**/__tests__/**" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
88145
15
836
2