() => {
   const {decorationAnalyses, sourceFile, renderer} = setup(PROGRAM);
   const output = new MagicString(PROGRAM.contents);
   const compiledClass =
       decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
   const decorator = compiledClass.decorators[0];
   const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
   decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
   renderer.removeDecorators(output, decoratorsToRemove);
   expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
   expect(output.toString()).toContain(`{ type: OtherA }`);
   expect(output.toString())
       .not.toContain(`{ type: Directive, args: [{ selector: '[b]' }] }`);
   expect(output.toString()).toContain(`{ type: OtherB }`);
   expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[c]' }] }`);
 });
       () => {
         const {analyzer, parser, program, renderer} = setup(PROGRAM);
         const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
         const output = new MagicString(PROGRAM.contents);
         const analyzedClass = analyzedFile.analyzedClasses[2];
         const decorator = analyzedClass.decorators[0];
         const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
         decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
         renderer.removeDecorators(output, decoratorsToRemove);
         expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
         expect(output.toString()).toContain(`{ type: OtherA }`);
         expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[b]' }] }`);
         expect(output.toString()).toContain(`{ type: OtherB }`);
         expect(output.toString()).not.toContain(`C.decorators = [
  { type: Directive, args: [{ selector: '[c]' }] },
];`);
       });
 it('should insert a default import at the start of the source file', () => {
   const {renderer} = setup(PROGRAM);
   const output = new MagicString(PROGRAM.contents);
   renderer.addImports(output, [
     {specifier: 'test', qualifier: 'i0', isDefault: true},
   ]);
   expect(output.toString()).toContain(`import i0 from 'test';`);
 });
    it('should insert the given imports at the start of the source file', () => {
      const {renderer} = setup(PROGRAM);
      const output = new MagicString(PROGRAM.contents);
      renderer.addImports(
          output, [{name: '@angular/core', as: 'i0'}, {name: '@angular/common', as: 'i1'}]);
      expect(output.toString()).toContain(`import * as i0 from '@angular/core';
import * as i1 from '@angular/common';

/* A copyright notice */`);
    });
Beispiel #5
0
  static run(content: string): StageResult {
    let log = logger(this.name);
    log(content);

    let editor = new MagicString(content);
    addVariableDeclarations(content, editor);
    return {
      code: editor.toString(),
      suggestions: []
    };
  }
    it('should insert the definitions directly after the class declaration', () => {
      const {analyzer, parser, program, renderer} = setup(PROGRAM);
      const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
      const output = new MagicString(PROGRAM.contents);
      renderer.addDefinitions(output, analyzedFile.analyzedClasses[0], 'SOME DEFINITION TEXT');
      expect(output.toString()).toContain(`
  function A() {}
SOME DEFINITION TEXT
  A.decorators = [
`);
    });
 it('should switch marked declaration initializers', () => {
   const {renderer, program, switchMarkerAnalyses, sourceFile} = setup(PROGRAM);
   const file = program.getSourceFile('some/file.js');
   if (file === undefined) {
     throw new Error(`Could not find source file`);
   }
   const output = new MagicString(PROGRAM.contents);
   renderer.rewriteSwitchableDeclarations(
       output, file, switchMarkerAnalyses.get(sourceFile) !.declarations);
   expect(output.toString())
       .not.toContain(`let compileNgModuleFactory = compileNgModuleFactory__PRE_R3__;`);
   expect(output.toString())
       .toContain(`let badlyFormattedVariable = __PRE_R3__badlyFormattedVariable;`);
   expect(output.toString())
       .toContain(`let compileNgModuleFactory = compileNgModuleFactory__POST_R3__;`);
   expect(output.toString())
       .toContain(`function compileNgModuleFactory__PRE_R3__(injector, options, moduleType) {`);
   expect(output.toString())
       .toContain(`function compileNgModuleFactory__POST_R3__(injector, options, moduleType) {`);
 });
    it('should insert the definitions directly after the class declaration', () => {
      const {renderer, decorationAnalyses, sourceFile} = setup(PROGRAM);
      const output = new MagicString(PROGRAM.contents);
      const analyzedClass = decorationAnalyses.get(sourceFile) !.analyzedClasses[0];
      renderer.addDefinitions(output, analyzedClass, 'SOME DEFINITION TEXT');
      expect(output.toString()).toContain(`
export class A {}
SOME DEFINITION TEXT
A.decorators = [
`);
    });
  transform: (code: string) => {
    const newContent = new MagicString(code);

    // Walks through every occurrence of a license comment and overwrites it with an empty string.
    for (let pos = -1; (pos = code.indexOf(licenseBanner, pos + 1)) !== -1; null) {
      newContent.overwrite(pos, pos + licenseBanner.length, '');
    }

    return {
      code: newContent.toString(),
      map:  newContent.generateMap({ hires: true })
    };
  }
Beispiel #10
0
 it('should not insert alias exports in js output', () => {
   const {renderer} = setup(PROGRAM);
   const output = new MagicString(PROGRAM.contents);
   renderer.addExports(output, PROGRAM.name.replace(/\.js$/, ''), [
     {from: '/some/a.js', alias: 'eComponentA1', identifier: 'ComponentA1'},
     {from: '/some/a.js', alias: 'eComponentA2', identifier: 'ComponentA2'},
     {from: '/some/foo/b.js', alias: 'eComponentB', identifier: 'ComponentB'},
     {from: PROGRAM.name, alias: 'eTopLevelComponent', identifier: 'TopLevelComponent'},
   ]);
   const outputString = output.toString();
   expect(outputString).not.toContain(`{eComponentA1 as ComponentA1}`);
   expect(outputString).not.toContain(`{eComponentB as ComponentB}`);
   expect(outputString).not.toContain(`{eTopLevelComponent as TopLevelComponent}`);
 });