import { expect } from './index' ; import { createIdentifier , createPrinter , createPropertyAccess , createThis , NewLineKind } from 'typescript' ; import { tsquery } from '../src/index' ; const printerOptions = { newLine : NewLineKind . LineFeed } ; describe ( 'tsquery:' , ( ) => { describe ( 'tsquery.map:' , ( ) => { it ( 'should replace individual AST nodes:' , ( ) => { const ast = tsquery . ast ( `

console.log('foo');
console.log('bar');
// console.log('baz');

            ` . trim ( ) ) ; const result = tsquery . map ( ast , 'Identifier[name="console"]' , ( ) => createIdentifier ( 'logger' ) ) ; const printer = createPrinter ( printerOptions ) ; expect ( printer . printFile ( result ) . trim ( ) ) . to . equal ( `

logger.log('foo');
logger.log('bar');
// console.log('baz');

            ` . trim ( ) ) ; } ) ; it ( 'should replace multiple AST nodes:' , ( ) => { const ast = tsquery . ast ( `

console.log('foo');
console.log('bar');
// console.log('baz');

            ` . trim ( ) ) ; const result = tsquery . map ( ast , 'Identifier[name="console"]' , ( ) => createPropertyAccess ( createThis ( ) , 'logger' ) ) ; const printer = createPrinter ( printerOptions ) ; expect ( printer . printFile ( result ) . trim ( ) ) . to . equal ( `

this.logger.log('foo');
this.logger.log('bar');
// console.log('baz');

            ` . trim ( ) ) ; } ) ; it ( 'should handle a noop transformer' , ( ) => { const ast = tsquery . ast ( `

console.log('foo');
console.log('bar');
// console.log('baz');

            ` . trim ( ) ) ; const result = tsquery . map ( ast , 'Identifier[name="console"]' , ( ) => null ) ; const printer = createPrinter ( printerOptions ) ; expect ( printer . printFile ( result ) . trim ( ) ) . to . equal ( `

console.log('foo');
console.log('bar');
// console.log('baz');

            ` . trim ( ) ) ; } ) ; } ) ; } ) ;