File tree Expand file tree Collapse file tree 2 files changed +45
-3
lines changed
Expand file tree Collapse file tree 2 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ new Vue({
2323}).$mount('#app')
2424` . trim ( ) )
2525fs . writeFileSync ( path . resolve ( templateDir , 'empty-entry.js' ) , `;` )
26+ fs . writeFileSync ( path . resolve ( templateDir , 'main.ts' ) , `const a: string = 'hello';` )
2627fs . writeFileSync ( path . resolve ( templateDir , 'hello.vue' ) , `
2728<template>
2829 <p>Hello, {{ msg }}</p>
@@ -530,6 +531,23 @@ test('api: injectImports to empty file', async () => {
530531 expect ( fs . readFileSync ( '/main.js' , 'utf-8' ) ) . toMatch ( / i m p o r t f o o f r o m ' f o o ' \r ? \n i m p o r t b a r f r o m ' b a r ' / )
531532} )
532533
534+ test ( 'api: injectImports to typescript file' , async ( ) => {
535+ const generator = new Generator ( '/' , { plugins : [
536+ {
537+ id : 'test' ,
538+ apply : api => {
539+ api . injectImports ( 'main.ts' , `import foo from 'foo'` )
540+ api . render ( {
541+ 'main.ts' : path . join ( templateDir , 'main.ts' )
542+ } )
543+ }
544+ }
545+ ] } )
546+
547+ await generator . generate ( )
548+ expect ( fs . readFileSync ( '/main.ts' , 'utf-8' ) ) . toMatch ( / i m p o r t f o o f r o m ' f o o ' / )
549+ } )
550+
533551test ( 'api: addEntryDuplicateImport' , async ( ) => {
534552 const generator = new Generator ( '/' , { plugins : [
535553 {
Original file line number Diff line number Diff line change 1- const jscodeshift = require ( 'jscodeshift' )
21const adapt = require ( 'vue-jscodeshift-adapter' )
2+ let jscodeshift = require ( 'jscodeshift' )
33
4- module . exports = function runCodemod ( transform , fileInfo , options ) {
5- return adapt ( transform ) ( fileInfo , { jscodeshift } , options || { } )
4+ module . exports = function runCodemod ( transformModule , fileInfo , options = { } ) {
5+ const transform = typeof transformModule . default === 'function'
6+ ? transformModule . default
7+ : transformModule
8+
9+ let parser = transformModule . parser || options . parser
10+ if ( ! parser ) {
11+ if ( fileInfo . path . endsWith ( ( '.ts' ) ) ) {
12+ parser = 'ts'
13+ } else if ( fileInfo . path . endsWith ( '.tsx' ) ) {
14+ parser = 'tsx'
15+ }
16+ }
17+
18+ if ( parser ) {
19+ jscodeshift = jscodeshift . withParser ( parser )
20+ }
21+
22+ const api = {
23+ jscodeshift,
24+ j : jscodeshift ,
25+ stats : ( ) => { } ,
26+ report : ( ) => { }
27+ }
28+
29+ return adapt ( transform ) ( fileInfo , api , options )
630}
You can’t perform that action at this time.
0 commit comments