ConsoleOutputDisableExpressionNode.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import * as esprima from 'esprima';
  2. import { INode } from "../../interfaces/nodes/INode";
  3. import { IOptions } from "../../interfaces/IOptions";
  4. import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
  5. import { Node } from '../Node';
  6. import { NodeUtils } from "../../NodeUtils";
  7. export class ConsoleOutputDisableExpressionNode extends Node {
  8. /**
  9. * @param options
  10. */
  11. constructor (options: IOptions) {
  12. super(options);
  13. this.node = this.getNodeStructure();
  14. }
  15. /**
  16. * @param blockScopeNode
  17. */
  18. public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
  19. NodeUtils.prependNode(blockScopeNode.body, this.getNode());
  20. }
  21. /**
  22. * JSCrush version of following code
  23. *
  24. * (function () {
  25. * var _console = []["filter"]["constructor"]("return this")().console;
  26. * var _function = function () {};
  27. *
  28. * _console.log = _function;
  29. * _console.info = _function;
  30. * _console.warn = _function;
  31. * _console.error = _function;
  32. * _console
  33. * })();
  34. *
  35. * @returns {INode}
  36. */
  37. protected getNodeStructure (): INode {
  38. return NodeUtils.getBlockStatementNodeByIndex(
  39. esprima.parse(`
  40. (function () {
  41. var _ = '(\u0004\u0006\u0003\u0005[]' + '["filter"]["\u0007tructor"]' + '("return this")()' + '.' + '\u0003;\u0006\u0002\u0005\u0004};' + '_\u0003.log\u0001.in' + 'fo\u0001.' + 'war' + 'n\u0001.er' + 'r' + 'or\u0001})();' + '\u0001\u0005_\u0002;' + '_\u0003\u0002function' + '\u0003\u0007ole\u0004\u0002 ()' + '{\u0005 = \u0006var ' + '_\u0007cons',
  42. Y,
  43. $;
  44. for (Y in $ = "\u0007\u0006\u0005\u0004\u0003\u0002\u0001") {
  45. var arr = _.split($[Y]);
  46. _ = arr.join(arr.pop());
  47. }
  48. []["filter"]["constructor"](_)();
  49. })()
  50. `)
  51. );
  52. }
  53. }