版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

956 rivejä
29 KiB

  1. const $ = require('../index');
  2. const config = require('./config');
  3. const jc1 = require('./code/simple1');
  4. const jc2 = require('./code/simple2');
  5. const jc3 = require('./code/simple3');
  6. const jc4 = require('./code/simple4');
  7. const jTryout = require('./code/simple-tryout');
  8. const jReact = require('./code/react');
  9. const hc1 = require('./code/simple1.html');
  10. const CODE = `
  11. const m = {
  12. a: function() {
  13. console.log('test a');
  14. },
  15. b: function() {
  16. console.log('test b');
  17. }
  18. }
  19. m.a();
  20. `;
  21. const COMPARE_CODE = `
  22. const m = {
  23. a: function() {
  24. console.log('test a');
  25. },
  26. b: function() {
  27. console.log('test b');
  28. }
  29. }
  30. m.b();
  31. `;
  32. test('$.replace: simple code', () => {
  33. expect(() => {
  34. const G = $(CODE);
  35. G.replace('$_$.a()','$_$.b()')
  36. }).not.toThrow();
  37. })
  38. test('$.replace: simple code', () => {
  39. const G = $(CODE);
  40. G.replace('$_$.a()','$_$.b()');
  41. const code = G.generate();
  42. expect(code).toBe(COMPARE_CODE);
  43. })
  44. test('$.replace: this[0] is null', () => {
  45. expect(()=>{
  46. const G = $('var a = 1;');
  47. G[0] = null
  48. G.replace();
  49. }).not.toThrow();
  50. })
  51. test('$.replace: null', () => {
  52. expect(() => {
  53. const G = $(CODE);
  54. G.replace('$_$.a()',null)
  55. }).not.toThrow();
  56. })
  57. test('$.replace: no $_$', () => {
  58. expect(() => {
  59. const G = $(CODE);
  60. G.replace('$_$.a()','m.b()');
  61. }).not.toThrow();
  62. })
  63. test('$.replace: not find', () => {
  64. expect(() => {
  65. const G = $(CODE);
  66. G.replace('$_$.c()','m.b()');
  67. }).not.toThrow();
  68. })
  69. // TODO
  70. // test('$.replace: replace head String should be ok', () => {
  71. // const code = `'aaaa';`;
  72. // const output = $(code).replace(`'aaaa'`, `'bbbb'`).generate();
  73. // expect(output).toBe(`"bbbb"`);
  74. // })
  75. test('$.replace: simple1 js code result should be ok', () => {
  76. const G = $(jc1);
  77. const result = G.replace('const c = add();', 'const b = add();');
  78. const code = result.generate();
  79. expect(code.indexOf('const c = add()') < 0 && code.indexOf('const b = add()') > -1).toBeTruthy();
  80. })
  81. test('$.replace: simple2 js code result should be ok', () => {
  82. const G = $(jc2);
  83. const result = G.replace('this.updater.digest(loc);', 'this.updater.set(loc);');
  84. const code = result.generate();
  85. expect(code.indexOf('this.updater.digest(loc)') < 0 && code.indexOf('this.updater.set(loc)') > -1).toBeTruthy();
  86. })
  87. test('$.replace: simple2 js code, replace with multiple $_$ show throw error', () => {
  88. expect(() => {
  89. // 是否允许多个$_$ 出现?
  90. const G = $(jc2);
  91. const result = G.replace('$_$.updater.digest($_$);', '$_$.setData($_$);');
  92. const code = result.generate();
  93. }).not.toThrow();
  94. })
  95. test('$.replace: simple3 js code result should be ok', () =>{
  96. const code = $(jc3)
  97. .replace(`await $_$()`, `
  98. try {
  99. await $_$()
  100. } catch(e) {
  101. handleError()
  102. }`)
  103. .replace(`my.alert({
  104. title: $_$1,
  105. content: $_$2,
  106. success: $_$3
  107. })`, `this.alert($_$1, $_$2, $_$3)`)
  108. .generate();
  109. const result = code.indexOf('handleError()') > -1 && code.indexOf('this.alert(') > -1
  110. expect(result).toBeTruthy();
  111. })
  112. test('$.replace: simple3 js code with $$$ result should be ok', () =>{
  113. const code = $(jc3)
  114. .replace(`
  115. Page({
  116. onShow($_$1) {
  117. $$$1
  118. },
  119. $$$2
  120. })
  121. `,
  122. `
  123. View.extend({
  124. init() {
  125. this.onInit();
  126. this.didMount();
  127. },
  128. render($_$1) {
  129. var a = 1
  130. $$$1
  131. },
  132. $$$2
  133. })
  134. `)
  135. .generate();
  136. const result = code.indexOf('View.extend(') > -1;
  137. expect(result).toBeTruthy();
  138. })
  139. test('$.replace: simple tryout js code replace width array should be ok', () =>{
  140. const code = $(jTryout)
  141. .replace([
  142. 'var $_$ = Tryout.TRYOUT_SID_391',
  143. 'let $_$ = Tryout.TRYOUT_SID_391',
  144. 'const $_$ = Tryout.TRYOUT_SID_391'
  145. ], 'let $_$ = true;')
  146. .generate();
  147. const result = code.indexOf('let t1 = true') > -1 && code.indexOf('let t2 = true') > -1 && code.indexOf('let t3 = true') > -1
  148. expect(result).toBeTruthy();
  149. })
  150. test('$.replace: simple tryout js code result should be ok', () => {
  151. const code = $(jTryout)
  152. .replace(`Tryout.TRYOUT_SID_391 ? $_$1 : $_$2`, '$_$1')
  153. .generate();
  154. const result = code.indexOf('const t4 = 1') > -1
  155. expect(result).toBeTruthy();
  156. })
  157. test('$.replace: simple tryout js code result should be ok', () => {
  158. const code = $(jTryout)
  159. .replace(`!Tryout.TRYOUT_SID_391 ? $_$1 : $_$2`, '$_$2')
  160. .generate();
  161. const result = code.indexOf(`const t7 = "2"`) > -1 && code.indexOf(`const t8 = "2"`) > -1
  162. expect(result).toBeTruthy();
  163. })
  164. test('$.replace: simple js code result should be ok', () => {
  165. const CODE = `alert({
  166. type: 'error',
  167. content: '请填写必填项',
  168. done: () => {}
  169. })`;
  170. const code = $(CODE)
  171. .replace(`alert({ type: $_$1, done: $_$3, content: $_$2})`,
  172. `alert( $_$1, $_$2, $_$3 )`)
  173. .generate();
  174. const result = code.indexOf(`alert( 'error', '请填写必填项', () => {} )`) > -1 ;
  175. expect(result).toBeTruthy();
  176. })
  177. test('$.replace: replace use $$$ result should be ok', () => {
  178. const CODE = `alert({
  179. type: 'error',
  180. content: '请填写必填项',
  181. done: () => {}
  182. })`;
  183. const code = $(CODE)
  184. .replace(`alert($$$)`, `alert(this, $$$)`)
  185. .generate();
  186. const result = code.indexOf(`alert(this,`) > -1 ;
  187. expect(result).toBeTruthy();
  188. })
  189. test('$.replace: replace use $$$ result should be ok', () => {
  190. const CODE = `const code = Page({
  191. onShow() { },
  192. data: { }
  193. })`;
  194. const code = $(CODE)
  195. .replace(`Page({
  196. onShow() {
  197. $$$1
  198. },
  199. $$$2
  200. })`, `Page({
  201. render() {
  202. $$$1
  203. },
  204. $$$2
  205. })`)
  206. .generate();
  207. const result = code.indexOf(`render()`) > -1 && code.indexOf(`onShow()`) < 0;
  208. expect(result).toBeTruthy();
  209. })
  210. test('$.replace: simple tryout js code if condition result should be ok', () => {
  211. const code = $(jTryout)
  212. .replace(`if(Tryout.TRYOUT_SID_391){$_$}`, '$_$')
  213. .generate();
  214. const result = code.indexOf(`if (Tryout.TRYOUT_SID_391)`) < 0
  215. expect(result).toBeTruthy();
  216. })
  217. test('$.replace: replace use callback function result should be ok', () => {
  218. const map = { input: 'textarea' } // 在map中有映射的才进行修改
  219. const code = $(`
  220. const componentList = [{
  221. index: 1,
  222. component: 'input'
  223. }, {
  224. index: 2,
  225. component: 'radio'
  226. }, {
  227. index: 3,
  228. component: 'checkbox'
  229. }]`)
  230. .replace('component: $_$', (match => {
  231. if (map[match[0][0].value]) {
  232. return `component: '${map[match[0][0].value]}'`
  233. } else {
  234. return 'component: $_$'
  235. }
  236. }))
  237. .generate();
  238. const result = code.indexOf(`component: 'input'`) < 0 && code.indexOf(`component: 'textarea'`) > -1;
  239. expect(result).toBeTruthy();
  240. })
  241. test('$.replace: use replace to insert code result should be ok', () => {
  242. const code = $(`
  243. Page({
  244. onShow() { },
  245. data: { }
  246. })`)
  247. .replace(`Page({
  248. $$$2
  249. })`, `Page({
  250. init() {
  251. this.data = {}
  252. },
  253. $$$2
  254. })`)
  255. .generate();
  256. const result = code.indexOf(`init()`) > -1;
  257. expect(result).toBeTruthy();
  258. })
  259. test('$.replace: replace import result should be ok', () => {
  260. const code = $(jc2)
  261. .replace(`import $_$ from 'moment';`, `import $_$ from 'gogocode';`)
  262. .generate();
  263. const result = code.indexOf(`'gogocode'`) > -1;
  264. expect(result).toBeTruthy();
  265. })
  266. test('$.replace: replace import result should be ok', () => {
  267. const code = $(jc2)
  268. .replace(`import { useContext, $$$ } from '@as/mdw-hk'`, `import { useFContext, $$$ } from '@as/mdw-hk'`)
  269. .generate();
  270. const result = code.indexOf(`{ useFContext, rest }`) > -1;
  271. expect(result).toBeTruthy();
  272. })
  273. test('$.replace: replace with multiple $_$', () => {
  274. const code = `this.$emit('input', e.target.value)`
  275. const compareCode = $(code)
  276. .replace(`$_$1.$emit('input',$$$1)`, `$_$1.$emit('update:modelValue', $$$1)`)
  277. .generate();
  278. expect(compareCode).toBe(`this.$emit('update:modelValue', e.target.value)`);
  279. })
  280. test('$.replace: replace with $_$ and $$$', () => {
  281. const code = `this.$emit('input', e.target.value)`
  282. const compareCode = $(code)
  283. .replace(`$_$1.$emit('input',$$$)`, `$_$1.$emit('update:modelValue',$$$)`)
  284. .generate();
  285. expect(compareCode).toBe(`this.$emit('update:modelValue',e.target.value)`);
  286. })
  287. test('$.replace: react js result should be ok', () => {
  288. let code = $(jReact.code)
  289. .replace(`import { $$$ } from "@alifd/next"`, `import { $$$ } from "antd"`)
  290. .replace(`<h2>转译前</h2>`, `<h2>转译后</h2>`)
  291. .replace(
  292. `<Button type="normal" $$$></Button>`,
  293. `<Button type="default" $$$></Button>`
  294. )
  295. .replace(
  296. `<Button size="medium" $$$></Button>`,
  297. `<Button size="middle" $$$></Button>`
  298. )
  299. .replace(`<Button text $$$></Button>`, `<Button type="link" $$$></Button>`)
  300. .replace(`<Button warning $$$></Button>`, `<Button danger $$$></Button>`)
  301. .generate();
  302. expect(code).toBe(jReact.compareCode);
  303. })
  304. test('$.replace: replace attr result should be ok', () => {
  305. let code = $(jc4)
  306. .replace(
  307. '{ text: $_$1, value: $_$2, $$$ }',
  308. '{ name: $_$1, id: $_$2, $$$ }'
  309. )
  310. .generate();
  311. const result = code.indexOf('text:') < 0 && code.indexOf('name:') > -1 &&
  312. code.indexOf('value:') < 0 && code.indexOf('id:') > -1 &&
  313. code.indexOf('tips:') > -1
  314. expect(result).toBeTruthy();
  315. })
  316. test('$.replace: replace like remove result should be ok', () => {
  317. let code = $(jc1)
  318. .replace(
  319. `const XXMonitor = require($_$);`,
  320. ''
  321. )
  322. .generate();
  323. const result = code.indexOf(`const XXMonitor = require('../monitor');`) < 0
  324. expect(result).toBeTruthy();
  325. })
  326. // test('$.replace: simple tryout js code if condition with $$$ result should be ok', () => {
  327. // // 貌似这种情况不太可能支持
  328. // const code = $(jTryout)
  329. // .replace(`if(Tryout.TRYOUT_SID_391 $$$){$_$}`, 'if($$$){$_$}')
  330. // .root()
  331. // .generate();
  332. // const result = code.indexOf(`if (Tryout.TRYOUT_SID_391`) < 0
  333. // expect(result).toBeTruthy();
  334. // })
  335. // test('$.replace: simple tryout js code if condition with $$$ result should be ok', () => {
  336. // // 貌似这种情况不太可能支持
  337. // const code = $(jTryout)
  338. // .replace(`if(!Tryout.TRYOUT_SID_391 $$$){$_$}`, 'if(false $$$){$_$}')
  339. // .root()
  340. // .generate();
  341. // const result = code.indexOf(`if (!Tryout.TRYOUT_SID_391`) < 0
  342. // expect(result).toBeTruthy();
  343. // })
  344. test('$.replace: replace with $_$', () => {
  345. const G = $(jc2);
  346. const result = G.replace(`import $_$ from 'moment';`, `import $_$ from 'new_moment';`);
  347. const code = result.generate();
  348. expect(code.indexOf(`import moment from 'moment';`) < 0 &&
  349. code.indexOf(`import moment from 'new_moment';`) > -1).toBeTruthy();
  350. })
  351. test('$.replace: simple1 html code', () => {
  352. expect(() => {
  353. const G = $(hc1, config.html);
  354. G.replace('<span>test</span>','<span>replace</span>');
  355. }).not.toThrow();
  356. })
  357. test('$.replace: simple1 html code, replacer is empty should throw error', () => {
  358. expect(() => {
  359. const G = $(hc1, config.html);
  360. G.replace('<span>test</span>');
  361. }).not.toThrow();
  362. })
  363. test('$.replace: simple1 html code, replacer is ast node should not throw error', () => {
  364. expect(() => {
  365. const G = $(hc1, config.html);
  366. G.replace('<span>test</span>',$('<div>test</div>', config.html).node);
  367. }).not.toThrow();
  368. })
  369. test('$.replace: simple1 html code, replacer is ast node result should be ok', () => {
  370. const G = $(hc1, config.html);
  371. const replacer = $('<div>test</div>', config.html).node;
  372. const code = G.replace('<span>test</span>', replacer).generate();
  373. expect(code.indexOf('<span>test</span>') < 0).toBeTruthy();
  374. })
  375. test('$.replace: simple1 html code result should be ok', () => {
  376. const G = $(hc1, config.html);
  377. G.replace('<span>test</span>','<span>replace</span>');
  378. const code = G.generate();
  379. expect(code.indexOf('<span>test</span>') < 0 && code.indexOf('<span>replace</span>') > -1).toBeTruthy();
  380. })
  381. test('$.replace: simple1 html code use $_$ result should be ok', () => {
  382. const G = $(hc1, config.html);
  383. G.replace('<span>$_$</span>','<i>$_$</i>');
  384. const code = G.generate();
  385. expect(code.indexOf('<i>test</i>') > -1).toBeTruthy();
  386. })
  387. // test('$.replace: replace html tag result should be ok', () => {
  388. // const G = $(hc1, config.html);
  389. // const code = G.replace('<form $$$1>$$$2</form>','<mx-form $$$1>$$$2</mx-form>').generate();
  390. // expect(code.indexOf('<form') < 0 && code.indexOf('<mx-form') > -1).toBeTruthy();
  391. // })
  392. // TODO
  393. // test('$.replace: simple1 html code use $_$ result should be ok', () => {
  394. // const G = $(hc1, config.html);
  395. // const code = G.replace('<form id=$_$1 $$$>$_$2</form>','<div formId=$_$1 $$$>$_$2</div>').root().generate();
  396. // expect(code.indexOf(`<div formId="myform" class="mt10" style="margin-top:10px;"`) > -1).toBeTruthy();
  397. // })
  398. test('$.replace: jsx tag replace should be ok', () => {
  399. const G = $(`<a v="1"></a>`);
  400. const res = G.replace('<$_$1 $$$1>$$$2</$_$1>', '<$_$1 $$$1>$$$2 ssssss</$_$1>').generate()
  401. expect(res.indexOf('ssssss') > -1).toBeTruthy();
  402. })
  403. test('$.replace: class replace should be ok', () => {
  404. const res = $(`import { MM } from "@mm"
  405. import { loaders } from 'p.js'
  406. export class Sc extends MMG {
  407. constructor(options) {
  408. super(options);
  409. }
  410. onInited() {
  411. }
  412. /**
  413. * 初始化加载器
  414. */
  415. initLoader() {
  416. const textureLoader = new loaders.Loader();
  417. textureLoader.load();
  418. }
  419. }
  420. `).replace(`initLoader(){$$$}`, `abc() {
  421. $$$
  422. }`).generate()
  423. expect(res.indexOf('abc') > -1).toBeTruthy();
  424. })
  425. test('$.replace: class replace should be ok', () => {
  426. const res = $('<div></div>', {parseOptions: { html: true}}).replace(`<div $$$1>$$$2</div>`,
  427. `<div mx-view="xxx" $$$1> $$$2</div>`).generate()
  428. expect(res.indexOf('xxx') > -1).toBeTruthy();
  429. })
  430. test('$.replace: class replace should be ok', () => {
  431. // const res = $(`
  432. // getApp().globalData.eventEmmit.emit("openTasks",{} );
  433. // `, {parseOptions: { html: true}}).replace(`<div $$$1>$$$2</div>`,
  434. // `<div mx-view="xxx" $$$1> $$$2</div>`).generate()
  435. // expect(res.indexOf('xxx') > -1).toBeTruthy();
  436. })
  437. test('$.replace: class replace should be ok', () => {
  438. const res = $(`
  439. const a = {
  440. aaa: 1,
  441. bbb: 2,
  442. }
  443. `).replace('aaa: 1', '')
  444. .generate()
  445. expect(res.indexOf(',') == -1).toBeTruthy();
  446. })
  447. test('$.replace: class replace should be ok', () => {
  448. const res = $(`
  449. import a from 'a';
  450. console.log('get A');
  451. var b = console.log();
  452. console.log.bind();
  453. var c = console.log;
  454. console.log = func;
  455. `)
  456. .replace(`var $_$ = console.log()`, `var $_$ = void 0`)
  457. .replace(`var $_$ = console.log`, `var $_$ = function(){};`)
  458. .find(`console.log()`)
  459. .remove()
  460. .generate();
  461. expect(res.match(/;/g).length == 4).toBeTruthy();
  462. })
  463. test('$.replace: class replace should be ok', () => {
  464. const res = $(`
  465. import a from 'a';console.log('get A');var b = console.log();console.log.bind();var c = console.log;console.log = func;
  466. `)
  467. .replace(`var $_$ = console.log()`, `var $_$ = void 0;`)
  468. .replace(`var $_$ = console.log`, `var $_$ = function(){};`)
  469. .find(`console.log()`)
  470. .remove()
  471. .generate();
  472. expect(res.indexOf(`import a from 'a';var b = void 0;console.log.bind();var c = function(){};console.log = func;`) > -1).toBeTruthy();
  473. })
  474. test('$.replace: class replace should be ok', () => {
  475. const res = $(`
  476. import * as ctx from '@ali/midway-hooks';
  477. import { useContext } from '@ali/midway-hooks';
  478. import ctx2 from '@ali/midway-hooks'
  479. import a, { b, c } from '@ali/midway-hooks'
  480. `)
  481. .replace(`import $_$ from "@ali/midway-hooks"`, `import $_$ from "@al/hooks"`)
  482. .replace(`import * as $_$ from "@ali/midway-hooks"`, `import * as $_$ from "@al/hooks"`)
  483. .replace(`import { $$$ } from "@ali/midway-hooks"`, `import { $$$ } from "@al/hooks"`)
  484. .generate();
  485. // TODO
  486. expect(res.match(/\@al\/hooks/g).length == 4).toBeTruthy();
  487. })
  488. test('$.replace: class replace should be ok', () => {
  489. const res = $(`
  490. import * as ctx from '@ali/midway-hooks';
  491. import { useContext } from '@ali/midway-hooks';
  492. import ctx2 from '@ali/midway-hooks'
  493. `)
  494. .replace(`import $_$ from "@ali/midway-hooks"`, `import $_$ from "@al/hooks"`)
  495. .replace(`import * as $_$ from "@ali/midway-hooks"`, `import * as $_$ from "@al/hooks"`)
  496. .replace(`import { $$$ } from "@ali/midway-hooks"`, `import { $$$ } from "@al/hooks"`)
  497. .generate();
  498. expect(res.match(/\@al\/hooks/g).length == 3).toBeTruthy();
  499. })
  500. test('$.replace: class replace should be ok', () => {
  501. const res = $(`
  502. import * as ctx from '@ali/midway-hooks';
  503. import { useContext } from '@ali/midway-hooks';
  504. import ctx2 from '@ali/midway-hooks'
  505. `)
  506. .replace(`import $_$ from "@ali/midway-hooks"`, `import $_$ from "@al/hooks"`)
  507. .replace(`import * as $_$ from "@ali/midway-hooks"`, `import * as $_$ from "@al/hooks"`)
  508. .generate();
  509. expect(res.match(/\@al\/hooks/g).length == 2).toBeTruthy();
  510. })
  511. test('$.replace: class replace should be ok', () => {
  512. const res = $(`
  513. import * as ctx from '@ali/midway-hooks';
  514. import { useContext } from '@ali/midway-hooks';
  515. import ctx2 from '@ali/midway-hooks'
  516. `)
  517. .replace(`import $_$ from "@ali/midway-hooks"`, `import $_$ from "@al/hooks"`)
  518. .generate();
  519. expect(res.match(/\@al\/hooks/g).length == 1).toBeTruthy();
  520. })
  521. test('$.replace: class replace should be ok', () => {
  522. const res = $(`const a = b?.s?.c?.e`)
  523. .replace(`$_$1?.$_$2`, `$_$1 ? $_$1.$_$2 : null`)
  524. .generate();
  525. expect(res.indexOf('const a = b?.s?.c ? b?.s?.c.e : null') > -1).toBeTruthy();
  526. })
  527. test('$.replace: class replace should be ok', () => {
  528. const res = $(`
  529. "use strict";
  530. module.exports = app => {
  531. const mongoose = app.mongoose;
  532. const BrandSchema = new Schema({
  533. name: { type: String }
  534. });
  535. const Brand = mongoose.model('Brand');
  536. return Brand;
  537. };
  538. `)
  539. .replace(`module.exports = () => $_$`, `$_$`)
  540. .replace(`return $_$`, `module.exports = $_$`)
  541. .before(`const app = require('mongoose')`)
  542. .generate();
  543. expect(res.indexOf('module.exports = Brand') > -1 && res.indexOf('module.exports = app') == -1).toBeTruthy();
  544. })
  545. test('$.replace: replace arguments should not throw error', () => {
  546. const code = `
  547. this.$emit("input", date, {
  548. type: "CUSTOM",
  549. value: date
  550. });
  551. `;
  552. expect(() => {
  553. const res = $(code)
  554. .replace(`$_$1.$emit('input',$$$)`, `$_$1.$emit('update:modelValue',$$$)`)
  555. }).not.toThrow();
  556. })
  557. test('$.replace: replace arguments should not throw error', () => {
  558. const code = `(v) => Boolean(v.text)`;
  559. const res = $(code)
  560. .replace('() => import($_$)', 'Vue.defineAsyncComponent(() => import($_$))').generate()
  561. expect(res.match(/Boolean\(v\.text\)/)).toBeTruthy();
  562. })
  563. test('$.replace: replace arguments should not throw error', () => {
  564. const code = `
  565. Vue.directive('a', 'b', {
  566. bind(a) {
  567. }
  568. })`;
  569. expect(() => {
  570. $(code)
  571. .replace(
  572. `
  573. Vue.directive($$$1, {
  574. bind($_$2) {
  575. $$$2
  576. },
  577. $$$3
  578. },
  579. )`,
  580. `
  581. Vue.directive($$$1, {
  582. beforeMount($_$2) {
  583. $$$2
  584. },
  585. $$$3
  586. },
  587. )
  588. `
  589. )
  590. }).not.toThrow();
  591. })
  592. test('$.replace: replace arguments should not throw error', () => {
  593. const code = `Vue.set(state.items, item.id, item)`;
  594. const res = $(code)
  595. .replace(`Vue.set($_$1,$_$2,$_$3)`, `$_$1[$_$2] = $_$3`).generate()
  596. expect(res == 'state.items[item.id] = item;').toBeTruthy();
  597. })
  598. test('$.replace: replace object', () => {
  599. const code = `
  600. VueRouter.createRouter({
  601. routes,
  602. history: VueRouter.createWebHistory(),
  603. base: '/root'
  604. })`;
  605. const res = $(code)
  606. .replace(`{history: VueRouter.createWebHistory(), base: $_$,$$$}`,
  607. `{history: VueRouter.createWebHistory($_$),$$$}`)
  608. .root()
  609. .generate()
  610. expect(res.indexOf('({history') > -1).toBeTruthy();
  611. })
  612. test('$.replace: html tag replace should be ok', () => {
  613. const G = $(`<a v="1"></a>`, { parseOptons: { language: 'html' }});
  614. const res = G.replace('<$_$1 $$$1>$$$2</$_$1>', '<$_$1 $$$1>$$$2 ssssss</$_$1>').generate()
  615. expect(res.indexOf('ssssss') > -1).toBeTruthy();
  616. })
  617. test('$.replace: html attr replace should be ok', () => {
  618. const G = $(`<a v="1"></a>`, { parseOptions: { language: 'html' }});
  619. const res = G.replace('<$_$1 v="$_$2" $$$1>$$$2</$_$1>', '<$_$1 v="$_$2" v2="$_$2" $$$1>$$$2 ssssss</$_$1>').generate()
  620. expect(res.indexOf('v2="1"') > -1).toBeTruthy();
  621. })
  622. test('$.replace: html attr replace should be ok', () => {
  623. const G = $(`<a v="1"></a>`);
  624. const res = G.replace('<$_$1 v="$_$2" $$$1>$$$2</$_$1>', '<$_$1 v="$_$2" v2="$_$2" $$$1>$$$2 ssssss</$_$1>').generate()
  625. expect(res.indexOf('v2="1"') > -1).toBeTruthy();
  626. })
  627. test('$.replace: this replace should be ok', () => {
  628. let res = $(`
  629. this.ddd = 2
  630. sss.ddd = 3
  631. xxx.ddd = 4
  632. `)
  633. .replace('this.$_$', 'ggg.$_$')
  634. .generate();
  635. expect(res.match(/ggg/g).length == 1).toBeTruthy();
  636. })
  637. test('$.replace: scope replace should be ok', () => {
  638. const res = $(`
  639. var n = {};
  640. n.n = square(10);
  641. function ajax(){
  642. console.log(n)
  643. const {a, n} = obj;
  644. console.log("没想到吧,我又来了", n)
  645. $.ajax({
  646. url:"",
  647. success:function(n){
  648. console.log(n)
  649. }
  650. })
  651. }
  652. function test(n) {
  653. n = 5
  654. }
  655. function test2() {
  656. n = 5
  657. }
  658. function square(n) {
  659. return n * n;
  660. }
  661. `)
  662. .find('n')
  663. .each(n => {
  664. if (n[0].nodePath.scope.lookup('n') && n[0].nodePath.scope.lookup('n').isGlobal && n[0].nodePath.name !== 'property') {
  665. n.replaceBy('x')
  666. }
  667. })
  668. .root()
  669. .generate()
  670. expect(res.match(/x/g).length == 5).toBeTruthy();
  671. })
  672. test('$.replace: this replace should be ok', () => {
  673. let res = $(`
  674. var isTrue = !0;
  675. var isFalse = !1;
  676. `)
  677. .replace('!0', 'true')
  678. .generate();
  679. expect(res.match(/true/g).length == 1).toBeTruthy();
  680. })
  681. test('$.replace: this replace should be ok', () => {
  682. let res = $(`
  683. export default {
  684. init() {
  685. let strMap = {
  686. borrowCDBMoney: ['押金', '$元'],
  687. gratisTime: ['免费时间', '$'],
  688. borrowPay: ['起步收费', '$'],
  689. hourPay: ['超时收费', '$'],
  690. buyLinePay: ['充电线', '$元/条'],
  691. presentLineType: ['购线券', '$'],
  692. dayMaxPay: ['每日上限', '$元'],
  693. chargeType: ['收费模式', '$']
  694. }
  695. },
  696. }
  697. `)
  698. .replace(`export default {$$$}`, `export default {$$$,emits:['remark']}`).generate()
  699. expect(res.match(/remark/g).length == 1).toBeTruthy();
  700. })
  701. test('$.replace: this replace should be ok', () => {
  702. let res = $(`
  703. <td class="abc"><td>
  704. `, { parseOptions: { language: 'html' } })
  705. .replace('<td class="abc"><td>', '<tag>xxx</tag>')
  706. .generate()
  707. expect(res.match(/tag/g).length == 2).toBeTruthy();
  708. })
  709. test('$.replace: this replace should be ok', () => {
  710. let res = $(`
  711. enum TYPE {
  712. A = 1,
  713. B = 2,
  714. C = 3
  715. }`)
  716. .replace('enum TYPE { $$$ }', 'enum TYPE { $$$, D = 4 }')
  717. .generate()
  718. expect(res.match('D = 4')).toBeTruthy()
  719. })
  720. test('parse html data-id', () => {
  721. let res = $(`
  722. <div>
  723. <block class="zpzp" data-id="zpid">我是内容</block>
  724. <block class="zp" data-id="zpid">我也是内容</block>
  725. <block class="zp" data-id="zpid">打酱油的来了</block>
  726. </div>
  727. `, { parseOptions: { language: 'html' } })
  728. .replace(
  729. '<$_$1 class="$_$2" $$$1>$$$2</$_$1>',
  730. '<$_$1 $$$1>$$$2</$_$1>'
  731. )
  732. .replace(
  733. '<$_$1 data-id="$_$2" $$$1>$$$2</$_$1>',
  734. '<$_$1 $$$1>$$$2</$_$1>'
  735. )
  736. .generate()
  737. console.log(res)
  738. expect(!res.match('data-id')).toBeTruthy()
  739. })
  740. test('parse html replace comments', () => {
  741. let res = $(`
  742. <div class="a">1</div>
  743. <div class="a">2</div>
  744. <!-- <view>TEST</view> -->
  745. `, { parseOptions: { language: 'html' } })
  746. .replace('<view></view>', '<div>也替换了嘛?算正常不?</div>')
  747. .generate()
  748. expect(!!res.match('view')).toBeTruthy()
  749. })
  750. test('parse html replace SpreadElement', () => {
  751. let res = $(`
  752. ({
  753. methods: {
  754. ...a({
  755. f: 'f'
  756. }),
  757. ...b({
  758. d: 'd'
  759. })
  760. }
  761. })
  762. `)
  763. .replace(`methods: { $$$ }`, `methods: { $$$, c: 'c', ...d() }`)
  764. .generate()
  765. expect(res.match('...d') && res.match(`'c'`)).toBeTruthy()
  766. })
  767. test('$.replace: script replace', () => {
  768. let res = $(`<script attr="5" data="10" >var a = 1;
  769. var b = 1</script> `, { parseOptions: { language: 'html'}})
  770. .replace(`<script attr="$_$1" $$$1>$_$2</script>`, `<script newattr="newvalue" $$$1>$_$2</script>`)
  771. .generate()
  772. expect(res.match('newattr="newvalue"') && res.match('var a = 1')).toBeTruthy()
  773. })
  774. test('$.replace: multi script replace', () => {
  775. let res = $(`
  776. <script attr="5" data="10" >
  777. var a = 1;
  778. var b = 1
  779. </script>
  780. <script attr="6" >
  781. var b = 1
  782. </script> `, { parseOptions: { language: 'html'}})
  783. .replace(`<script attr="$_$1" $$$1>$_$2</script>`, `<script newattr="newvalue" $$$1>$_$2</script>`)
  784. .generate()
  785. expect(res.match('newattr="newvalue"') && res.match('var b = 1')).toBeTruthy()
  786. })
  787. test('$.replace: multi script replace', () => {
  788. let res = $(`export default {};`)
  789. .replace('export default {$$$}', 'export default {$$$, data:{}}').generate()
  790. expect(res.match('data')).toBeTruthy()
  791. })
  792. test('$.replace: template $$$ replace', () => {
  793. let res = $(` <div>
  794. hello,
  795. <span>{{ name }}</span>
  796. <span>{{ id }}</span>
  797. </div>`, { parseOptions: { language: 'html'} })
  798. .replace('<div>hello, $$$1</div>', '<div>bye, $$$1</div>').generate()
  799. expect(!res.match('hello')).toBeTruthy()
  800. })
  801. test('$.replace: template $$$ replace', () => {
  802. let res = $(` <div>
  803. <span>hello, </span>
  804. <span>{{ name }}</span>
  805. </div>`, { parseOptions: { language: 'html'} })
  806. .replace('<div><span>hello, </span>$$$1</div>', '<div>bye, $$$1</div>').generate()
  807. expect(!res.match('hello')).toBeTruthy()
  808. })
  809. test('$.replace: template $_$ string replace', () => {
  810. let res = $(`console.log(a)`)
  811. .replace('console.log($_$1)', `console.log("$_$1", $_$1)`).generate()
  812. expect(res == `console.log("a", a)`).toBeTruthy()
  813. })
  814. test('$.replace: function replace', () => {
  815. let findLength = $(`console.log(a, b, c)`)
  816. .find(`console.log($_$1)`)
  817. .match[1].length
  818. let replaceLength = 0;
  819. $(`console.log(a, b, c)`)
  820. .replace(`console.log($_$1)`, (match) => {
  821. replaceLength = match[1].length
  822. })
  823. expect(findLength == replaceLength).toBeTruthy()
  824. })
  825. test('$.replace: empty attr replace', () => {
  826. let res = $(`<div class="">2</div>
  827. `, { parseOptions: { language: 'html'} })
  828. .replace('<div class="$_$1">$$$2</div>', '<div className="$_$1">$$$2</div>')
  829. expect(res.generate().match('className=""')).toBeTruthy()
  830. })
  831. test('$.replace: import. replace', () => {
  832. let res = $(`const req = require.context('./', true, /\.svg$/)`, { parseOptions: {
  833. sourceType: 'module'
  834. }})
  835. .replace('const $_$ = require.context($$$)', `const $_$ = import.meta.glob('./**/*.svg')`)
  836. expect(res.generate().match('import.meta')).toBeTruthy()
  837. })
  838. test('$.replace: import. replace', () => {
  839. let res = $(`export const foo = (bar1 = 1, bar2 = 2) => bar1 + bar2`)
  840. .replace(
  841. `const $_$1 = ($$$1) => $_$2 `,
  842. `function $_$1($$$1) { return $_$2 }`
  843. )
  844. expect(res.generate().match('bar1 = 1,')).toBeTruthy()
  845. })
  846. test('$.replace: type. replace', () => {
  847. let res = $(`export type TDateTimeSelectValueItem = moment.Moment.m | string;`)
  848. .replace(
  849. `moment.Moment.m`,
  850. `Dayjs`
  851. )
  852. expect(res.generate().match('Dayjs')).toBeTruthy()
  853. })
  854. test('$.replace: string space', () => {
  855. let res = $(`class Version {
  856. async update(options) {
  857. const sql = \`
  858. c
  859. a
  860. \`
  861. }
  862. }`)
  863. .replace('async update(options) {$_$}', 'async update(options) {$_$}')
  864. expect(res.generate().match(`c\n a`)).toBeTruthy()
  865. })