本文整理汇总了TypeScript中dts-element.parse函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parse函数的具体用法?TypeScript parse怎么用?TypeScript parse使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: generate_function
export const create_n_ary_declarations = (
min_curry_level: number,
max_curry_level: number,
generate_function: (args: Args) => string,
generate_additional?: (args: Args) => string,
) => {
const return_type = 'R';
const types = R.repeat(0, max_curry_level).map((_, index) => `T${index + 1}`);
const variables = R.repeat(0, max_curry_level).map(
(_, index) => `v${index + 1}`,
);
const parameters = variables.map(
(variable, index) => `${variable}: ${types[index]}`,
);
const curry_levels = [];
const declarations = [];
for (
let curry_level = min_curry_level;
curry_level <= max_curry_level;
curry_level++
) {
curry_levels.push(curry_level);
declarations.push(
generate_function({
curry_level,
curry_levels,
return_type,
types: types.slice(0, curry_level),
variables: variables.slice(0, curry_level),
parameters: parameters.slice(0, curry_level),
generics: [...types.slice(0, curry_level), return_type],
}),
);
}
if (generate_additional !== undefined) {
declarations.push(
generate_additional({
curry_level: -1,
curry_levels,
return_type,
types,
variables,
parameters,
generics: [...types, return_type],
}),
);
}
return dts.parse(declarations.join('\n')).members;
};
示例2: default
export default (selectable: boolean, placeholder: boolean) => [
...dts.parse(`
${
placeholder
? `import {${placeholder_name} as ${placeholder_name_abbr}} from './$placeholder';`
: ''
}
export type CurriedFunction0<R> = () => R;
`).members,
...create_curried_interfaces(max_curry_level, {
selectable,
placeholder,
}).map(the_interface => ({
...the_interface,
export: true,
})),
];
示例3: get_top_level_members
function get_top_level_members(filename: string): dts.ITopLevelMember[] {
const members: dts.ITopLevelMember[] = [];
const basename = path.basename(filename);
const function_name = basename.match(/^\$?(.+?)\./)![1];
if (basename === '$curriedFunctions.ts') {
push_curried_functions_members();
} else if (basename.endsWith('.d.ts')) {
if (basename.startsWith('$')) {
push_r_ts_members();
} else {
push_d_ts_members();
}
} else if (basename.endsWith('.c.ts')) {
push_c_ts_members();
} else if (basename.endsWith('.r.ts')) {
push_r_ts_members();
} else if (basename.endsWith('.ts')) {
push_ts_members();
} else {
throw new Error(`Unexpected filename ${filename}`);
}
if (!basename.startsWith('$')) {
bind_member_jsdoc();
add_export_equal();
}
if (function_name === 'path') {
bind_mixin(
members,
dts
.parse(
fs.readFileSync(
path.resolve(__dirname, '../mixins/path.d.ts'),
'utf8',
),
)
.members.filter(dts.is_function_declaration)
.map(function_declaration => function_declaration.type!),
);
}
return members;
function add_export_equal() {
const target_member = members.find(member => {
switch (member.kind) {
case dts.ElementKind.VariableDeclaration:
case dts.ElementKind.FunctionDeclaration:
return true;
default:
return false;
}
}) as undefined | dts.IVariableDeclaration | dts.IFunctionDeclaration;
if (target_member === undefined) {
throw new Error(`Cannot find element to set export equal in ${filename}`);
}
members.push(dts.create_export_equal({ value: target_member.name! }));
}
function bind_member_jsdoc() {
const target_member_index = members.findIndex(member => {
switch (member.kind) {
case dts.ElementKind.VariableDeclaration:
return function_name === '__';
case dts.ElementKind.TypeDeclaration:
case dts.ElementKind.FunctionDeclaration:
return true;
default:
return false;
}
});
if (target_member_index === -1) {
throw new Error(`Cannot find element to bind jsdoc in ${filename}`);
}
const target_member = members[target_member_index] as
| dts.IVariableDeclaration
| dts.IFunctionDeclaration
| dts.ITypeDeclaration;
if (target_member.kind === dts.ElementKind.TypeDeclaration) {
const target_overloads = (target_member.type as dts.IObjectType).members!;
bind_jsdoc(filename, target_overloads[0]);
} else {
bind_jsdoc(filename, members[target_member_index]);
}
}
function push_r_ts_members() {
const top_level_element = dts.parse(fs.readFileSync(filename, 'utf8'));
members.push(...top_level_element.members);
}
function push_d_ts_members() {
const top_level_element = dts.parse(fs.readFileSync(filename, 'utf8'));
push_curry_members(top_level_element.members);
}
function push_ts_members() {
const declarations = get_ts_default();
//.........这里部分代码省略.........
示例4: push_curry_members
function push_curry_members(the_members: dts.ITopLevelMember[]) {
const imports = the_members.filter(
(member): member is dts.IImportNamed =>
member.kind === dts.ElementKind.ImportNamed,
);
const functions = the_members.filter<dts.IFunctionDeclaration>(
(member): member is dts.IFunctionDeclaration =>
member.kind === dts.ElementKind.FunctionDeclaration,
);
if (!functions.every(fn => fn.name!.startsWith('$'))) {
throw new Error(
`Exported functions in ${filename} should be prefixed with $`,
);
}
const function_names_no_need_to_check_last_overload_name = ['cond'];
if (
function_names_no_need_to_check_last_overload_name.indexOf(
function_name,
) === -1
) {
const valid_last_overload_names = ['$mixed', '$general', '$variadic'];
if (
functions.length > 1 &&
valid_last_overload_names.indexOf(
functions[functions.length - 1].name!,
) === -1
) {
throw new Error(
`Exported multi-overload functions in ${filename} should end with ${valid_last_overload_names.join(
' / ',
)}}`,
);
}
}
const overload_names_should_exist_simultaneously = ['$record', '$keyof'];
if (
functions.some(
func =>
overload_names_should_exist_simultaneously.indexOf(func.name!) !== -1,
)
) {
const is_exist_simultaneously = overload_names_should_exist_simultaneously.every(
overload_name => functions.some(func => func.name === overload_name),
);
if (!is_exist_simultaneously) {
throw new Error(
`Exported multi-overload functions in ${filename} should have ${overload_names_should_exist_simultaneously.join(
' / ',
)} simultaneously`,
);
}
}
const placeholder_imports =
!placeholder || functions[0].type!.parameters!.length <= 1
? []
: dts.parse(`
import {${placeholder_name} as ${placeholder_name_abbr}} from './$placeholder';
`).members;
const curried_declarations = dts_fp.create_curried_declarations(
path.basename(filename).replace(/(\.[a-z])?\.ts$/, ''),
functions.reduce<Record<string, dts.IFunctionType>>(
(current_functions, fn) => ({
...current_functions,
[fn.name!.slice(1)]: fn.type!,
}),
{},
),
{
selectable,
placeholder,
},
);
members.push(...imports, ...placeholder_imports, ...curried_declarations);
}
示例5: push_d_ts_members
function push_d_ts_members() {
const top_level_element = dts.parse(fs.readFileSync(filename, 'utf8'));
push_curry_members(top_level_element.members);
}
示例6: push_r_ts_members
function push_r_ts_members() {
const top_level_element = dts.parse(fs.readFileSync(filename, 'utf8'));
members.push(...top_level_element.members);
}
示例7:
.map(
after_generic => `
(${current_input_generics
.map(
(input_generic, index) => `
${parameters[index]}: ${input_generic}
`,
)
.join(',')}) => ${after_generic}
`,
)
.join(',')}]
): ${curried_function_name}<${[
...current_input_generics,
generic_return,
].join(',')}>;
`);
}
import_curried_functions.push(curried_function_name);
}
export default dts.parse(`
import {${import_curried_functions.join(',')}} from './$curriedFunctions';
import {List, Variadic} from './$types';
${declarations.join('\n')}
function $variadic<${generic_return}>(
after: Variadic<${generic_return}>,
fns: List<Variadic<any>>
): Variadic<${generic_return}>;
`).members;
示例8:
import * as dts from 'dts-element';
import { max_curry_level } from './$curriedFunctions';
import { create_composition_declarations } from './utils/create-composition-declarations';
export default [
...dts.parse(`
import {Chain} from './$types';
`).members,
...create_composition_declarations(
'pipeK',
'pipe',
max_curry_level,
1,
x => `Chain<${x}>`,
x => `Chain<${x}>`,
),
];
示例9: as
//.........这里部分代码省略.........
max_parameter_count: number,
// istanbul ignore next
generate_function_parameter_type: (generic: string) => string = x => x,
// istanbul ignore next
generate_function_return_type: (
generic: string,
index: number,
) => string = x => x,
// istanbul ignore next
generate_composed_return_type = generate_function_return_type as (
generic: string,
) => string,
) => {
const function_names = R.repeat(0, max_function_count).map(
(_, index) => `fn${index + 1}`,
);
const return_generics = R.repeat(0, max_function_count).map(
(_, index) => `R${index + 1}`,
);
const parameter_names = R.repeat(0, max_parameter_count).map(
(_, index) => `v${index + 1}`,
);
const parameter_generics = R.repeat(0, max_parameter_count).map(
(_, index) => `V${index + 1}`,
);
const declarations = [`declare const ${name}: typeof $;`];
for (
let function_count = 1;
function_count <= max_function_count;
function_count++
) {
const entry_index = kind === 'pipe' ? 0 : function_count - 1;
const current_return_generics = return_generics.slice(0, function_count);
const current_sorted_return_generics = current_return_generics.slice();
const current_sorted_function_names = function_names.slice(
0,
function_count,
);
if (kind === 'compose') {
current_sorted_return_generics.reverse();
current_sorted_function_names.reverse();
}
for (
let parameter_count = 0;
parameter_count <= max_parameter_count;
parameter_count++
) {
const current_parameter_names = parameter_names.slice(0, parameter_count);
const current_parameter_generics = parameter_generics.slice(
0,
parameter_count,
);
const entry_parameters = current_parameter_names
.map(
(parameter_name, param_index) => `
${parameter_name}: ${generate_function_parameter_type(
current_parameter_generics[param_index],
)}
`,
)
.join(',');
declarations.push(`
declare function $<${[
...current_parameter_generics,
...current_return_generics,
].join(',')}>(
${current_return_generics
.map((_, index) => {
const function_name = current_sorted_function_names[index];
const return_generic = current_sorted_return_generics[index];
return index === entry_index
? `${function_name}: (${entry_parameters}) => ${generate_function_return_type(
return_generic,
index,
)}`
: `${function_name}: (v: ${generate_function_parameter_type(
current_sorted_return_generics[
index + (kind === 'compose' ? 1 : -1)
],
)}) => ${generate_function_return_type(
return_generic,
index,
)}`;
})
.join(',')}
): (${entry_parameters}) => ${generate_composed_return_type(
current_return_generics[current_return_generics.length - 1],
)};
`);
}
}
return dts.parse(declarations.join('\n')).members;
};
示例10:
(variable_name, index) =>
`${variable_name}: ${current_middle_generics[index]}`,
)
.join(',')}) => ${return_generic},
fns: ${
current_input_generics.length === 0
? `never[]`
: `[${current_input_generics
.map(
(input_generic, index) =>
`Morphism<${input_generic}, ${middle_generics[index]}>`,
)
.join(',')}]`
}
): ${curried_function_name}<${[
...current_input_generics,
return_generic,
].join(',')}>;
`);
}
export default dts.parse(`
import {${curried_function_names.join(',')}} from './$curriedFunctions';
import {Morphism, Variadic} from './$types';
${declarations.join('\n')}
function $variadic<${return_generic}>(
after: Variadic<${return_generic}>,
fns: Function[]
): Variadic<${return_generic}>;
`).members;