How to use newAst method in storybook-root

Best JavaScript code snippet using storybook-root

Ast.js

Source:Ast.js Github

copy

Full Screen

1const generate = require('./js-core/generate');2const htmlGenerate = require('./html-core/serialize-node');3const vueGenerate = require('./vue-core/generate');4const core = require('./js-core/core');5const htmlCore = require('./html-core/core')6const vueCore = require('./vue-core/core')7const NodePath = require('./NodePath');8const filterProp = require('./js-core/filter-prop')9const { isObject } = require('./util')10const languageMap = {11 'js': { 12 generate,13 core14 },15 'html': { 16 generate: htmlGenerate,17 core: htmlCore18 },19 'vue': { 20 generate: vueGenerate,21 core: vueCore22 }23}24class AST {25 constructor(nodePath, { parseOptions, match, rootNode } = {}) {26 if (nodePath) {27 this[0] = {28 nodePath, match29 }30 }31 this.rootNode = rootNode;32 this.expando = 'g' + ('' + Math.random()).replace( /\D/g, "" ) + 'g';33 this.parseOptions = parseOptions;34 }35 get node() {36 return this[0].nodePath.node37 }38 get value() {39 return this[0].nodePath.value40 }41 get match() {42 return this[0].match43 }44 get isHtml() {45 return this.parseOptions && (this.parseOptions.html || this.parseOptions.language == 'html');46 }47 get language() {48 return (this.parseOptions && this.parseOptions.language) || 'js';49 }50 get core() {51 return languageMap[this.language].core52 }53 get _index() {54 initParent(this);55 // todo js56 return this[0]._index;57 }58 get length() {59 let i = 0;60 while(this[i]) {61 i++62 }63 return i;64 }65 each(callback) {66 let i = 0;67 const newAST = cloneAST(this)68 while (this[i]) {69 const { nodePath, match } = this[i]70 const eachNode = new AST(nodePath, { parseOptions: this.parseOptions, match, rootNode: this.rootNode})71 callback(eachNode, i);72 newAST[i] = eachNode[0] || null73 i++;74 }75 return newAST76 }77 find(selector, options = {}) {78 if (!selector) {79 throw new Error('find failed! first argument should not be null!')80 }81 if (!this[0]) {82 return this;83 }84 const { nodePath } = this[0];85 // if (typeof selector !== 'string' && !Array.isArray(selector)) {86 // throw new Error('find failed! Nodepath is null!');87 // }88 const pOptions = options.parseOptions || this.parseOptions;89 const {nodePathList, matchWildCardList, extra = {} } = this.core.getAstsBySelector(90 nodePath.node,91 selector, {92 strictSequence: options.ignoreSequence === false,93 parseOptions: pOptions,94 expando: this.expando95 }96 );97 const newAST = cloneAST(this)98 if (!newAST.rootNode) {99 newAST.rootNode = this[0].nodePath;100 }101 nodePathList.forEach((nodePath, i) => {102 // 把this里的parentPath接到nodePath上103 if (this.language == 'js') {104 let theNodePath = nodePath;105 while(theNodePath.parentPath) {106 if (theNodePath.parentPath && theNodePath.parentPath.name == 'root') {107 if (theNodePath.parentPath.node.type != 'File') {108 theNodePath.parentPath = this[0].nodePath;109 }110 break;111 }112 theNodePath = theNodePath.parentPath;113 }114 }115 newAST[i] = { nodePath, parseOptions: extra.parseOptions || pOptions, match: matchWildCardList[i] };116 })117 if (extra.parseOptions) {118 newAST.parseOptions = extra.parseOptions119 }120 return newAST;121 }122 parent(level = 0) {123 if (!this[0]) {124 return this;125 }126 // if (!this[0].parentList) {127 initParent(this)128 // }129 const parent = this[0].parentList[level]130 const newAST = cloneAST(this)131 if (parent) {132 newAST[0] = { nodePath: parent, parseOptions: this.parseOptions };133 return newAST;134 } else {135 return this;136 }137 }138 parents() {139 if (!this[0]) {140 return this;141 }142 // if (!this[0].parentList) {143 initParent(this)144 // }145 const { parentList } = this[0];146 const newAST = cloneAST(this)147 parentList.forEach((nodePath, i) => {148 newAST[i] = { nodePath, parseOptions: this.parseOptions, match: null };149 })150 return newAST;151 }152 root(option) {153 if (!this.rootNode) {154 return this;155 }156 const newAST = cloneAST(this)157 newAST[0] = { nodePath: this.rootNode }158 newAST.rootNode = null;159 if (this.parseOptions && this.parseOptions.rootLanguage == 'vue') {160 if (option == 'template') {161 newAST[0] = { nodePath: this.rootNode.node.templateAst }162 } else if (option == 'script') {163 newAST[0] = { nodePath: this.rootNode.node.scriptAst }164 } else {165 newAST.parseOptions = { language: 'vue' };166 }167 }168 return newAST;169 }170 has(selector, options) {171 return !!this.find(selector, options)[0]172 }173 siblings() {174 if (!this[0]) {175 return this;176 }177 // if (!Array.isArray(this[0].siblings)) {178 initSiblings(this);179 // }180 const siblings = this[0].siblings || [];181 const newAST = cloneAST(this)182 siblings.forEach((sibling, i) => {183 newAST[i] = sibling184 });185 return newAST;186 }187 prevAll() {188 if (!this[0]) {189 return this;190 }191 // if (!Array.isArray(this[0].siblings)) {192 initSiblings(this);193 // }194 const prevAll = this[0].prevAll || [];195 const newAST = cloneAST(this)196 prevAll.forEach((prev, i) => {197 newAST[i] = prev198 });199 return newAST;200 }201 prev() {202 if (!this[0]) {203 return this;204 }205 // if (!Array.isArray(this[0].siblings)) {206 initSiblings(this);207 // }208 const prevAll = this[0].prevAll || [];209 const newAST = cloneAST(this)210 newAST[0] = prevAll[prevAll.length - 1];211 return newAST;212 }213 nextAll() {214 if (!this[0]) {215 return this;216 }217 // if (!Array.isArray(this[0].siblings)) {218 initSiblings(this);219 // }220 const nextAll = this[0].nextAll || [];221 const newAST = cloneAST(this)222 nextAll.forEach((next, i) => {223 newAST[i] = next224 });225 return newAST;226 }227 next() {228 if (!this[0]) {229 return this;230 }231 // if (!Array.isArray(this[0].siblings)) {232 initSiblings(this);233 // }234 const nextAll = this[0].nextAll || [];235 const newAST = cloneAST(this)236 newAST[0] = nextAll[0];237 return newAST;238 }239 eq(index) {240 index = index || 0;241 const { nodePath, match } = this[index] || {}242 const newAST = cloneAST(this)243 newAST[0] = { nodePath, parseOptions: this.parseOptions, match }244 return newAST;245 }246 attr(arg1, arg2) {247 if (!this[0] || !this[0].nodePath || !this[0].nodePath.node) {248 return this;249 }250 let attrMap = {};251 if (arg2) {252 // arg1是key arg2是value253 if (typeof arg1 == 'string') {254 attrMap = { [arg1]: arg2 }255 } else {256 throw new Error('attr failed! args[0] should be string!')257 }258 } else {259 if (typeof arg1 == 'string') {260 // 取某个属性261 return getAttrValue(this[0].nodePath.node, arg1);262 } else if (typeof arg1 == 'object') {263 attrMap = arg1;264 }265 }266 setAttrValue(this[0].nodePath.node, attrMap);267 return this;268 }269 child(attrName) {270 if (!this[0] || !this[0].nodePath || !this[0].nodePath.node) {271 return this;272 }273 const keyList = attrName.split('.');274 let currentNode = this.node;275 let nodePath = this[0].nodePath;276 let parentPath = this[0].nodePath.parentPath;277 let newNodeAST;278 let deep = 0;279 if (this.node.program) {280 nodePath = nodePath.get('program', 'body', '0')281 currentNode = currentNode.program.body[0];282 }283 keyList.forEach(attr => {284 const node = currentNode[attr];285 if (node) {286 if (this.language == 'js') {287 newNodeAST = cloneAST(this);288 nodePath = nodePath.get(attr)289 newNodeAST[0] = { nodePath, parseOptions: this.parseOptions }290 } else {291 newNodeAST = cloneAST(this);292 parentPath = nodePath293 nodePath = new NodePath(currentNode[attr], nodePath, nodePath)294 newNodeAST[0] = { nodePath, parseOptions: this.parseOptions }295 }296 currentNode = node297 deep++298 }299 })300 if (deep == keyList.length) {301 return newNodeAST;302 } else {303 return null304 }305 }306 clone() {307 if (!this[0]) {308 return this;309 }310 let nodePath;311 // html深度克隆时需要忽略parentRef312 if (this.isHtml) {313 const parentRefList = []314 markParent(this[0].nodePath.node)315 const newNode = JSON.parse(JSON.stringify(this[0].nodePath.node));316 resetParent(newNode);317 resetParent(this[0].nodePath.node);318 nodePath = new NodePath(319 newNode, 320 this[0].nodePath.parent, 321 this[0].nodePath.parentPath 322 )323 function resetParent(node) {324 for (let key in node) {325 if (key == 'parentRef') {326 node[key] = parentRefList[node[key]]327 } else if (isObject(node[key])) {328 if (Array.isArray(node[key])) {329 node[key].forEach(n => {330 resetParent(n)331 });332 } else {333 resetParent(node[key]);334 }335 }336 }337 }338 function markParent(node) {339 for (let key in node) {340 if (key == 'parentRef') {341 parentRefList.push(node[key]);342 node[key] = parentRefList.length - 1;343 } else if (isObject(node[key])) {344 if (Array.isArray(node[key])) {345 node[key].forEach(n => {346 markParent(n)347 });348 } else {349 markParent(node[key]);350 }351 }352 }353 }354 355 } else {356 const node = {};357 // js需要做一层属性过滤,否则会有环形依赖358 filterProp(this[0].nodePath.node, node, [359 'computed',360 'range',361 'loc',362 'start',363 'end',364 'leadingComments',365 'shorthand',366 'extra',367 'static',368 'typeParameters'369 ]);370 nodePath = new NodePath(371 // JSON.parse(JSON.stringify(this[0].nodePath.node)), 372 JSON.parse(JSON.stringify(node)), 373 this[0].nodePath.parent, 374 this[0].nodePath.parentPath375 )376 }377 const { match } = this[0]378 const newAST = cloneAST(this)379 newAST[0] = { nodePath, parseOptions: this.parseOptions, match }380 return newAST;381 }382 replace(selector, replacer, { ignoreSequence, parseOptions } = {}) {383 if (!this[0]) {384 // throw new Error('replace failed! Nodepath is null!');385 return this;386 }387 this.core.replaceSelBySel(this[0].nodePath, selector, replacer, ignoreSequence === false, parseOptions, this.expando)388 return this;389 }390 replaceBy(replacer) {391 if (!this[0]) {392 return this.root();393 }394 if (replacer[0] && replacer[0].nodePath) {395 replacer = replacer[0].nodePath.node396 }397 if (typeof replacer == 'string') {398 replacer = this.core.buildAstByAstStr(replacer);399 }400 if (replacer.type == 'File') {401 replacer = replacer.program.body[0]402 }403 let i = 0;404 while(this[i]) {405 this.core.replaceAstByAst(this[i].nodePath, replacer)406 i++407 }408 409 return this;410 }411 insertSiblingNode(node, type) {412 if (!this[0]) {413 return this;414 }415 if (!node.type && !node.nodeType) {416 throw new Error('insert failed! Unexpected node for insert!')417 }418 // if (!this[0].parentList) {419 initParent(this)420 // }421 if (this.isHtml) {422 let p;423 let index = -1424 if (this.node.nodeType == 'document') {425 p = this.node.content.children;426 index = type == 'before' ? 0 : p.length - 1427 } else {428 const parent = this.parent();429 // todotodo430 p = parent.attr('content.children') || [];431 p.forEach((item, i) => {432 if (item == this.node) {433 index = i434 }435 })436 }437 if (type == 'before') {438 p.splice(index, 0, node)439 } else {440 p.splice(index + 1, 0, node)441 }442 } else {443 const parentList = this[0].parentList;444 if ((!parentList || parentList.length == 0) && this.node.type == 'File') {445 if (type == 'before') {446 this.attr('program.body').unshift(node)447 } else {448 this.attr('program.body').push(node)449 }450 return;451 }452 let getArrayParent = false;453 let i = 0;454 let selfPathNode = this[0].nodePath.value;455 let selfIndex = -1;456 while(!getArrayParent) {457 if (!parentList[i] || !parentList[i].value) {458 getArrayParent = true;459 } else if (Array.isArray(parentList[i].value)) {460 getArrayParent = true;461 parentList[i].value.forEach((nodePath, index) => {462 if (nodePath == selfPathNode) {463 selfIndex = index;464 }465 })466 if (type == 'after') {467 parentList[i].value.splice(selfIndex + 1, 0, node)468 } else {469 parentList[i].value.splice(selfIndex, 0, node)470 }471 }472 selfPathNode = parentList[i].value;473 i++474 }475 }476 }477 after(node) {478 if (!node) {479 throw new Error('after failed! Unexpected node for insert!')480 }481 if (typeof node == 'string') {482 node = this.core.buildAstByAstStr(node)483 }484 if (node[0] && node[0].nodePath) {485 node = node[0].nodePath.value486 }487 if (node.type == 'File') {488 if (node.program.body.length > 0) {489 node.program.body.forEach(item => {490 this.insertSiblingNode(item, 'after');491 })492 return this;493 } else {494 return this;495 }496 }497 if (!Array.isArray(node)) {498 node = [node]499 }500 node.forEach(n => {501 this.insertSiblingNode(n, 'after');502 })503 return this;504 }505 before(node) {506 if (!node) {507 throw new Error('before failed! Unexpected node for insert!')508 }509 if (typeof node == 'string') {510 node = this.core.buildAstByAstStr(node)511 }512 if (node[0] && node[0].type == 'Decorator') {513 this.node.decorators = (this.node.decorators || []).concat(node);514 return this;515 }516 if (node[0] && node[0].nodePath) {517 node = node[0].nodePath.value518 }519 if (node.type == 'File') {520 if (node.program.body.length > 0) {521 node.program.body.reverse().forEach(item => {522 this.insertSiblingNode(item, 'before');523 })524 return this;525 } else {526 return this;527 }528 }529 if (!Array.isArray(node)) {530 node = [node]531 }532 node.reverse().forEach(n => {533 this.insertSiblingNode(n, 'before');534 })535 return this;536 }537 insertChildNode(attr, node, type) {538 if (!this[0] || !this[0].nodePath) {539 return;540 }541 let selfNode = this[0].nodePath.value;542 if (!Array.isArray(selfNode)) {543 // for(let key in selfNode) {544 // if (Array.isArray(selfNode[key])) {545 // selfNode = selfNode[key]546 // }547 // }548 if (attr == 'content.children') {549 selfNode = selfNode.content.children;550 } else if (selfNode.program && selfNode.program.body) {551 if (attr == 'program.body') {552 selfNode = selfNode.program.body;553 } else {554 selfNode.program.body[0][attr] = selfNode.program.body[0][attr] || []555 selfNode = selfNode.program.body[0][attr]556 }557 } else {558 selfNode[attr] = selfNode[attr] || [];559 selfNode = selfNode[attr];560 if (!Array.isArray(selfNode)) {561 selfNode = selfNode.body562 }563 }564 565 }566 if (node.type == 'File' && node.program.body) {567 node = node.program.body[0]568 if (!node) return;569 }570 if (selfNode) {571 if (type == 'append') {572 selfNode.push(node);573 } else {574 selfNode.unshift(node);575 }576 }577 }578 append(attr, node) {579 if (!attr) {580 return this;581 }582 if (this.isHtml) {583 node = attr;584 attr = 'content.children';585 }586 if (!node) {587 node = attr;588 attr = 'program.body'589 }590 if (typeof node == 'string') {591 node = this.core.buildAstByAstStr(node)592 }593 if (node[0] && node[0].nodePath) {594 node = node[0].nodePath.value595 }596 if (!Array.isArray(node)) {597 node = [node]598 }599 node.forEach(n => {600 this.insertChildNode(attr, n, 'append');601 })602 return this;603 }604 prepend(attr, node) {605 if (this.isHtml) {606 node = attr;607 attr = 'content.children';608 }609 if (!node) {610 node = attr;611 attr = 'program.body'612 }613 if (typeof node == 'string') {614 node = this.core.buildAstByAstStr(node)615 }616 if (node[0] && node[0].nodePath) {617 node = node[0].nodePath.value618 }619 if (!Array.isArray(node)) {620 node = [node]621 }622 node.reverse().forEach(n => {623 this.insertChildNode(attr, n, 'prepend');624 })625 return this;626 }627 empty() {628 if (Array.isArray(this[0].nodePath.value)) {629 this[0].nodePath.value = [];630 } else if (this.node.type == 'File') {631 this.attr('program.body', [])632 }633 return this634 }635 remove(selector, options = {}) {636 if (!this[0]) {637 return this.root()638 }639 if (typeof selector == 'string' || Array.isArray(selector)) {640 const pOptions = options.parseOptions || this.parseOptions;641 let i = 0;642 while(this[i]) {643 this.core.removeAst(this.node, selector, { 644 strictSequence: options.ignoreSequence === false, 645 parseOptions: pOptions, 646 expando: this.expando647 });648 i++649 }650 } else {651 let i = 0;652 while(this[i]) {653 this.core.remove(this[i].nodePath)654 i++655 }656 657 }658 return this.root()659 }660 generate({ isPretty = false } = {}) {661 if (!this[0]) {662 return '';663 }664 if (this.language == 'js') {665 return generate(this[0].nodePath.node, isPretty)666 } else {667 return (languageMap[this.language].generate)(this[0].nodePath.value);668 }669 }670}671function cloneAST(ast) {672 const newAST = new AST('', { parseOptions: ast.parseOptions, rootNode: ast.rootNode})673 if (ast.sfc) {674 newAST.sfc = ast.sfc675 }676 return newAST677}678function getAttrValue(node, attr) {679 const keyList = attr.split('.');680 let currentNode = node;681 let deep = 0;682 keyList.forEach(attr => {683 if (currentNode[attr]) {684 currentNode = currentNode[attr];685 deep++686 }687 })688 if (deep == keyList.length) {689 return currentNode;690 } else {691 return null692 }693}694function initParent(ast) {695 if (ast.isHtml) {696 ast[0].parentList = ast.core.getParentListByAst(ast[0].nodePath)697 ast[0]._index = ast[0].parentList[0] ? ast[0].parentList[0].node.content.children.indexOf(ast[0].nodePath.node) : 0;698 } else {699 ast[0].parentList = core.getParentListByAst(ast[0].nodePath)700 }701 return ast[0].parentList702}703function initSiblings(ast) {704 if (ast.language == 'html') {705 const parent = ast.parent();706 const siblings = (parent.attr('content.children') || []).map((node, index) => {707 return {708 _index: index,709 nodePath: new NodePath(node, parent[0].nodePath, parent[0].nodePath),710 parseOptions: ast.parseOptions711 }712 });713 714 ast[0].siblings = siblings;715 ast[0].prevAll = siblings.filter(s => s._index < ast._index);716 ast[0].nextAll = siblings.filter(s => s._index > ast._index);717 } else {718 const parentList = initParent(ast);719 if (!parentList || parentList.length == 0) {720 return;721 }722 const parseOptions = ast.parseOptions;723 let getArrayParent = false;724 let i = 0;725 const siblings = [];726 const prevAll = [];727 const nextAll = [];728 let selfPathNode = ast[0].nodePath.value;729 while(!getArrayParent) {730 if (!parentList[i] || !parentList[i].value) {731 getArrayParent = true;732 } else if (Array.isArray(parentList[i].value)) {733 getArrayParent = true;734 let isPrev = true;735 let childIndex = 0736 while (parentList[i].__childCache[childIndex]) {737 const nodePath = parentList[i].__childCache[childIndex]738 if (nodePath.value == selfPathNode) {739 // find self740 isPrev = false;741 } else {742 siblings.push({ nodePath, parseOptions })743 if (isPrev) {744 prevAll.push({ nodePath, parseOptions })745 } else {746 nextAll.push({ nodePath, parseOptions })747 }748 }749 childIndex++750 }751 ast[0].siblings = siblings;752 ast[0].prevAll = prevAll;753 ast[0].nextAll = nextAll;754 }755 selfPathNode = parentList[i].value;756 i++757 }758 } 759}760function setAttrValue(node, attrMap) {761 for(const key in attrMap) {762 const value = attrMap[key];763 const keyList = key.split('.');764 let currentNode = node;765 keyList.forEach((attr, index) => {766 if (index == keyList.length - 1) {767 currentNode[attr] = value;768 } else if (currentNode[attr]) {769 currentNode = currentNode[attr]770 }771 })772 }773 774}...

Full Screen

Full Screen

ast_updater.js

Source:ast_updater.js Github

copy

Full Screen

1define(function(require, exports, module) {2 var infer = require("./infer");3 var assert = require("c9/assert");4 var tree = require("treehugger/tree");5 6 /**7 * A regex determining if it is likely8 * possible to update the old AST, if it's diff matches this.9 * Note that a more expensive, AST-based check is performed10 * after this.11 */12 var REGEX_SAFE_CHANGE = /^[\(\)\s\.\/\*+;,A-Za-z-0-9_$]*$/;13 14 var lastAST;15 var lastDocValue;16 17 /**18 * Attempts to reuse & update the previously analyzed AST,19 * or re-analyzes as needed, using infer.update().20 * 21 * @param callback22 * @param callback.ast The analyzed AST to use.23 */24 module.exports.updateOrReanalyze = function(doc, ast, filePath, basePath, pos, callback) {25 // Try with our last adapted AST26 var docValue = doc.getValue();27 var updatedAST = tryUpdateAST(doc, docValue, ast);28 if (updatedAST) {29 if (ast.getAnnotation("scope")) {30 lastDocValue = docValue;31 lastAST = updatedAST;32 }33 // console.log("[ast_updater] reused AST"); // DEBUG34 return callback(updatedAST, findNode(updatedAST, pos));35 }36 37 // Re-analyze instead38 var start = new Date().getTime();39 return infer.analyze(doc, ast, filePath, basePath, function() {40 // console.log("[ast_updater] Reanalyzed in " + (new Date().getTime() - start) + "ms"); // DEBUG41 lastDocValue = docValue;42 lastAST = ast;43 callback(ast, findNode(ast, pos));44 }, true);45 };46 47 function tryUpdateAST(doc, docValue, ast) {48 if (lastAST && (!lastAST.annos || !lastAST.annos.scope)) {49 console.error("Warning: Source does not appear to be analyzed yet; restarting analysis");50 return false;51 }52 if (lastDocValue === docValue) {53 // Note: if this message appears when it shouldn't, something is54 // wrong with the mirror (mirror.js)55 // console.log("[ast_updater] Doc appears unchanged; reusing analysis");56 return lastAST;57 }58 if (!isUpdateableAST(doc, docValue, ast))59 return null;60 61 if (!copyAnnosTop(lastAST, ast, true))62 return null;63 copyAnnosTop(lastAST, ast);64 assert(ast.annos.scope, "Target is empty");65 return ast;66 }67 68 /**69 * Performs a simple, performant check to see if the70 * input is eligle for reusing the previous analysis.71 *72 * @returns {Boolean} true if the old AST may be reusable73 */74 function isUpdateableAST(doc, docValue, ast) {75 if (!lastDocValue)76 return false;77 var diff = getDiff(lastDocValue, docValue) || getDiff(docValue, lastDocValue);78 79 return diff && diff.text.match(REGEX_SAFE_CHANGE);80 }81 82 function copyAnnosTop(oldAST, newAST, dryRun) {83 if (!dryRun) copyAnnos(oldAST, newAST);84 85 for (var i = 0, j = 0; j < newAST.length; i++, j++) {86 if (!oldAST[i]) {87 if (newAST[j].cons !== "Var")88 return false;89 // Var(x) was just inserted90 copyAnnos(findScopeNode(oldAST), newAST[j]);91 if (!newAST[j].annos)92 return false;93 continue;94 }95 if (oldAST[i].cons !== newAST[j].cons) {96 // Var(x) became PropAccess(Var(x), y)97 if (oldAST[i].cons === "Var" && newAST[j].isMatch("PropAccess(Var(_),_)")) {98 copyAnnos(oldAST[i], newAST[j][0]);99 continue;100 }101 // PropAccess(Var(x), y) became Var(x)102 if (newAST[j].cons === "Var" && oldAST[i].isMatch("PropAccess(Var(_),_)")) {103 copyAnnos(oldAST[i][0], newAST[j]);104 continue;105 }106 // PropAccess became Call(PropAccess, _)107 if (oldAST[i].isMatch("PropAccess(Var(_),_)") && newAST[j].isMatch("Call(PropAccess(Var(_),_),_)")) {108 copyAnnos(oldAST[i][0], newAST[j][0][0]);109 var oldTemplate = new tree.ListNode([oldAST[i][0]]);110 oldTemplate.parent = oldAST;111 copyAnnosTop(oldTemplate, newAST[j][1], dryRun);112 continue;113 }114 // Call(PropAccess, _) became PropAccess115 if (newAST[j].isMatch("PropAccess(Var(_),_)") && oldAST[i].isMatch("Call(PropAccess(Var(_),_),_)")) {116 copyAnnos(oldAST[i][0][0], newAST[j][0]);117 continue;118 }119 // Var(x) was (possibly) inserted120 if (newAST[j].cons === "Var" && newAST[j + 1] && newAST[j + 1].cons === oldAST[i].cons) {121 copyAnnos(findScopeNode(oldAST), newAST[j]);122 if (!newAST[j].annos)123 return false;124 i--;125 continue;126 }127 // Var(x) was (possibly) added128 if (oldAST[i].cons === "None" && newAST[j].cons === "Var") {129 copyAnnos(findScopeNode(oldAST), newAST[j]);130 if (!newAST[j].annos)131 return false;132 i--;133 continue;134 }135 // Var(x) was (possibly) removed136 if (oldAST[i].cons === "Var" && oldAST[i + 1] && oldAST[i + 1].cons === newAST[i].cons) {137 j--;138 continue;139 }140 // [stm1, stm2] became [If(Stm1), stm2] or [If(stm2)]141 if (["If", "Return", "Throw"].indexOf(newAST[j].cons) > -1 && (!newAST[j][1] || newAST[j][1].isMatch("Block([])"))) {142 var cond = newAST[j][0].toString();143 if (cond === oldAST[i].toString()) {144 copyAnnos(oldAST[i], newAST[j][0]);145 continue;146 }147 else if (!oldAST[i + 1]) {148 continue;149 }150 else if (cond === oldAST[i + 1].toString()) {151 i++;152 copyAnnos(oldAST[i], newAST[j][0]);153 continue;154 }155 }156 // "if () s" became "if (c) s"157 if (oldAST.cons === "If" && newAST.cons === "If" && newAST[0].cons === "Var" && oldAST[1].isMatch("Block([])")) {158 var oldCond = oldAST[0];159 var newCond = newAST[0];160 var newBody = newAST[1];161 if (oldCond.toString() === newBody.toString()) {162 copyAnnos(findScopeNode(oldAST), newCond);163 if (!newCond.annos)164 return false;165 copyAnnos(oldCond, newBody);166 continue;167 }168 }169 return false;170 }171 if (newAST[j].length) {172 if (!copyAnnosTop(oldAST[i], newAST[j], dryRun))173 return false;174 } else if (!dryRun && newAST[j].$pos) {175 copyAnnos(oldAST[i], newAST[j]);176 }177 178 }179 return true;180 }181 182 function copyAnnos(oldNode, newNode) {183 newNode.oldNode = oldNode.oldNode || oldNode;184 newNode.oldNode.$pos = newNode.$pos;185 186 if (!oldNode.annos)187 return;188 newNode.annos = oldNode.annos;189 }190 191 function findScopeNode(ast) {192 if (!ast)193 return null;194 if (ast.annos && ast.annos.scope)195 return ast;196 return findScopeNode(ast.parent);197 }198 199 function getDiff(oldDoc, newDoc) {200 if (oldDoc.length > newDoc.length)201 return null;202 203 var diffLeft = -1;204 var diffRight = 0;205 206 for (var i = 0; i < newDoc.length; i++) {207 if (oldDoc[i] !== newDoc[i]) {208 diffLeft = i;209 break;210 }211 }212 213 for (var i = newDoc.length, j = oldDoc.length; j >= 0; i--, j--) {214 if (oldDoc[j] !== newDoc[i]) {215 diffRight = i + 1;216 break;217 }218 }219 220 assert(diffLeft != -1, "Inputs can't be equal");221 222 return {223 start: diffLeft,224 end: diffRight,225 text: newDoc.substring(diffLeft, diffRight)226 };227 }228 229 function findNode(ast, pos) {230 var treePos = { line: pos.row, col: pos.column };231 return ast.findNode(treePos);232 }233 234 ...

Full Screen

Full Screen

core.js

Source:core.js Github

copy

Full Screen

1const parse = require('./parse');2const jsCore = require('../js-core/core');3const htmlCore = require('../html-core/core')4const NodePath = require('../NodePath');5const core = {6 getAstsBySelector(ast, selector, { parseOptions } = {}) {7 parseOptions = Object.assign({}, parseOptions);8 let newAst = ast;9 if (selector == '<template></template>') {10 parseOptions.language = 'html';11 parseOptions.rootLanguage = 'vue';12 if (ast.templateAst) {13 newAst = ast.templateAst;14 } else {15 ast.templateAst = core.getTemplate(ast);16 newAst = ast.templateAst;17 }18 } else if (selector == '<script></script>') {19 parseOptions.language = 'js'20 parseOptions.rootLanguage = 'vue';21 if (ast.scriptAst) {22 newAst = ast.scriptAst;23 } else {24 ast.scriptAst = core.getScript(ast);25 newAst = ast.scriptAst;26 }27 } else if (selector == '<script setup></script>') {28 parseOptions.language = 'js'29 parseOptions.rootLanguage = 'vue';30 if (ast.scriptSetupAst) {31 newAst = ast.scriptSetupAst;32 } else {33 ast.scriptSetupAst = core.getScript(ast, { isSetup: true });34 newAst = ast.scriptSetupAst;35 }36 }37 return { nodePathList: newAst ? [newAst] : [], matchWildCardList: [], extra: { parseOptions } }38 },39 getTemplate(ast) {40 // 仅针对vue,取template,后续通过htmlcore处理41 if (ast.template) {42 const template = htmlCore.buildAstByAstStr(43 ast.template.content,44 {},45 {46 isProgram: true,47 parseOptions: { language: 'html' }48 }49 );50 return new NodePath(template);51 } else {52 return undefined;53 }54 },55 getScript(ast, { isSetup = false } = {} ) {56 // 仅针对vue,取script,后续通过jscore处理57 let content;58 if (isSetup && ast.scriptSetup) {59 content = ast.scriptSetup.content;60 } else if (!isSetup && ast.script) {61 content = ast.script.content62 // const content = ast.script.content.replace(/\n/g, '')63 }64 if (content) {65 const script = jsCore.buildAstByAstStr(66 content, {},67 { isProgram: true }68 );69 return new NodePath(script);70 } else {71 return undefined;72 }73 },74 buildAstByAstStr(str, astPatialMap = {}, { isProgram = false, parseOptions } = {}) {75 try {76 const program = parse(str, parseOptions);77 if (program) {78 if (isProgram) {79 return program;80 } else {81 if (program.template && program.template.ast) {82 return program.template83 } else return null84 }85 } else {86 return null;87 }88 } catch(e) {89 console.log('buildAstByAstStr failed:' + e)90 }91 }92}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const rootCause = require('storybook-root-cause');2const ast = rootCause.newAst('path/to/storybook');3const rootCause = require('storybook-root-cause');4const ast = rootCause.newAst('path/to/storybook');5const rootCause = require('storybook-root-cause');6const ast = rootCause.newAst('path/to/storybook');7const rootCause = require('storybook-root-cause');8const ast = rootCause.newAst('path/to/storybook');9const rootCause = require('storybook-root-cause');10const ast = rootCause.newAst('path/to/storybook');11const rootCause = require('storybook-root-cause');12const ast = rootCause.newAst('path/to/storybook');13const rootCause = require('storybook-root-cause');14const ast = rootCause.newAst('path/to/storybook');15const rootCause = require('storybook-root-cause');16const ast = rootCause.newAst('path/to/storybook');17const rootCause = require('storybook-root-cause');18const ast = rootCause.newAst('path/to/storybook');19const rootCause = require('storybook-root-cause');20const ast = rootCause.newAst('path/to/storybook');21const rootCause = require('storybook-root-cause');22const ast = rootCause.newAst('path/to/storybook');23const rootCause = require('storybook-root-cause');24const ast = rootCause.newAst('path/to/storybook');

Full Screen

Using AI Code Generation

copy

Full Screen

1import { newAst } from 'storybook-root';2const ast = newAst();3console.log(ast);4export { newAst } from './src/ast';5export const newAst = () => {6 return {7 };8};

Full Screen

Using AI Code Generation

copy

Full Screen

1const { newAst } = require('storybook-root-cause');2const ast = newAst(/*...*/);3const ast2 = newAst(/*...*/);4const { newAst } = require('storybook-root-cause');5const ast = newAst(/*...*/);6const ast2 = newAst(/*...*/);7const { newAst } = require('storybook-root-cause');8const ast = newAst(/*...*/);9const ast2 = newAst(/*...*/);10const { newAst } = require('storybook-root-cause');11const ast = newAst(/*...*/);12const ast2 = newAst(/*...*/);13const { newAst } = require('storybook-root-cause');14const ast = newAst(/*...*/);15const ast2 = newAst(/*...*/);16const { newAst } = require('storybook-root-cause');17const ast = newAst(/*...*/);18const ast2 = newAst(/*...*/);19const { newAst } = require('storybook-root-cause');20const ast = newAst(/*...*/);21const ast2 = newAst(/*...*/);22const { newAst } = require('storybook-root-cause');23const ast = newAst(/*...*/);24const ast2 = newAst(/*...*/);25const { newAst } = require('storybook-root-cause');26const ast = newAst(/*...*/);27const ast2 = newAst(/*...*/);28const { newAst } = require('storybook-root-cause');29const ast = newAst(/*...*/);30const ast2 = newAst(/*...*/);31const { newAst } = require('storybook-root-cause');32const ast = newAst(/*...*/);33const ast2 = newAst(/*...*/);

Full Screen

Using AI Code Generation

copy

Full Screen

1const storybookRootCause = require('storybook-root-cause');2const { newAst } = storybookRootCause;3const ast = newAst({4 storybook: {5 },6 storybookConfig: {7 },8});9const storybookRootCause = require('storybook-root-cause');10const { newAst } = storybookRootCause;11const ast = newAst({12 storybook: {13 },14 storybookConfig: {15 },16});17const storybookRootCause = require('storybook-root-cause');18const { newAst } = storybookRootCause;19const ast = newAst({20 storybook: {21 },22 storybookConfig: {23 },24});25const storybookRootCause = require('storybook-root-cause');26const { newAst } = storybookRootCause;27const ast = newAst({28 storybook: {29 },30 storybookConfig: {31 },32});33const storybookRootCause = require('storybook-root-cause');34const { newAst } = storybookRootCause;35const ast = newAst({36 storybook: {37 },38 storybookConfig: {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { newAst } = require('@storybook/root-cause');2const ast = newAst('path/to/file.js');3const { newAst } = require('@storybook/root-cause');4const ast = newAst('path/to/file.js');5const { newAst } = require('@storybook/root-cause');6const ast = newAst('path/to/file.js');7const { newAst } = require('@storybook/root-cause');8const ast = newAst('path/to/file.js');9const { newAst } = require('@storybook/root-cause');10const ast = newAst('path/to/file.js');11const { newAst } = require('@storybook/root-cause');12const ast = newAst('path/to/file.js');13const { newAst } = require('@storybook/root-cause');14const ast = newAst('path/to/file.js');15const { newAst } = require('@storybook/root-cause');16const ast = newAst('path/to/file.js');17const { newAst } = require('@storybook/root-cause');18const ast = newAst('path/to/file.js');

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-root automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful