Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

匹配错 import from #255

Open
dedoyle opened this issue Oct 29, 2024 · 0 comments
Open

匹配错 import from #255

dedoyle opened this issue Oct 29, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@dedoyle
Copy link

dedoyle commented Oct 29, 2024

code to transform 可以贴这个代码

import * as React from 'react';

import { DayPicker } from 'react-day-picker';

import { buttonVariants } from '@foo/ui/button';
import { cn } from '@foo/ui/utils';

export type CalendarProps = React.ComponentProps<typeof DayPicker>;

function Calendar({
  className,
  classNames,
  showOutsideDays = true,
  ...props
}: CalendarProps) {
  return (
    <DayPicker
      showOutsideDays={showOutsideDays}
      className={cn('p-3', className)}
      classNames={{
        months: 'flex flex-col sm:flex-row space-y-4 sm:space-y-0',
        month: 'space-y-4',
        month_caption: 'flex justify-center pt-1 relative items-center',
        caption_label: 'text-sm',
        nav: 'space-x-1 flex',
        chevron: 'h-4 w-4',
        button_previous: cn(
          buttonVariants({ variant: 'outline' }),
          'absolute left-2 h-7 w-7 bg-transparent p-0 opacity-50 hover:opacity-100 z-10'
        ),
        button_next: cn(
          buttonVariants({ variant: 'outline' }),
          'absolute right-2 h-7 w-7 bg-transparent p-0 opacity-50 hover:opacity-100 z-10'
        ),
        table: 'w-full border-collapse space-y-1',
        weekdays: 'flex',
        weekday:
          'text-muted-foreground rounded-md w-9 font-normal text-[0.8rem] text-center',
        week: 'flex w-full mt-2',
        day_button:
          'w-full h-full text-center text-sm p-0 relative [&:has([aria-selected])]:bg-accent first:[&:has([aria-selected])]:rounded-l-md last:[&:has([aria-selected])]:rounded-r-md focus-within:relative focus-within:z-20',
        day: cn(
          buttonVariants({ variant: 'ghost' }),
          'h-9 w-9 p-0 aria-selected:opacity-100'
        ),
        selected:
          'bg-primary text-primary-foreground hover:bg-primary hover:text-primary-foreground focus:bg-primary focus:text-primary-foreground',
        today: 'text-primary',
        outside: 'text-muted-foreground opacity-50',
        disabled: 'text-muted-foreground opacity-50',
        range_middle:
          'aria-selected:bg-accent aria-selected:text-accent-foreground',
        hidden: 'invisible',
        ...classNames,
      }}
      {...props}
    />
  );
}
Calendar.displayName = 'Calendar';

export { Calendar };

transform script 可以贴这个代码:

function transform(fileInfo, api, options) {
  const $ = api.gogocode
  const source = fileInfo.source
  // return your transformed code here
  return $(source).replace(`import { $$$0 } from '@foo/ui$_$'`, `import { $$$0 } from '@bar/ui'`).generate()
}
  • your description:
    我的项目用的 react,希望替换 js, jsx, ts, tsx 代码里的 import { xxx } from '@foo/ui/xxx',改成 import { xxx } from '@bar/ui'

发现以下这两种方式都会匹配到所有 import from,比如 import { DayPicker } from 'react-day-picker'; 也会被替换成 import { DayPicker } from '@bar/ui'

ast.replace(`import { $$$0 } from '@foo/ui$_$'`, `import { $$$0 } from '@bar/ui'`)
ast.replace(`import { $$$0 } from '@foo/ui$$$1'`, `import { $$$0 } from '@bar/ui'`)

官方文档里的 import $_$1 from '$_$2' 则会报错,始终无法跑。

@dedoyle dedoyle added the bug Something isn't working label Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant