@@ -20,18 +20,24 @@ import {fromVisitor, RecipeSpec} from "../../src/test";
2020import {
2121 AddImport ,
2222 ImportStyle ,
23+ IntelliJ ,
2324 javascript ,
2425 JavaScriptVisitor ,
2526 JS ,
2627 maybeAddImport ,
2728 npm ,
2829 packageJson ,
2930 RemoveImport ,
31+ SpacesStyle ,
3032 Template ,
3133 tsx ,
3234 typescript
3335} from "../../src/javascript" ;
3436import { emptySpace , J } from "../../src/java" ;
37+ import { MarkersKind } from "../../src/markers" ;
38+ import { randomId } from "../../src/uuid" ;
39+ import { NamedStyles } from "../../src/style" ;
40+ import { produce } from "immer" ;
3541import { withDir } from "tmp-promise" ;
3642
3743/**
@@ -1701,4 +1707,74 @@ describe('AddImport visitor', () => {
17011707 ) ;
17021708 } ) ;
17031709 } ) ;
1710+
1711+ describe ( 'style options' , ( ) => {
1712+ /**
1713+ * Helper function to create a visitor that:
1714+ * 1. Adds a NamedStyles marker to the compilation unit with es6ImportExportBraces: true
1715+ * 2. Then uses AddImport to add an import statement
1716+ */
1717+ function createAddImportWithBraceSpacingVisitor (
1718+ module : string ,
1719+ member ?: string ,
1720+ alias ?: string
1721+ ) : JavaScriptVisitor < any > {
1722+ return new class extends JavaScriptVisitor < any > {
1723+ override async visitJsCompilationUnit ( cu : JS . CompilationUnit , p : any ) : Promise < J | undefined > {
1724+ // Create a SpacesStyle with es6ImportExportBraces: true using produce
1725+ const spacesStyle : SpacesStyle = produce ( IntelliJ . TypeScript . spaces ( ) , draft => {
1726+ draft . within . es6ImportExportBraces = true ;
1727+ } ) ;
1728+
1729+ // Create a NamedStyles marker
1730+ const namedStyles : NamedStyles = {
1731+ kind : MarkersKind . NamedStyles ,
1732+ id : randomId ( ) ,
1733+ name : "test-style" ,
1734+ displayName : "Test Style" ,
1735+ tags : [ ] ,
1736+ styles : [ spacesStyle ]
1737+ } ;
1738+
1739+ // Add the NamedStyles marker to the compilation unit
1740+ let result : JS . CompilationUnit = {
1741+ ...cu ,
1742+ markers : {
1743+ ...cu . markers ,
1744+ markers : [ ...cu . markers . markers , namedStyles ]
1745+ }
1746+ } ;
1747+
1748+ // Then add the import
1749+ const addImport = new AddImport ( {
1750+ module,
1751+ member,
1752+ alias,
1753+ onlyIfReferenced : false
1754+ } ) ;
1755+ result = await addImport . visit ( result , p ) as JS . CompilationUnit ;
1756+
1757+ return result ;
1758+ }
1759+ } ;
1760+ }
1761+
1762+ test ( 'should add spaces inside braces when es6ImportExportBraces style is true' , async ( ) => {
1763+ const spec = new RecipeSpec ( ) ;
1764+ spec . recipe = fromVisitor ( createAddImportWithBraceSpacingVisitor ( 'fs' , 'readFile' ) ) ;
1765+
1766+ await spec . rewriteRun (
1767+ typescript (
1768+ `
1769+ const x = 1;
1770+ ` ,
1771+ `
1772+ import { readFile } from 'fs';
1773+
1774+ const x = 1;
1775+ `
1776+ )
1777+ ) ;
1778+ } ) ;
1779+ } ) ;
17041780} ) ;
0 commit comments