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

94.11% Statements 16/17
83.33% Branches 10/12
100% Functions 2/2
94.11% Lines 16/17

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 7237x 37x 37x 1125x 1125x                                                         1125x                           1125x     1125x 1125x 1125x     101x 101x 101x 101x         101x     47x          
const recast = require('recast-yx');
const babelParse = require('@babel/parser');
module.exports = function(code, options) {
    options = options || global.parseOptions;
    let plugins = ((options && options.plugins) ? options.plugins : [])
        .concat([
            'plugin-syntax-typescript',
            'typescript',
            'asyncGenerators',
            'bigInt',
            'classProperties',
            'classPrivateProperties',
            'classPrivateMethods',
            'doExpressions',
            'dynamicImport',
            'exportDefaultFrom',
            'exportNamespaceFrom',
            'functionBind',
            'functionSent',
            'importMeta',
            'logicalAssignment',
            'nullishCoalescingOperator',
            'numericSeparator',
            'objectRestSpread',
            'optionalCatchBinding',
            'optionalChaining',
            'partialApplication',
            ['pipelineOperator', {'proposal': "smart"}],
            'throwExpressions',
            'topLevelAwait',
            'decorators-legacy', 
            ['@babel/plugin-syntax-decorators','decorators', { decoratorsBeforeExport: true }]
        ]);
    const parseOptions = {
        // sourceType: 'module',
        allowHashBang: true,
        ecmaVersion: Infinity,
        allowImportExportEverywhere: true,
        allowReturnOutsideFunction: true,
        allowAwaitOutsideFunction: true,
        allowUndeclaredExports: true,
        allowSuperOutsideMethod: true,
        startLine: 1,
        tokens: true,
        ...(options || {}),
        plugins,
    };
    return recast.parse(code, {
        parser: {
            parse(code) {
                try {
                    try {
                        return babelParse.parse(code, parseOptions);   
                    } catch(e) {
                        // 是否存在jsx可能导致parse报错,所以在此兼容
                        Eif (parseOptions.plugins) {
                            const jsxIndex = parseOptions.plugins.indexOf('jsx');
                            if (jsxIndex == -1) {
                                parseOptions.plugins.push('jsx');
                            } else E{
                                parseOptions.plugins.splice(jsxIndex, 1)
                            }
                        }
                        return babelParse.parse(code, parseOptions);
                    }
                } catch(e) {
                    throw Error(e.message)
                }
            }
        }
    });
}