# All files / gogocode-core/src/vue-core core.js

84.62% Statements 44/52
67.5% Branches 27/40
100% Functions 4/4
84.62% Lines 44/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    9436x
    36x
    36x
    36x
    36x

    25x
    25x
    25x
    15x
    15x
    15x

    15x
    15x

    10x
    8x
    8x
    8x

    8x
    8x

    2x
    2x
    2x
    2x

    2x
    2x

    25x

    15x
    14x

    14x

    1x

    10x
    2x
    8x
    6x

    10x
    8x

    8x

    2x

    22x
    22x
    22x
    22x
    22x

    36xconst parse = require('./parse');
    const jsCore = require('../js-core/core');
    const htmlCore = require('../html-core/core')
    const NodePath = require('../NodePath');
    const core = {
        getAstsBySelector(ast, selector, { parseOptions } = {}) {
            parseOptions = Object.assign({}, parseOptions);
            let newAst = ast;
            if (selector == '<template></template>') {
                parseOptions.language = 'html';
                parseOptions.rootLanguage = 'vue';
                Iif (ast.templateAst) {
                    newAst = ast.templateAst;
                } else {
                    ast.templateAst = core.getTemplate(ast);
                    newAst = ast.templateAst;
                }
            } else if (selector == '<script></script>') {
                parseOptions.language = 'js'
                parseOptions.rootLanguage = 'vue';
                Iif (ast.scriptAst) {
                    newAst = ast.scriptAst;
                } else {
                    ast.scriptAst = core.getScript(ast);
                    newAst = ast.scriptAst;
                }
            } else Eif (selector == '<script setup></script>') {
                parseOptions.language = 'js'
                parseOptions.rootLanguage = 'vue';
                Iif (ast.scriptSetupAst) {
                    newAst = ast.scriptSetupAst;
                } else {
                    ast.scriptSetupAst = core.getScript(ast, { isSetup: true });
                    newAst = ast.scriptSetupAst;
                }
            }
            return { nodePathList: newAst ? [newAst] : [], matchWildCardList: [], extra: { parseOptions } }
        },
        getTemplate(ast) {
            // 仅针对vue，取template，后续通过htmlcore处理
            if (ast.template) {
                const template = htmlCore.buildAstByAstStr(
                    ast.template.content,
                    {},
                    {
                        isProgram: true,
                        parseOptions: { language: 'html' }
                    }
                );
                return new NodePath(template);
            } else {
                return undefined;
            }
        },
        getScript(ast, { isSetup = false } = {} ) {
            // 仅针对vue，取script，后续通过jscore处理
            let content;
            if (isSetup && ast.scriptSetup) {
                content = ast.scriptSetup.content;
            } else if (!isSetup && ast.script) {
                content = ast.script.content
                // const content = ast.script.content.replace(/\n/g, '')
            }
            if (content) {
                const script = jsCore.buildAstByAstStr(
                    content, {},
                    { isProgram: true }
                );
                return new NodePath(script);
            } else {
                return undefined;
            }
        },
        buildAstByAstStr(str, astPatialMap = {}, { isProgram = false, parseOptions } = {}) {
            try {
                const program = parse(str, parseOptions);
                Eif (program) {
                    Eif (isProgram) {
                        return program;
                    } else {
                        if (program.template && program.template.ast) {
                            return program.template
                        } else return null
                    }
                } else {
                    return null;
                }
            } catch(e) {
                console.log('buildAstByAstStr failed:' + e)
            }
        }
    }

    module.exports = core;