Skip to content

Commit

Permalink
Alternative pattern for matching local imports (#22)
Browse files Browse the repository at this point in the history
* Alternative pattern for matching local imports

* Support doublequotes in local import
  • Loading branch information
Xarvalus authored and diegohaz committed May 31, 2018
1 parent 91a1e77 commit aec90ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export const replaceContents = (
oldName: string,
newName: string
): string => contents.replace(
new RegExp(`([^a-zA-Z0-9_$])${oldName}([^a-zA-Z0-9_$]|Container)`, 'g'),
`$1${newName}$2`
new RegExp(`([^a-zA-Z0-9_$])${oldName}([^a-zA-Z0-9_$]|Container)|(['|"]./[a-zA-Z0-9_$]*?)${oldName}([a-zA-Z0-9_$]*?)`, 'g'),
`$1$3${newName}$2$4`
)

export const replicate = async (
Expand Down
14 changes: 13 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,27 @@ test('getComponentFiles', async () => {
test('replaceContents', () => {
const contents = `
import './Button.css'
import ButtonComponent from '../ButtonComponent'
import NodeButtonComponent from 'node-button-component'
import NodeYellowButtonComponent from 'node-component/YellowButton'
import SimpleButton from './SimpleButton'
import { someButtonUtil } from './SimpleButtonUtils'
import { someButtonUtil } from "./SimpleButtonUtils";
const Button = () => <button />
export const someButtonUtil = () => {}
export default Button
`

expect(replaceContents(contents, 'Button', 'AnotherButton')).toBe(`
import './AnotherButton.css'
import SimpleButton from './SimpleButton'
import ButtonComponent from '../ButtonComponent'
import NodeButtonComponent from 'node-button-component'
import NodeYellowButtonComponent from 'node-component/YellowButton'
import SimpleButton from './SimpleAnotherButton'
import { someButtonUtil } from './SimpleAnotherButtonUtils'
import { someButtonUtil } from "./SimpleAnotherButtonUtils";
const AnotherButton = () => <button />
export const someButtonUtil = () => {}
export default AnotherButton
`)
})
Expand Down

0 comments on commit aec90ab

Please sign in to comment.