版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

G.find.test.js 29 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. const $ = require('../index');
  2. const config = require('./config');
  3. const jc1 = require('./code/simple1');
  4. const jc2 = require('./code/simple2');
  5. const jc5 = require('./code/simple5');
  6. const jc6 = require('./code/simple6');
  7. const tc1 = require('./code/simple-ts-1');
  8. const hc1 = require('./code/simple1.html');
  9. const hc2 = require('./code/simple2.html');
  10. const jTryout = require('./code/simple-tryout');
  11. const glob = require('glob');
  12. const SIMPLE_CODE = `var a = 1;`;
  13. const CODE = `
  14. function test(c){
  15. let a = 1;
  16. let b = 2;
  17. return a + b + c;
  18. }
  19. test(11)`;
  20. const OBJECT_CODE = `
  21. const a = {
  22. name: 'jerry',
  23. age: 12
  24. }`;
  25. test('$.find: input is empty string should not throw error', () => {
  26. expect(()=>{
  27. const G = $(SIMPLE_CODE);
  28. G.find('');
  29. }).toThrow();
  30. })
  31. test('$.find: input is empty string should not throw error', () => {
  32. expect(()=>{
  33. const G = $(SIMPLE_CODE);
  34. const result = G.find('');
  35. }).toThrow();
  36. })
  37. test('$.find: input is simple selector should not throw error', () => {
  38. expect(()=>{
  39. const G = $(SIMPLE_CODE);
  40. G.find('var $_$ = $_$');
  41. }).not.toThrow();
  42. })
  43. test('$.find: input is simple selector result should be ok', () => {
  44. const G = $(SIMPLE_CODE);
  45. result = G.find('var $_$ = $_$').generate();
  46. expect(result).toBe('var a = 1;');
  47. })
  48. test('$.find: input is simple selector without $_$ should not throw error', () => {
  49. expect(()=>{
  50. const G = $(SIMPLE_CODE);
  51. G.find('var a = 1;');
  52. }).not.toThrow();
  53. })
  54. test('$.find: input is simple selector without $_$ result should be ok', () => {
  55. const G = $(SIMPLE_CODE);
  56. result = G.find('var a = 1;').generate();
  57. expect(result).toBe('var a = 1;');
  58. })
  59. test('$.find: input is string array should not throw error', () => {
  60. expect(()=>{
  61. const G = $(CODE);
  62. G.find(['let $_$ = $_$','test()']);
  63. }).not.toThrow();
  64. })
  65. test('$.find: input is string array result should be ok', () => {
  66. const G = $(CODE);
  67. const result = G.find(['let $_$ = $_$', 'test()']);
  68. const code0 = $(result.eq(0).node).generate();
  69. const code1 = $(result.eq(1).node).generate();
  70. const code2 = $(result.eq(2).node).generate();
  71. expect(code0 === 'let a = 1;' && code1 === 'let b = 2;' && code2 === 'test(11)').toBeTruthy();
  72. })
  73. test('$.find: result is return object', () => {
  74. expect(()=>{
  75. const G = $(CODE);
  76. G.find('return $_$');
  77. }).not.toThrow();
  78. })
  79. test('$.find: result is return object, result should be ok', () => {
  80. const G = $(CODE);
  81. const code = G.find('return $_$').generate();
  82. expect(code).toBe('return a + b + c;')
  83. })
  84. test('$.find: input is string array ,only one selector, should not throw error', () => {
  85. expect(()=>{
  86. const G = $(CODE);
  87. G.find(['var $_$ = $_$']);
  88. }).not.toThrow();
  89. })
  90. test('$.find: input is string array ,only one selector, result should be ok', () => {
  91. const G = $(CODE);
  92. const result = G.find(['let $_$ = $_$']).generate();
  93. expect(result).toBe('let a = 1;');
  94. })
  95. test('$.find: input is object should not throw error', () => {
  96. expect(()=>{
  97. const G = $(jc1);
  98. G.find({type: 'StringLiteral', value: '$_$'});
  99. }).not.toThrow();
  100. })
  101. test('$.find: input is object result should be ok', () => {
  102. const G = $(jc1);
  103. const code = G.find({ type: 'StringLiteral', value: '$_$' }).generate();
  104. expect(code).toBe("'../index'");
  105. })
  106. test('$.find: input is object array should not throw error', () => {
  107. expect(()=>{
  108. const G = $(CODE);
  109. G.find([{type: 'StringLiteral', value: '$_$'}, {type: 'Identifier', name: '$_$'}]);
  110. }).not.toThrow();
  111. })
  112. test('$.find: find properties key should not throw error', () => {
  113. expect(()=>{
  114. const G = $(OBJECT_CODE);
  115. G.find(`$_$:'jerry'`);
  116. }).not.toThrow();
  117. })
  118. test('$.find: find properties key result should be ok', () => {
  119. const G = $(OBJECT_CODE);
  120. // todo support key: value
  121. const code = G.find(`{ $_$ : 'jerry' }`).generate();
  122. expect(code.indexOf(`name`) > -1).toBeTruthy();
  123. })
  124. test('$.find: find object ', () => {
  125. expect(()=>{
  126. const G = $(OBJECT_CODE);
  127. G.find(`{$_$:'jerry'}`);
  128. }).not.toThrow();
  129. })
  130. test('$.find: find object result should be ok', () => {
  131. const G = $(OBJECT_CODE);
  132. const code = G.find(`{$_$:'jerry'}`).generate();
  133. expect(code.indexOf(`name: 'jerry',`) > -1).toBeTruthy();
  134. })
  135. test('$.find: find object result should be ok', () => {
  136. const G = $(OBJECT_CODE);
  137. const code = G.find(`{name:'jerry'}`).generate();
  138. expect(code.indexOf(`name: 'jerry',`) > -1).toBeTruthy();
  139. })
  140. test('$.find: find properties value should not throw error', () => {
  141. expect(()=>{
  142. const G = $(OBJECT_CODE);
  143. G.find(`name:$_$`);
  144. }).not.toThrow();
  145. })
  146. test('$.find: find properties value result should be ok', () => {
  147. const G = $(OBJECT_CODE);
  148. const code = G.find(`{ name: $_$ }`).generate();
  149. expect(code.indexOf(`jerry`) > -1).toBeTruthy();
  150. })
  151. test('$.find: simple tryout js code if condition result should be ok', () => {
  152. const code = $(jTryout).find('if ($_$){}')
  153. .each(ast => {
  154. ast.replace(`!Tryout.TRYOUT_SID_391`,`false`)
  155. .replace(`Tryout.TRYOUT_SID_391`,'true')
  156. })
  157. .root()
  158. .generate();
  159. const result = code.indexOf(`if (!Tryout.TRYOUT_SID_391`) < 0 && code.indexOf(`if (Tryout.TRYOUT_SID_391`) < 0
  160. expect(result).toBeTruthy();
  161. })
  162. test('$.find: simple js code if condition result should be ok', () => {
  163. const match = $(`if (a && 1 || 0) { b } else { c; dosth() }`)
  164. .find(`if ($_$1) { $_$2 } else { $_$3 }`)
  165. .match;
  166. const result = match['1'][0].value === 'a && 1 || 0' &&
  167. match['2'][0].value === 'b' &&
  168. match['3'][0].value === 'c'
  169. expect(result).toBeTruthy();
  170. })
  171. test('$.find: replace function name', () => {
  172. const G = $(`const code = Page({
  173. onShow() { },
  174. data: { }
  175. })`);
  176. const code = G.find(`Page({ $_$() { } })`)
  177. .each(item => {
  178. if (item.match[0][0].value == 'onShow') {
  179. item.match[0][0].node.name = 'render'
  180. }
  181. }).generate();
  182. const result = code.indexOf(`render()`) > -1 && code.indexOf(`onShow()`) < 0;
  183. expect(result).toBeTruthy();
  184. })
  185. test('$.find: find function name', () => {
  186. expect(()=>{
  187. const G = $(CODE);
  188. G.find(`function $_$(c)`);
  189. }).not.toThrow();
  190. })
  191. test('$.find: find function name result should be ok', () => {
  192. const G = $(CODE);
  193. //待定
  194. const code = G.find(`function $_$(c) { $_$2 }`).match[0][0].value;
  195. expect(code).toBe('test');
  196. })
  197. test('$.find: find arrow function name', () => {
  198. const G = $(jc1);
  199. const code = G.find(`()=>{}`).generate();
  200. expect(code.indexOf(`console.log('this is arrow function' )`) > -1).toBeTruthy();
  201. })
  202. test('$.find: find object', () => {
  203. expect(()=>{
  204. const G = $(OBJECT_CODE);
  205. G.find(`const a = $_$`);
  206. }).not.toThrow();
  207. })
  208. test('$.find: find object result should be ok', () => {
  209. const G = $(OBJECT_CODE);
  210. const code = G.find(`const a = $_$`).generate();
  211. expect(code.indexOf(`name: 'jerry',`) > -1).toBeTruthy();
  212. })
  213. test('$.find: find argument', () => {
  214. expect(()=>{
  215. const G = $(CODE);
  216. G.find(`test($_$)`);
  217. }).not.toThrow();
  218. })
  219. test('$.find: find argument result should be ok', () => {
  220. const G = $(CODE);
  221. const code = G.find(`test($_$)`).generate();
  222. expect(code).toBe('test(11)');
  223. })
  224. test('$.find: find argument and body', () => {
  225. expect(()=>{
  226. const G = $(CODE);
  227. G.find(`function test($_$){$_$}`);
  228. }).not.toThrow();
  229. })
  230. test('$.find: find argument and body should be ok', () => {
  231. const G = $(CODE);
  232. const code = G.find(`function test($_$){$_$}`).generate();
  233. expect(code.indexOf('function test(c){') > -1 &&
  234. code.indexOf('let a = 1;') > -1
  235. ).toBeTruthy()
  236. })
  237. test('$.find: find in function should be ok', () => {
  238. const G = $(CODE);
  239. const code = G.find(`function $_$() {
  240. let a = 1;
  241. }`).generate();
  242. expect(code.indexOf('function test(c){') > -1 &&
  243. code.indexOf('let a = 1;') > -1
  244. ).toBeTruthy()
  245. })
  246. test('$.find: find in function should be ok', () => {
  247. let has = false;
  248. const G = $(CODE);
  249. G.find(`function $_$() { }`)
  250. .each(item => {
  251. if(item.has('let a = 1;')){
  252. has = true;
  253. }
  254. })
  255. expect(has).toBeTruthy();
  256. })
  257. test('$.find: find function should be ok', () => {
  258. let has = false;
  259. const G = $(CODE);
  260. G.find(`$_$()`)
  261. .each(item => {
  262. has = true;
  263. })
  264. expect(has).toBeTruthy();
  265. })
  266. test('$.find: find function should be ok', () => {
  267. let has = false;
  268. const G = $(jc1);
  269. G.find(`parent().$_$()`)
  270. .each(item => {
  271. has = true;
  272. })
  273. expect(has).toBeTruthy();
  274. })
  275. test('$.find: find function should be ok', () => {
  276. let has = false;
  277. const G = $(CODE);
  278. G.find(`test()`)
  279. .each(item => {
  280. has = true;
  281. })
  282. expect(has).toBeTruthy();
  283. })
  284. test('$.find: find "for" code should be ok', () => {
  285. const G = $(jc2);
  286. const code = G.find(`for($_$1;$_$2;$_$3){$_$4}`).generate();
  287. expect(code.indexOf('for (let i = 0; i < array.length; i++) {') > -1 &&
  288. code.indexOf('const element = array[i];') > -1
  289. ).toBeTruthy()
  290. })
  291. test('$.find: find "for" code should be ok', () => {
  292. const G = $(jc2);
  293. const match = G.find(`for($_$1;$_$2;$_$3){$_$2}`).match;
  294. expect(match['1'].length === 1 && match['2'].length === 2 && match['3'].length === 1).toBeTruthy();
  295. })
  296. // test('$.find: find in "switch" code should be ok', () => {
  297. // const G = $(jc2);
  298. // const code = G.find(`switch ($_$) {$_$}`).generate();
  299. // expect(code.indexOf('switch (a)') > -1 &&
  300. // code.indexOf(' case 1:') > -1
  301. // ).toBeTruthy()
  302. // })
  303. // TODO
  304. test('$.find: find in "catch" code should be ok', () => {
  305. const G = $(jc2);
  306. const code = G.find(`try {$_$} catch (e) {$_$}`).generate();
  307. expect(code.indexOf('try') > -1 &&
  308. code.indexOf(' catch') > -1
  309. ).toBeTruthy()
  310. })
  311. // test('$.find: find in "catch" code should be ok', () => {
  312. // // 如何处理非完整try catch 语句??
  313. // const G = $(jc2);
  314. // const code = G.find(`try {$_$} `).generate();
  315. // expect(code.indexOf('try') > -1
  316. // ).toBeTruthy()
  317. // })
  318. // TODO
  319. // test('$.find: find in "catch" code should be ok', () => {
  320. // // 如何处理非完整try catch 语句??
  321. // const G = $(jc2);
  322. // const code = G.find(`catch (e) {$_$}`).generate();
  323. // expect(code.indexOf('try') > -1
  324. // ).toBeTruthy()
  325. // })
  326. test('$.find: find super in ts',()=>{
  327. const nodes = [];
  328. const code = $(tc1)
  329. .find(`super($_$,$_$,$_$)`, { ignoreSequence: true })
  330. .each(item => {
  331. item.after('console.log("super")')
  332. }).root().generate();
  333. $(code).find(`console.log("super")`).each(item => {
  334. nodes.push(item);
  335. })
  336. const result = nodes.length === 1 &&
  337. nodes[0].siblings().generate().indexOf(`super(options.baseWidth, options.baseHeight, options);`) > -1;
  338. expect(result).toBeTruthy();
  339. })
  340. test('$.find: find constructor in ts', () => {
  341. const nodes = [];
  342. const code = $(tc1)
  343. .find(`constructor($_$){}`)
  344. .append('body', 'console.log("constructor")')
  345. .root()
  346. .generate();
  347. $(code).find(`console.log("constructor")`).each(item => {
  348. nodes.push(item);
  349. })
  350. const result = nodes.length === 1
  351. expect(result).toBeTruthy();
  352. })
  353. test('$.find: find all',()=>{
  354. const nodes = [];
  355. $(jc1)
  356. .find('$_$')
  357. .each(item => {
  358. nodes.push(item);
  359. })
  360. expect(nodes.length > 0).toBeTruthy();
  361. })
  362. test('$.find: find h',()=>{
  363. const nodes = [];
  364. $(jc1)
  365. .find('h')
  366. .each(item => {
  367. nodes.push(item);
  368. })
  369. expect(nodes.length > 0).toBeTruthy();
  370. })
  371. test('$.find: 获取表达式中的变量', () => {
  372. const members = [];
  373. $(`(a.b.c && b) || (c && d)`)
  374. .find('$_$')
  375. .each(item => {
  376. if (item.parent().node.type == 'MemberExpression' && item.parent(1).node.type != 'MemberExpression') {
  377. // 输出a.b.c整体 而不是a \ b \ c
  378. members.push(item.parent().generate())
  379. } else if (item.parent().node.type != 'MemberExpression') {
  380. // 输出独立的变量
  381. members.push(item.generate())
  382. }
  383. })
  384. expect(members[0] === 'a.b.c' &&
  385. members[1] === 'b' &&
  386. members[2] === 'c' &&
  387. members[3] === 'd'
  388. ).toBeTruthy();
  389. })
  390. test('$.find: find string', () => {
  391. const nodes = [];
  392. $(jc1)
  393. .find(`'$_$'`)
  394. .each(item => {
  395. nodes.push(item);
  396. })
  397. expect(nodes[0].generate() === `'../index'`).toBeTruthy();
  398. })
  399. test('$.find: find string', () => {
  400. const nodes = [];
  401. $(jc1)
  402. .find(`'this is string'`)
  403. .each(item => {
  404. nodes.push(item);
  405. })
  406. expect(nodes[0].generate() === `'this is string'`).toBeTruthy();
  407. })
  408. test('$.find: find 获取赋值语句', () => {
  409. const matchList = [];
  410. $(jc1)
  411. .find(`$_$1 = $_$2`)
  412. .each(item => {
  413. matchList.push(item.match[1]);
  414. })
  415. expect(matchList[0][0].value === `a`).toBeTruthy();
  416. })
  417. test('$.find: find 获取赋值语句', () => {
  418. const matchList = [];
  419. $(jc1)
  420. .find('a = $_$')
  421. .each(item => {
  422. matchList.push(item.match[0])
  423. })
  424. expect(matchList[0][0].value).toBe(3);
  425. })
  426. test('$.find: find 获取赋值语句', () => {
  427. const matchList = [];
  428. $(jc1)
  429. .find('car.color = $_$')
  430. .each(item => {
  431. matchList.push(item.match[0])
  432. })
  433. expect(matchList[0][0].value).toBe('green');
  434. })
  435. test('$.find: find 获取赋值语句', () => {
  436. const matchList = [];
  437. $(jc1)
  438. .find('$_$1.color = $_$2')
  439. .each(item => {
  440. matchList.push(item.match['1'])
  441. })
  442. expect(matchList[0][0].value).toBe('car');
  443. })
  444. test('$.find: find 获取赋值语句 arr', () => {
  445. const matchList = [];
  446. $(jc1)
  447. .find('$_$ = [1, 2]')
  448. .each(item => {
  449. matchList.push(item.match[0])
  450. })
  451. expect(matchList[0][0].value).toBe('arr');
  452. })
  453. test('$.find: 在某作用域里面获取变量定义', () => {
  454. const matchList = [];
  455. const code = $(jc1)
  456. .find(`const obj = { name: 'test' };`)
  457. .parent().generate();
  458. const compareCode = $(
  459. `{
  460. function test(){
  461. let a = 1;
  462. }
  463. const obj = { name: 'test' };
  464. }`).generate();
  465. expect(code).toBe(compareCode);
  466. })
  467. test('$.find: class define use $_$', () => {
  468. const code = $(jc5)
  469. .find('class $_$ {}').generate();
  470. const result = code.indexOf('class Car {') > -1;
  471. expect(result).toBeTruthy();
  472. })
  473. test('$.find: class define', () => {
  474. const code = $(jc5)
  475. .find('class Car {}').generate();
  476. const result = code.indexOf('class Car {') > -1;
  477. expect(result).toBeTruthy();
  478. })
  479. test('$.find: class define', () => {
  480. const match = $(jc5)
  481. .find(`class Car {
  482. color = $_$c;
  483. size = $_$s;
  484. }`).match;
  485. const result = match['c'][0].value === 'red' && match['s'][0].value === 12;
  486. expect(result).toBeTruthy();
  487. })
  488. test('$.find: ts type define', () => {
  489. let find = false;
  490. $(jc6)
  491. .find('CheckBoxProps') // 找到的有可能是变量名,也有可能是类型定义
  492. .each(item => {
  493. if (item.parent().node.type == 'TSTypeReference') {
  494. find = true;
  495. }
  496. })
  497. expect(find).toBeTruthy();
  498. })
  499. test('$.find: ts type define', () => {
  500. const match = $(jc6)
  501. .find('let $_$1:CheckBoxProps = $_$2')
  502. .match;
  503. const result = match['1'][0].value === 'cbp' && match['2'][0].value === '{\n checked: true,\n width: 100\n}';
  504. expect(result).toBeTruthy();
  505. })
  506. test('$.find: ts type define in function', () => {
  507. const match = $(jc6)
  508. .find('($_$: CheckBoxProps) => {}')
  509. .match;
  510. const result = match[0][0].value === 'props' ;
  511. expect(result).toBeTruthy();
  512. })
  513. test('$.find: find import ', () => {
  514. const match = $(jc2)
  515. .find(`import $_$1 from '$_$2'`)
  516. .match;
  517. const result = match['1'][0].value === 'moment' ;
  518. expect(result).toBeTruthy();
  519. })
  520. test('$.find: find import ', () => {
  521. const match = $(jc1)
  522. .find(`export $_$ from '@path/sth'`)
  523. .match;
  524. const result = match[0][0].value === 'sth' ;
  525. expect(result).toBeTruthy();
  526. })
  527. test('$.find: render property', () => {
  528. const match = $(`const a = {
  529. render: function() {}
  530. }`)
  531. .find('render: function() {}')
  532. const result = !!match[0];
  533. expect(result).toBeTruthy();
  534. })
  535. test('$.find: find 模拟解构赋值 ', () => {
  536. // 找到const {a,b = {b1},c = 3} = d; 转为const a = d.a, b = d.b || {b1}, c = d.c || 3;
  537. const code = $(`const {a,b = {b1},c = 3} = d`)
  538. .find('const { $_$1 = $_$2 } = $_$3')
  539. .each(item => {
  540. const keyList = item.match[1].filter((item, i) => i % 2 == 0)
  541. const obj = item.match[3][0].value
  542. const newkeyList = keyList.map((key, i) => {
  543. let dec = `${key.value} = ${obj}.${key.value}`
  544. if (item.match[2][i].value != key.value) {
  545. dec += ('||' + item.match[2][i].value)
  546. }
  547. return dec
  548. })
  549. item.replaceBy(`const ${newkeyList.join(', ')}`)
  550. })
  551. .root()
  552. .generate()
  553. const result = code.indexOf('const a = d.a, b = d.b||{b1}, c = d.c||3') > -1;
  554. expect(result).toBeTruthy();
  555. })
  556. test('$.find: simple1 html code', () => {
  557. expect(() => {
  558. const G = $(hc1, config.html).find('<span>$_$</span>');
  559. }).not.toThrow();
  560. })
  561. test('$.find: simple1 html code input is object result should be ok', () => {
  562. const G = $(hc1, config.html);
  563. // 选择器构造 抛出异常报错
  564. const code = G.find({ nodeType: 'text', content: G.expando }).generate();
  565. //待定
  566. expect(code).toBe("\n");
  567. })
  568. test('$.find: simple1 html code input is object result should be ok', () => {
  569. const G = $(hc1, config.html);
  570. // 选择器构造 抛出异常报错
  571. const code = G.find({ nodeType: 'tag', content: G.expando }).generate();
  572. //待定
  573. expect(code.indexOf('<html>') > -1).toBeTruthy();
  574. })
  575. test('$.find: simple1 html code result should be ok', () => {
  576. const G = $(hc1, config.html).find('<span>');
  577. const code = G.generate();
  578. expect(code).toBe('<span>test</span>')
  579. })
  580. test('$.find: simple1 html code use $_$ result should be ok', () => {
  581. const G = $(hc1, config.html).find('<span>$_$</span>');
  582. const code = G.generate();
  583. expect(code).toBe('<span>test</span>')
  584. })
  585. test('$.find: simple1 html code use full tag result should be ok', () => {
  586. const G = $(hc1, config.html).find('<span>test</span>');
  587. const code = G.generate();
  588. expect(code).toBe('<span>test</span>')
  589. })
  590. test('$.find: simple1 html code find attr value', () => {
  591. const G = $(hc1, config.html).find('<div id=$_$>');
  592. const match = G.match;
  593. expect(match[0][0].value).toBe('1');
  594. })
  595. test('$.find: simple1 html code find attr key', () => {
  596. const G = $(hc1, config.html).find('<div $_$="1">');
  597. const match = G.match;
  598. expect(match[0][0].value).toBe('id');
  599. })
  600. test('$.find: simple1 html code find DOCTYPE ', () => {
  601. const G = $(hc1, config.html).find('<!DOCTYPE html>');
  602. const match = G.match;
  603. // 无$_$ 无match返回也合理
  604. // expect(match[0].value).toBe('');
  605. })
  606. test('$.find: script code result should be ok', () => {
  607. const G = $(hc1, config.html);
  608. const result = G.find('<script>$_$</script>');
  609. const match = result.match;
  610. expect(match[0][0].value).toBe(` var a = '1';`);
  611. })
  612. test('$.find: style code result should be ok', () => {
  613. const G = $(hc1, config.html);
  614. const result = G.find('<style>$_$</style>');
  615. const match = result.match;
  616. expect(match.indexOf('color: #000;') > -1).toBeTruthy();
  617. })
  618. test('$.find: comment code result should be ok', () => {
  619. const G = $(hc1, config.html);
  620. const result = G.find('<!-- $_$ -->');
  621. const match = result.match;
  622. expect(match).toBe(` comment test `);
  623. })
  624. test('$.find: replace html tag result should be ok', () => {
  625. const G = $(hc1, config.html);
  626. const code = G.find('<form $_$>$_$</form>').each((ast)=>{
  627. ast.node.content.name = 'mx-form'
  628. }).root().generate();
  629. expect(code.indexOf('<form') < 0 && code.indexOf('<mx-form') > -1).toBeTruthy();
  630. })
  631. test('$.find: find script tag content', () => {
  632. const G = $(hc2, config.html);
  633. let appScriptCount = 0;
  634. const AST = G.find('<script>$_$</script>').each(function (ast) {
  635. //找到两个script标签有内容,实际上应该只有一个
  636. if (ast.match) {
  637. appScriptCount++;
  638. }
  639. });
  640. expect(appScriptCount).toBe(1);
  641. })
  642. test('$.find: find objproperty', () => {
  643. const AST = $(
  644. `
  645. import Vue from 'vue';
  646. export default {
  647. name: 'cd',
  648. props: {
  649. msg: String,
  650. },
  651. directives: {
  652. highlight: {
  653. bind(el, binding, vnode) {
  654. el.style.background = binding.value;
  655. },
  656. inserted(el, binding) {
  657. el.parentNode.style.border = \`1px solid \${binding.value}\`;
  658. }
  659. },
  660. },
  661. };`
  662. ).find(`
  663. directives: {
  664. $_$1: {
  665. bind($_$2, $_$3, $_$4) {
  666. $$$1
  667. },
  668. },
  669. }
  670. `)
  671. expect(AST).toBeTruthy();
  672. })
  673. test('$.find: find comments 1', () => {
  674. const res = $(`// c3
  675. aaaa
  676. // c1
  677. var a = 1;
  678. // c2
  679. function x () {
  680. // c3
  681. // c3
  682. i++
  683. // c3
  684. // c4
  685. /* sdsd
  686. sdads
  687. */
  688. // c3
  689. }
  690. // c3`)
  691. .find('// $_$')
  692. .each(item => {
  693. item.value.value += '>>>>>'
  694. })
  695. .root()
  696. .generate()
  697. expect(res.match('>>>>')).toBeTruthy();
  698. })
  699. test('$.find: find comments 1', () => {
  700. const res = $(`
  701. // c1
  702. var a = 1;
  703. // c2
  704. function x () {
  705. i++
  706. // c3
  707. /* sdsd
  708. sdads
  709. */
  710. }`)
  711. .find('/* $_$ */')
  712. .each(item => {
  713. item.value.value += '>>>>>'
  714. })
  715. .find('// $_$')
  716. .each(item => {
  717. item.value.value += '<<<<<'
  718. })
  719. .root()
  720. .generate()
  721. expect(res.match('>>>>')).toBeTruthy();
  722. })
  723. test('$.find: html close tag', () => {
  724. const G = $(`<hhh>222
  725. <hhh>111</hhh>
  726. <hhh/>
  727. <hhh/>
  728. <hhh>333</hhh>
  729. </hhh>`, config.html);
  730. const result = G.find(['<hhh></hhh>', '<hhh/>']);
  731. expect(result.length == 5).toBeTruthy();
  732. })
  733. test('$.find: should not find vue template',()=>{
  734. const code = `
  735. <script>
  736. import Vue from "vue";
  737. export default {
  738. };
  739. </script>`;
  740. const has = $(code, config.vue)
  741. .find('<template></template>').length > 0;
  742. expect(has).not.toBeTruthy()
  743. })
  744. test('$.find: should not find vue script', () => {
  745. const code = `
  746. <template>
  747. <div class="context-menu-list">
  748. <slot></slot>
  749. </div>
  750. </template>`;
  751. const has = $(code, config.vue)
  752. .find('<script></script>').length > 0;
  753. expect(has).not.toBeTruthy()
  754. })
  755. test('$.find: should not find vue script', () => {
  756. const code = `
  757. <template>
  758. <div class="context-menu-list">
  759. <slot></slot>
  760. </div>
  761. </template>`;
  762. const res = $(code, config.vue)
  763. .find('<script></script>')
  764. .root()
  765. .generate();
  766. expect(res.match('template')).toBeTruthy()
  767. })
  768. test('$.find: find import', () => {
  769. const code = `
  770. import foo from './foo'
  771. import { MuxButton, MuxSelect } from '@alife/mux-components'
  772. import lodash, { add } from 'lodash'
  773. import Antd from 'antd'`;
  774. console.log(`import $$$1, {$$$2} from '$_$1'`)
  775. $(code)
  776. .find(`import $$$1, {$$$2} from '$_$1'`)
  777. .each(item => console.log(item.generate()));
  778. console.log(`import $$$1 from '$_$1'`)
  779. $(code)
  780. .find(`import $$$1 from '$_$1'`)
  781. .each(item => console.log(item.generate()));
  782. console.log(`import { $$$1 } from '$_$1'`)
  783. $(code)
  784. .find(`import { $$$1 } from '$_$1'`)
  785. .each(item => console.log(item.generate()));
  786. expect(true).toBeTruthy()
  787. })
  788. test('$.find: find import', () => {
  789. const res = $(`
  790. $t('s')
  791. $t('sss')
  792. 's'`)
  793. .replace('$t($_$)', (match => {
  794. if (match[0][0].value == 's') {
  795. return `global.$t($_$)`
  796. } else return null
  797. }))
  798. .root()
  799. .generate()
  800. expect(res.match(`global`)).toBeTruthy()
  801. })
  802. test('$.find: find script setup', () => {
  803. const res = $(`
  804. <script setup>
  805. const a = inject('global')
  806. </script>
  807. <template>
  808. </template>`, { parseOptions: { language: 'vue' }})
  809. .find('<script setup></script>')
  810. .find(`const $_$1 = inject('global')`)
  811. .generate()
  812. expect(res.match('global')).toBeTruthy()
  813. })
  814. test('$.find: find script setup', () => {
  815. const res = $(`
  816. <script setup>
  817. </script>
  818. <template>
  819. <div a="1" b="s">
  820. <div c="1222" b="s">
  821. </div>
  822. </div>
  823. </template>`, { parseOptions: { language: 'vue' }})
  824. .find('<template></template>')
  825. .find(`<$_$1 $$$></$_$1>`)
  826. .each(item => {
  827. item.match['$$$$'].forEach(attr => {
  828. if (attr.value.content == 's') {
  829. attr.value.content = 'handle("s")'
  830. }
  831. })
  832. })
  833. .generate()
  834. expect(res.match('handle')).toBeTruthy()
  835. })
  836. test('$.find: match typeAnnotation', () => {
  837. const res = $(`
  838. export default async (
  839. data: Request<{ msg: string }>
  840. ): Promise<string> => {
  841. return data.msg
  842. }
  843. `)
  844. .find(`export default async ($$$1): $_$2 => { $$$3 }`)
  845. .match[2]
  846. expect(res[0].type == 'TSTypeReference').toBeTruthy()
  847. })
  848. test('$.find: match typeAnnotation', () => {
  849. const res = $(`
  850. export default async (
  851. data: Request<{ msg: string }>
  852. ): Promise<string> => {
  853. return data.msg
  854. }
  855. `)
  856. .find(`export default async ($$$1): $_$1<$_$2> => { $$$3 }`)
  857. .match
  858. expect(res[1][0] && res[2][0]).toBeTruthy()
  859. })
  860. test('$.find: match Vue.createApp', () => {
  861. const find = $(`
  862. import App from './App';
  863. window.$vueApp.use();
  864. const app = window.$vueApp = Vue.createApp(App).mount('#app');
  865. window.$vueApp.config.globalProperties.routerAppend = (path, pathToAppend) => {
  866. return path + (path.endsWith('/') ? '' : '/') + pathToAppend; };
  867. `)
  868. .find(`Vue.createApp(App).mount('#app')`)
  869. expect(find.length > 0).toBeTruthy()
  870. })
  871. test('$.find: match typeAnnotation', () => {
  872. const res = $(`const c = () => x;
  873. const d = function () {
  874. console.log('ddd');
  875. }`)
  876. .find(`() => $_$`)
  877. expect(res.match[0][0].value == 'x').toBeTruthy()
  878. })
  879. test('$.find: replace html tag result should be ok', () => {
  880. const res = $(`
  881. var e;
  882. var a = 1, b=2, d, c=3;
  883. function xxx(){
  884. return a = 5, b=6, c=7, 8;
  885. }
  886. if(a=5,b=6,c=7,d){
  887. }
  888. `)
  889. .find(`var $_$1 = $_$2`)
  890. .each(item => {
  891. item.match
  892. })
  893. expect(!!res).toBeTruthy();
  894. })
  895. test('$.find: match string', () => {
  896. let res = '';
  897. $('`/aa/bb/${cc}/cccccc/${s}`')
  898. .find('`$_$1\${$_$2}`')
  899. .each(item => {
  900. res = item.match[1].length + item.match[2].length
  901. })
  902. expect(res == 5).toBeTruthy()
  903. })
  904. test('$.find: $$$ match ObjectMethod', () => {
  905. let res =
  906. $(`
  907. Component({
  908. mixins: [],
  909. methods: {
  910. b() {
  911. }
  912. }
  913. });
  914. `)
  915. .find(`Component({
  916. $$$1,
  917. methods: { $$$2 }
  918. })`)
  919. expect(res.length == 1).toBeTruthy()
  920. })
  921. test('$.find: match string', () => {
  922. let res = $(`function a () {
  923. function b () {
  924. }
  925. }
  926. function c() {
  927. }`)
  928. .find('function $_$() {}')
  929. expect(res.length == 3).toBeTruthy()
  930. })
  931. test('$.find: match string', () => {
  932. let res = $(`{function a () {
  933. function b () {
  934. }
  935. }
  936. function c() {
  937. }}`)
  938. .find('function $_$() {}', { deep: 'n' })
  939. expect(res.length == 2).toBeTruthy()
  940. })
  941. test('$.find: match string', () => {
  942. let res = $(`<div>
  943. <p v-if=""></p>
  944. </div>`, { parseOptions: { language: 'html'}})
  945. .find(`
  946. <p></p>`)
  947. expect(res.length == 1).toBeTruthy()
  948. })
  949. test('$.find: match string', () => {
  950. let res = $(`<script attr="5" data="10" ></script> `, { parseOptions: { language: 'html'}})
  951. .find(`<script attr="$_$1" $$$1></script>`)
  952. expect(res.match[1] && res.match['$$$1']).toBeTruthy()
  953. })
  954. // test('$.find: match string', () => {
  955. // let res = $(`const car1 = {
  956. // size: 'small',
  957. // color: 'red',
  958. // init(a, b) {
  959. // }
  960. // } `)
  961. // .find(`{
  962. // init() {}
  963. // }`)
  964. // expect(res.length == 1).toBeTruthy()
  965. // })
  966. test('$.find: match string', () => {
  967. let res = $(`const car1 = {
  968. size: 'small',
  969. color: 'red',
  970. init(a, b) {
  971. }
  972. } `)
  973. .find(`{
  974. color: 'red'
  975. }`)
  976. expect(res.length == 1).toBeTruthy()
  977. })
  978. test('$.find: match string', () => {
  979. let res = $(`import 'foo.css';
  980. import { a } from 'bar';
  981. import b from 'baz';
  982. import c, { d } from 'far';`)
  983. .find(`import { $$$ } from '$_$';`)
  984. expect(res.length == 2).toBeTruthy()
  985. })
  986. test('$.find: match string', () => {
  987. let res = $(`import 'foo.css';`)
  988. .find(`import { $$$ } from '$_$';`)
  989. expect(res.length == 0).toBeTruthy()
  990. })