@nestia/sdk
Version:
Nestia SDK and Swagger generator
50 lines • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdentifierFactory = void 0;
const factory_1 = require("@ttsc/factory");
const TypeFactory_1 = require("./TypeFactory");
// Conservative "is this string a syntactically valid JavaScript variable
// name?" test, inlined to avoid pulling in @typia/utils' full namespace.
const VARIABLE_REGEX = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
const isVariableName = (str) => VARIABLE_REGEX.test(str);
/**
* Identifier and member-access helpers. The surface kept here is the subset
* nestia generators actually call (`identifier`, `access`, `parameter`).
*/
var IdentifierFactory;
(function (IdentifierFactory) {
/**
* Build an identifier or string literal depending on whether `name` is a
* valid JavaScript identifier.
*/
IdentifierFactory.identifier = (name) => isVariableName(name)
? factory_1.factory.createIdentifier(name)
: factory_1.factory.createStringLiteral(name);
/**
* Member access on `input` by `key`. Falls back to element access when the
* key is not a valid identifier.
*/
IdentifierFactory.access = (input, key, chain) => {
const postfix = IdentifierFactory.identifier(key);
if (postfix.kind === "StringLiteral")
return chain === true
? factory_1.factory.createElementAccessChain(input, factory_1.factory.createToken(factory_1.SyntaxKind.QuestionDotToken), postfix)
: factory_1.factory.createElementAccessExpression(input, postfix);
return chain === true
? factory_1.factory.createPropertyAccessChain(input, factory_1.factory.createToken(factory_1.SyntaxKind.QuestionDotToken), postfix)
: factory_1.factory.createPropertyAccessExpression(input, postfix);
};
/**
* Parameter declaration with default `any` type when the caller omits one.
* Passing a `QuestionToken` as `init` marks the parameter optional.
*/
IdentifierFactory.parameter = (name, type, init) => {
const isQuestionToken = !!init &&
init.kind === "Token" &&
init.token === factory_1.SyntaxKind.QuestionToken;
return factory_1.factory.createParameterDeclaration(undefined, undefined, name, isQuestionToken
? factory_1.factory.createToken(factory_1.SyntaxKind.QuestionToken)
: undefined, type !== null && type !== void 0 ? type : TypeFactory_1.TypeFactory.keyword("any"), isQuestionToken ? undefined : init);
};
})(IdentifierFactory || (exports.IdentifierFactory = IdentifierFactory = {}));
//# sourceMappingURL=IdentifierFactory.js.map