當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript prettier.resolveConfig函數代碼示例

本文整理匯總了TypeScript中prettier.resolveConfig函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript resolveConfig函數的具體用法?TypeScript resolveConfig怎麽用?TypeScript resolveConfig使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了resolveConfig函數的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: graphQLTypes

export async function graphQLTypes(): Promise<void> {
    const schemaStr = await readFile(GRAPHQL_SCHEMA_PATH, 'utf8')
    const schema = buildSchema(schemaStr)
    const result = (await graphql(schema, introspectionQuery)) as { data: IntrospectionQuery }

    const formatOptions = (await resolveConfig(__dirname, { config: __dirname + '/../prettier.config.js' }))!
    const typings =
        'export type ID = string\n\n' +
        generateNamespace(
            '',
            result,
            {
                typeMap: {
                    ...DEFAULT_TYPE_MAP,
                    ID: 'ID',
                },
            },
            {
                generateNamespace: (name: string, interfaces: string) => interfaces,
                interfaceBuilder: (name: string, body: string) =>
                    'export ' + DEFAULT_OPTIONS.interfaceBuilder(name, body),
                enumTypeBuilder: (name: string, values: string) =>
                    'export ' + DEFAULT_OPTIONS.enumTypeBuilder(name, values),
                typeBuilder: (name: string, body: string) => 'export ' + DEFAULT_OPTIONS.typeBuilder(name, body),
                wrapList: (type: string) => `${type}[]`,
                postProcessor: (code: string) => format(code, { ...formatOptions, parser: 'typescript' }),
            }
        )
    await writeFile(__dirname + '/src/graphql/schema.ts', typings)
}
開發者ID:JoYiRis,項目名稱:sourcegraph,代碼行數:30,代碼來源:gulpfile.ts

示例2: parser

const isFormatted = prettier.check('foo ( );', { semi: false });

const result = prettier.formatWithCursor(' 1', { cursorOffset: 2 });

const customFormatted = prettier.format('lodash ( )', {
    parser(text, { babylon }) {
        const ast = babylon(text);
        const statement = ast.program.body[0] as ExpressionStatement;
        const expression = statement.expression as CallExpression;
        const identifier = expression.callee as Identifier;
        identifier.name = '_';
        return ast;
    },
});

prettier.resolveConfig('path/to/somewhere').then(options => {
    if (options !== null) {
        const formatted = prettier.format('hello world', options);
    }
});

if (prettier.getFileInfo.sync('./tsconfig.json').inferredParser !== 'json') {
    throw new Error('Bad parser');
}

prettier.getFileInfo('./tsconfig.json').then(result => {
    if (result.inferredParser !== 'json') {
        throw new Error('Bad parser');
    }
});
開發者ID:DanielRosenwasser,項目名稱:DefinitelyTyped,代碼行數:30,代碼來源:prettier-tests.ts


注:本文中的prettier.resolveConfig函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。