function.d.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Next, ToString } from "./types";
  2. declare const FUNCTION_PREFIXES: {
  3. Function: string;
  4. GeneratorFunction: string;
  5. AsyncFunction: string;
  6. AsyncGeneratorFunction: string;
  7. };
  8. /**
  9. * Track function parser usage.
  10. */
  11. export declare const USED_METHOD_KEY: WeakSet<(...args: unknown[]) => unknown>;
  12. /**
  13. * Stringify a function.
  14. */
  15. export declare const functionToString: ToString;
  16. /**
  17. * Rewrite a stringified function to remove initial indentation.
  18. */
  19. export declare function dedentFunction(fnString: string): string;
  20. /**
  21. * Function parser and stringify.
  22. */
  23. export declare class FunctionParser {
  24. fn: (...args: unknown[]) => unknown;
  25. indent: string;
  26. next: Next;
  27. key?: string | undefined;
  28. fnString: string;
  29. fnType: keyof typeof FUNCTION_PREFIXES;
  30. keyQuote: string | undefined;
  31. keyPrefix: string;
  32. isMethodCandidate: boolean;
  33. pos: number;
  34. hadKeyword: boolean;
  35. constructor(fn: (...args: unknown[]) => unknown, indent: string, next: Next, key?: string | undefined);
  36. stringify(): string;
  37. getPrefix(): string;
  38. tryParse(): string | undefined;
  39. /**
  40. * Attempt to parse the function from the current position by first stripping
  41. * the function's name from the front. This is not a fool-proof method on all
  42. * JavaScript engines, but yields good results on Node.js 4 (and slightly
  43. * less good results on Node.js 6 and 8).
  44. */
  45. tryStrippingName(): string | undefined;
  46. /**
  47. * Attempt to advance the parser past the keywords expected to be at the
  48. * start of this function's definition. This method sets `this.hadKeyword`
  49. * based on whether or not a `function` keyword is consumed.
  50. */
  51. tryParsePrefixTokens(): boolean;
  52. /**
  53. * Advance the parser past one element of JavaScript syntax. This could be a
  54. * matched pair of delimiters, like braces or parentheses, or an atomic unit
  55. * like a keyword, variable, or operator. Return a normalized string
  56. * representation of the element parsed--for example, returns '{}' for a
  57. * matched pair of braces. Comments and whitespace are skipped.
  58. *
  59. * (This isn't a full parser, so the token scanning logic used here is as
  60. * simple as it can be. As a consequence, some things that are one token in
  61. * JavaScript, like decimal number literals or most multi-character operators
  62. * like '&&', are split into more than one token here. However, awareness of
  63. * some multi-character sequences like '=>' is necessary, so we match the few
  64. * of them that we care about.)
  65. */
  66. consumeSyntax(wordLikeToken?: string): string | undefined;
  67. consumeSyntaxUntil(startToken: string, endToken: string): string | undefined;
  68. consumeMatch(re: RegExp): RegExpExecArray | null;
  69. /**
  70. * Advance the parser past an arbitrary regular expression. Return `token`,
  71. * or the match object of the regexp.
  72. */
  73. consumeRegExp(re: RegExp, token: string): string | undefined;
  74. /**
  75. * Advance the parser past a template string.
  76. */
  77. consumeTemplate(): "`" | undefined;
  78. /**
  79. * Advance the parser past any whitespace or comments.
  80. */
  81. consumeWhitespace(): void;
  82. }
  83. export {};