All files / gogocode-core/src $.js

100% Statements 44/44
100% Branches 27/27
100% Functions 3/3
100% Lines 44/44

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 8936x 36x 36x 36x 36x   36x 36x 36x   36x             640x 640x 208x   640x 1x 1x   640x     36x 658x           658x 657x 211x     657x 3x     657x 640x 640x 640x                 3x         637x 17x 2x 15x   11x 4x   3x   1x     653x 653x     36x 6x 5x     36x   36x   36x  
const jsCore = require('./js-core/core');
const htmlCore = require('./html-core/core');
const vueCore = require('./vue-core/core');
const NodePath = require('./NodePath');
const AST = require('./Ast');
// const build = require('./build-node');
const loadFile = require('./file-tool/read-file');
const writeFile = require('./file-tool/write-file');
const pkg = require('../package.json');
 
const langCoreMap = {
    'vue': vueCore,
    'html': htmlCore,
    'js': jsCore
}
 
function getCore(parseOptions = {}) {
    let core = jsCore
    if (parseOptions.language && langCoreMap[parseOptions.language]) {
        core = langCoreMap[parseOptions.language]
    }
    if (parseOptions.html) {
        core = htmlCore
        parseOptions.language = 'html'
    }
    return core
}
 
const main = (code, options = {}) => {
    code = code || '';
    let node;
    let nodePath;
    let parseOptions;
    let astFragment;
    let isProgram = 
        options.isProgram === undefined || options.isProgram === true;
    if (typeof options.parseOptions == 'object') {
        parseOptions = options.parseOptions;
    }
 
    if (typeof options.astFragment == 'object') {
        astFragment = options.astFragment;
    }
 
    if (typeof code == 'string') {
        try {
            const core = getCore(parseOptions)
            node = core.buildAstByAstStr(
                code,
                astFragment,                 
                {
                    parseOptions,
                    isProgram
                }
            );
        } catch (e) {
            return { 
                src: code,
                error: `Only correct js / html / vue could be parse successfully, please check the code or parseOptions!`
            }
        }
        nodePath = new NodePath(node);
    } else if (code.nodeType) {
        nodePath = new NodePath(code);
    } else if (code.type) {
        // 传入ast node对象
        nodePath = new NodePath(code);
    } else if (code.node && code.parent) {
        // 传入nodePath对象
        nodePath = code;
    } else {
        throw new Error('$ failed! invalid input! accept code / ast node / nodePath');
    }
 
    let ast = new AST(nodePath, { parseOptions, rootNode: nodePath });
    return ast;
};
 
main.loadFile = (filePath, { parseOptions } = {}) => {
    const code = loadFile(filePath).toString();
    return main(code, { parseOptions })
};
 
main.writeFile = writeFile;
 
main.version = pkg.version;
 
module.exports = main;