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

fix(module): handle former props in ContentSlot transformer #2525

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export default defineNuxtModule<ModuleOptions>({
enforce: 'pre',
transform (code) {
if (code.includes('ContentSlot')) {
code = code.replace(/<ContentSlot(\s)+([.]*)(:use=['"](\$slots.)?([a-zA-Z0-9_-]*)['"]|use=['"]([a-zA-Z0-9_-]*)['"])/g, '<MDCSlot$1$2 name="$5"')
code = code.replace(/<ContentSlot(\s)+([^/>]*)(:use=['"](\$slots.)?([a-zA-Z0-9_-]*)['"])/g, '<MDCSlot$1$2name="$5"')
code = code.replace(/<\/ContentSlot>/g, '</MDCSlot>')
code = code.replace(/<ContentSlot/g, '<MDCSlot')
code = code.replace(/(['"])ContentSlot['"]/g, '$1MDCSlot$1')
Expand Down
57 changes: 57 additions & 0 deletions test/contentslot-transform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { test, describe, expect } from 'vitest'

describe('Content slot transform', () => {
const transform = (code: string) => {
if (code.includes('ContentSlot')) {
code = code.replace(/<ContentSlot(\s)+([^/>]*)(:use=['"](\$slots.)?([a-zA-Z0-9_-]*)['"])/g, '<MDCSlot$1$2name="$5"')
code = code.replace(/<\/ContentSlot>/g, '</MDCSlot>')
code = code.replace(/<ContentSlot/g, '<MDCSlot')
code = code.replace(/(['"])ContentSlot['"]/g, '$1MDCSlot$1')
code = code.replace(/ContentSlot\(([^(]*)(:use=['"](\$slots.)?([a-zA-Z0-9_-]*)['"]|use=['"]([a-zA-Z0-9_-]*)['"])([^)]*)/g, 'MDCSlot($1name="$4"$6')
return {
code,
map: { mappings: '' }
}
}
}
test('Self Closing', () => {
expect(transform('<ContentSlot :use="$slots.foo" />')?.code).toBe('<MDCSlot name="foo" />')
expect(transform('<ContentSlot unwrap="p" :use="$slots.foo" />')?.code).toBe('<MDCSlot unwrap="p" name="foo" />')
expect(transform('<ContentSlot :unwrap="[\'p\']" :use="$slots.foo" />')?.code).toBe('<MDCSlot :unwrap="[\'p\']" name="foo" />')
expect(transform('<ContentSlot some-pros="value" :use="$slots.foo" />')?.code).toBe('<MDCSlot some-pros="value" name="foo" />')

expect(transform('<ContentSlot :use="$slots.foo" unwrap="p" />')?.code).toBe('<MDCSlot name="foo" unwrap="p" />')
expect(transform('<ContentSlot :use="$slots.foo" :unwrap="[\'p\']" />')?.code).toBe('<MDCSlot name="foo" :unwrap="[\'p\']" />')
expect(transform('<ContentSlot :use="$slots.foo" some-pros="value" />')?.code).toBe('<MDCSlot name="foo" some-pros="value" />')

expect(transform('<ContentSlot\n:use="$slots.foo"\n/>')?.code).toBe('<MDCSlot\nname="foo"\n/>')
expect(transform('<ContentSlot\nunwrap="p"\n:use="$slots.foo"\n/>')?.code).toBe('<MDCSlot\nunwrap="p"\nname="foo"\n/>')
expect(transform('<ContentSlot\n:unwrap="[\'p\']"\n:use="$slots.foo"\n/>')?.code).toBe('<MDCSlot\n:unwrap="[\'p\']"\nname="foo"\n/>')
expect(transform('<ContentSlot\nsome-pros="value"\n:use="$slots.foo"\n/>')?.code).toBe('<MDCSlot\nsome-pros="value"\nname="foo"\n/>')

expect(transform('<ContentSlot\n:use="$slots.foo"\nunwrap="p"\n/>')?.code).toBe('<MDCSlot\nname="foo"\nunwrap="p"\n/>')
expect(transform('<ContentSlot\n:use="$slots.foo"\n:unwrap="[\'p\']"\n/>')?.code).toBe('<MDCSlot\nname="foo"\n:unwrap="[\'p\']"\n/>')
expect(transform('<ContentSlot\n:use="$slots.foo"\nsome-pros="value"\n/>')?.code).toBe('<MDCSlot\nname="foo"\nsome-pros="value"\n/>')
})

test('With Slot', () => {
expect(transform('<ContentSlot :use="$slots.foo"></ContentSlot>')?.code).toBe('<MDCSlot name="foo"></MDCSlot>')
expect(transform('<ContentSlot unwrap="p" :use="$slots.foo"></ContentSlot>')?.code).toBe('<MDCSlot unwrap="p" name="foo"></MDCSlot>')
expect(transform('<ContentSlot :unwrap="[\'p\']" :use="$slots.foo"></ContentSlot>')?.code).toBe('<MDCSlot :unwrap="[\'p\']" name="foo"></MDCSlot>')
expect(transform('<ContentSlot some-pros="value" :use="$slots.foo"></ContentSlot>')?.code).toBe('<MDCSlot some-pros="value" name="foo"></MDCSlot>')

expect(transform('<ContentSlot :use="$slots.foo" unwrap="p"></ContentSlot>')?.code).toBe('<MDCSlot name="foo" unwrap="p"></MDCSlot>')
expect(transform('<ContentSlot :use="$slots.foo" :unwrap="[\'p\']"></ContentSlot>')?.code).toBe('<MDCSlot name="foo" :unwrap="[\'p\']"></MDCSlot>')
expect(transform('<ContentSlot :use="$slots.foo" some-pros="value"></ContentSlot>')?.code).toBe('<MDCSlot name="foo" some-pros="value"></MDCSlot>')

const slotContent = '\n<div class="bg-black">Foo</div>\n'
expect(transform(`<ContentSlot\n:use="$slots.foo"\n>${slotContent}</ContentSlot>`)?.code).toBe(`<MDCSlot\nname="foo"\n>${slotContent}</MDCSlot>`)
expect(transform(`<ContentSlot\nunwrap="p"\n:use="$slots.foo"\n>${slotContent}</ContentSlot>`)?.code).toBe(`<MDCSlot\nunwrap="p"\nname="foo"\n>${slotContent}</MDCSlot>`)
expect(transform(`<ContentSlot\n:unwrap="['p']"\n:use="$slots.foo"\n>${slotContent}</ContentSlot>`)?.code).toBe(`<MDCSlot\n:unwrap="['p']"\nname="foo"\n>${slotContent}</MDCSlot>`)
expect(transform(`<ContentSlot\nsome-pros="value"\n:use="$slots.foo"\n>${slotContent}</ContentSlot>`)?.code).toBe(`<MDCSlot\nsome-pros="value"\nname="foo"\n>${slotContent}</MDCSlot>`)

expect(transform(`<ContentSlot\n:use="$slots.foo"\nunwrap="p"\n>${slotContent}</ContentSlot>`)?.code).toBe(`<MDCSlot\nname="foo"\nunwrap="p"\n>${slotContent}</MDCSlot>`)
expect(transform(`<ContentSlot\n:use="$slots.foo"\n:unwrap="['p']"\n>${slotContent}</ContentSlot>`)?.code).toBe(`<MDCSlot\nname="foo"\n:unwrap="['p']"\n>${slotContent}</MDCSlot>`)
expect(transform(`<ContentSlot\n:use="$slots.foo"\nsome-pros="value"\n>${slotContent}</ContentSlot>`)?.code).toBe(`<MDCSlot\nname="foo"\nsome-pros="value"\n>${slotContent}</MDCSlot>`)
})
})