How to use the parse function from @babel/parser
Find comprehensive JavaScript @babel/parser.parse code examples handpicked from public code repositorys.
86 87 88 89 90 91 92 93 94
const fileCasePath = this.resourcePath .replace(/([\\/])(src)([\\/])/, (...args) => args[1] + 'test' + args[3]) .replace(/\.\w{2,5}$/, '.js') const astBase = parser.parse(script.content, { sourceType: 'module', plugins: ['jsx', 'dynamicImport'] // xvm中可以编译jsx语法;增加动态引入js的语法。这里注意。 })
13
28
0
GitHub: facebook/metro
247 248 249 250 251 252 253 254 255 256 257 258
file: JSFile, {config, options, projectRoot}: TransformationContext, ): Promise<TransformResponse> { // Transformers can output null ASTs (if they ignore the file). In that case // we need to parse the module source code to get their AST. let ast = file.ast ?? babylon.parse(file.code, {sourceType: 'unambiguous'}); const {importDefault, importAll} = generateImportNames(ast); // Add "use strict" if the file was parsed as a module, and the directive did
615
0
99
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
linesBelow: 0, }); } function astFromCode(code: string): BabelNodeFile { return babylon.parse(code, { plugins: ['dynamicImport', 'flow'], sourceType: 'module', }); }
615
0
99
GitHub: qooxdoo/qooxdoo
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
) ]); let inject = [tmp]; if (hasLoadDeps) { tmp = babylon.parse( "qx.Bootstrap.executePendingDefers($$dbClassInfo);" ).program.body; inject.push(tmp[0]); }
257
757
77
GitHub: gluestack/dank-style
136 137 138 139 140 141 142 143 144 145
if (!fileData) { return {}; } fileData = fileData?.replace(/as const/g, ''); const ast = babel.parse(fileData, { presets: [babelPresetTypeScript], plugins: ['typescript'], sourceType: 'module', comments: false,
3
52
5
GitHub: lb0905/webpack
64 65 66 67 68 69 70 71 72 73
} for (let i = loaders.length - 1; i >= 0; i--) { const loader = loaders[i]; originSourceCode = require(loader)(originSourceCode); } const astTree = parse(originSourceCode); traverse(astTree, { CallExpression({ node }) { if (node.callee.name === 'require') { const moduleName = node.arguments[0].value;
0
0
1
+ 3 other calls in file
117 118 119 120 121 122 123 124 125 126
// 创建一个模块ID,就是相对于根目录的相对路径 dependencies就是此模块依赖的模块 // name是模块所属的代码块的名称,如果一个模块属于多个代码块,那么name就是一个数组 let module = { id: moduleId, dependencies: [], names: [name] } // 必须这里就push返回就解决 // 解析源码生成ast抽象语法树,分析模块依赖 let ast = parser.parse(sourceCode, { sourceType: 'module' }) // visitor是babel对ast的遍历器或者访问器 // ast可视化 https://astexplorer.net/ traverse(ast, { CallExpression: ({ node }) => {
0
0
1
+ 3 other calls in file
GitHub: randibrahim/AST-Pruning
125 126 127 128 129 130 131 132 133 134
var fileSrc = fs.readFileSync('C:\\Users\\Tec\\React Native Applications\\Upload-main\\backend\\uploadedFiles\\' + file); console.log(file); // The readFileSync() function returns a Buffer. //we can use the toString() method on the Buffer object to get the contents as String. // 4- parse the file content using the babel parser ast = babelParser.parse(fileSrc.toString(), { sourceType: "module", plugins: [ // enable jsx and js syntax "jsx",
0
0
1
12 13 14 15 16 17 18 19 20 21 22
'hello' != 'world' 0 == 0 true == true `; const ast = parser.parse(sourceCode, { sourceType: "unambiguous", // comments: true, });
0
0
1
+ 3 other calls in file
26 27 28 29 30 31 32 33 34 35
namesFile = fs.readFileSync( path.join(__dirname, "../names") + "/index.js", "utf-8" ); names = require("../names/index.js"); namesFileBabelObject = babel.parse(namesFile, configuration); } catch (error) {} }); it(":::NJSCPUSJFR_TEST_1:::A file should be created in 'name' directory as 'index.js'", () => {
0
0
1
25 26 27 28 29 30 31 32 33 34
moduleFile = fs.readFileSync( path.join(__dirname, "../utilities") + "/ratioFactorial/index.js", "utf-8" ); ratioAndFactorial = require("../utilities/ratioFactorial/index.js"); moduleFileBabelObject = babel.parse(moduleFile, configuration); } catch (error) { console.log(error, "error"); } });
0
0
1
3493 3494 3495 3496 3497 3498 3499 3500 3501 3502
// 3. Function arguments (v-for, v-slot): place in a function argument position const source = asRawStatements ? ` ${rawExp} ` : `(${rawExp})${asParams ? `=>{}` : ``}`; try { ast = parser.parse(source, { plugins: context.expressionPlugins }).program; } catch (e) {
0
0
1
23 24 25 26 27 28 29 30 31 32
} if (filename.endsWith('x')) { plugins.push('jsx'); } } const ast = parser.parse(src, { sourceType: 'module', plugins }); const s = new MagicString(src);
0
0
1
GitHub: DouLu/webpack
98 99 100 101 102 103 104 105 106 107
}, sourceCode); //通过loader翻译后的内容一定得是js内容,因为最后得走我们babel-parse,只有js才能成编译AST //第七步:找出此模块所依赖的模块,再对依赖模块进行编译 //7.1:先把源代码编译成 [AST](https://astexplorer.net/) let ast = parser.parse(sourceCode, { sourceType: 'module' }); traverse(ast, { CallExpression: (nodePath) => { const { node } = nodePath; //7.2:在 `AST` 中查找 `require` 语句,找出依赖的模块名称和绝对路径
0
0
1
110 111 112 113 114 115 116 117 118 119
// webpack最核心的几个概念:module,module中有模块ID、模块依赖数组 let module = { id: moduleId, dependencies: [], name } // 现在我们已经得到了转换后的代码:babel-loader es6=>es5 // 7. 再找出该模块依赖的模块,再递归本步骤直到所有入口依赖的文件都经过了本步骤的处理 // { sourceType: 'module' }表示原代码是一个模块。模块就是里面有module.exports、 exports let astTree = parser.parse(targetSourceCode, { sourceType: 'module' }); // 遍历语法树,并找出require节点 traverse(astTree, { CallExpression: ({ node }) => { // node 节点 if (node.callee.name === 'require') { // 是require
0
0
1
+ 2 other calls in file
6 7 8 9 10 11 12 13 14 15 16 17 18 19
'use strict'; const evalToString = require('../evalToString'); const parser = require('@babel/parser'); const parse = source => parser.parse(`(${source});`).program.body[0].expression; // quick way to get an exp node const parseAndEval = source => evalToString(parse(source)); describe('evalToString', () => {
0
0
1
27 28 29 30 31 32 33 34 35 36
default\\b|delete\\b|do\\b|else\\b|export\\b|extends\\b|finally\\b|in\\b|instanceof\\b|let\\b|return\\b|super\\b|switch\\b|throw\\b|try\\b|show\\b|tid\\b)$`, 'g') try { if (reservedKeyWords.test(expression)) { throw Error("A data binding parsing error occurred. Do not use the reserved:" + expression).message } let expAst = isEventFunc ? parser.parse('(' + expression + ')') : parser.parse(expression) traverse(expAst, { enter(path) { if (path.parent && path.node.type === "Identifier") { let flag = false
0
0
0
@babel/parser.parse is the most popular function in @babel/parser (46 examples)