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

84.9% Statements 45/53
67.5% Branches 27/40
100% Functions 4/4
84.9% Lines 45/53

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 94 95 96 97 9836x 36x 36x 36x 36x   29x 29x 29x 16x 16x 16x     16x 16x   13x 10x 10x 10x     10x 10x   3x 2x 2x 2x     2x 2x     29x       16x 15x               15x   1x           12x 2x 10x 8x     12x 10x             10x   2x       26x 26x 26x 26x 26x 26x                             36x
const 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 = null;
        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, { parseOptions });
                newAst = ast.scriptAst;
            }
        } else if (selector == '<script setup></script>') {
            parseOptions.language = 'js'
            parseOptions.rootLanguage = 'vue';
            Iif (ast.scriptSetupAst) {
                newAst = ast.scriptSetupAst;
            } else {
                ast.scriptSetupAst = core.getScript(ast, { isSetup: true, parseOptions });
                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, parseOptions } = {} ) {
        // 仅针对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,
                    parseOptions
                }
            );
            return new NodePath(script);
        } else {
            return undefined;
        }
    },
    buildAstByAstStr(str, astPatialMap = {}, { isProgram = false, parseOptions } = {}) {
        try {
            const program = parse(str, parseOptions);
            core.parseOptions = parseOptions;
            if (program) {
                if (isProgram) {
                    return program;
                } else E{
                    if (program.template && program.template.ast) {
                        return program.template
                    } else return null
                }
            } else E{
                return null;
            }
        } catch(e) {
            console.log('buildAstByAstStr failed:' + e)
        }
    }
}
 
module.exports = core;