Skip to content

Commit 6aaa7f9

Browse files
committed
feat: accept additional imports in addModelMixins method
1 parent 6c6032d commit 6aaa7f9

2 files changed

Lines changed: 142 additions & 1 deletion

File tree

src/code_transformer/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ export class CodeTransformer {
720720
await file.save()
721721
}
722722

723-
async addModelMixins(modelFileName: string, mixins: MixinDefinition[]) {
723+
async addModelMixins(modelFileName: string, mixins: MixinDefinition[], imports?: ImportInfo[]) {
724724
const directories = this.getDirectories()
725725
const filePath = `${directories.models}/${modelFileName}`
726726

@@ -775,6 +775,13 @@ export class CodeTransformer {
775775
})
776776
this.#addImportsFromImportInfo(file, mixinImports)
777777

778+
/**
779+
* Add additional imports if specified
780+
*/
781+
if (imports) {
782+
this.#addImportsFromImportInfo(file, imports)
783+
}
784+
778785
/**
779786
* Get the heritage clause (extends clause)
780787
*/

tests/code_transformer.spec.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,140 @@ test.group('Code Transformer | addModelMixins', (group) => {
18951895
`)
18961896
})
18971897

1898+
test('add mixins with additional imports', async ({ assert, fs }) => {
1899+
const transformer = new CodeTransformer(fs.baseUrl)
1900+
await fs.create(
1901+
'app/models/user.ts',
1902+
dedent`
1903+
import { compose } from '@adonisjs/core/helpers'
1904+
import { UserSchema } from '#database/schema'
1905+
1906+
export default class User extends compose(UserSchema) {
1907+
}
1908+
`
1909+
)
1910+
1911+
await transformer.addModelMixins(
1912+
'user.ts',
1913+
[
1914+
{
1915+
importPath: '#kit/auth',
1916+
importType: 'named',
1917+
name: 'withManagedEmail',
1918+
},
1919+
],
1920+
[
1921+
{
1922+
source: '#services/hash',
1923+
namedImports: ['hash'],
1924+
},
1925+
]
1926+
)
1927+
1928+
const file = await fs.contents('app/models/user.ts')
1929+
assert.snapshot(file).matchInline(`
1930+
"import { compose } from '@adonisjs/core/helpers'
1931+
import { UserSchema } from '#database/schema'
1932+
import { withManagedEmail } from '#kit/auth'
1933+
import { hash } from '#services/hash'
1934+
1935+
export default class User extends compose(UserSchema, withManagedEmail()) {
1936+
}
1937+
"
1938+
`)
1939+
})
1940+
1941+
test('additional imports should merge with existing imports from same module', async ({
1942+
assert,
1943+
fs,
1944+
}) => {
1945+
const transformer = new CodeTransformer(fs.baseUrl)
1946+
await fs.create(
1947+
'app/models/user.ts',
1948+
dedent`
1949+
import { compose } from '@adonisjs/core/helpers'
1950+
import { UserSchema } from '#database/schema'
1951+
import { hash } from '#services/hash'
1952+
1953+
export default class User extends compose(UserSchema) {
1954+
}
1955+
`
1956+
)
1957+
1958+
await transformer.addModelMixins(
1959+
'user.ts',
1960+
[
1961+
{
1962+
importPath: '#kit/auth',
1963+
importType: 'named',
1964+
name: 'withManagedEmail',
1965+
},
1966+
],
1967+
[
1968+
{
1969+
source: '#services/hash',
1970+
namedImports: ['scrypt'],
1971+
},
1972+
]
1973+
)
1974+
1975+
const file = await fs.contents('app/models/user.ts')
1976+
assert.snapshot(file).matchInline(`
1977+
"import { compose } from '@adonisjs/core/helpers'
1978+
import { UserSchema } from '#database/schema'
1979+
import { hash, scrypt } from '#services/hash'
1980+
import { withManagedEmail } from '#kit/auth'
1981+
1982+
export default class User extends compose(UserSchema, withManagedEmail()) {
1983+
}
1984+
"
1985+
`)
1986+
})
1987+
1988+
test('additional imports should not duplicate existing imports', async ({ assert, fs }) => {
1989+
const transformer = new CodeTransformer(fs.baseUrl)
1990+
await fs.create(
1991+
'app/models/user.ts',
1992+
dedent`
1993+
import { compose } from '@adonisjs/core/helpers'
1994+
import { UserSchema } from '#database/schema'
1995+
import { hash } from '#services/hash'
1996+
1997+
export default class User extends compose(UserSchema) {
1998+
}
1999+
`
2000+
)
2001+
2002+
await transformer.addModelMixins(
2003+
'user.ts',
2004+
[
2005+
{
2006+
importPath: '#kit/auth',
2007+
importType: 'named',
2008+
name: 'withManagedEmail',
2009+
},
2010+
],
2011+
[
2012+
{
2013+
source: '#services/hash',
2014+
namedImports: ['hash'],
2015+
},
2016+
]
2017+
)
2018+
2019+
const file = await fs.contents('app/models/user.ts')
2020+
assert.snapshot(file).matchInline(`
2021+
"import { compose } from '@adonisjs/core/helpers'
2022+
import { UserSchema } from '#database/schema'
2023+
import { hash } from '#services/hash'
2024+
import { withManagedEmail } from '#kit/auth'
2025+
2026+
export default class User extends compose(UserSchema, withManagedEmail()) {
2027+
}
2028+
"
2029+
`)
2030+
})
2031+
18982032
test('add multiple mixins with different arguments', async ({ assert, fs }) => {
18992033
const transformer = new CodeTransformer(fs.baseUrl)
19002034
await fs.create(

0 commit comments

Comments
 (0)