1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.addImports = addImports;
7exports.appendContentsInsideDeclarationBlock = appendContentsInsideDeclarationBlock;
8exports.findNewInstanceCodeBlock = findNewInstanceCodeBlock;
9function _commonCodeMod() {
10  const data = require("../utils/commonCodeMod");
11  _commonCodeMod = function () {
12    return data;
13  };
14  return data;
15}
16function _matchBrackets() {
17  const data = require("../utils/matchBrackets");
18  _matchBrackets = function () {
19    return data;
20  };
21  return data;
22}
23/**
24 * Find java or kotlin new class instance code block
25 *
26 * @param contents source contents
27 * @param classDeclaration class declaration or just a class name
28 * @param language 'java' | 'kt'
29 * @returns `CodeBlock` for start/end offset and code block contents
30 */
31function findNewInstanceCodeBlock(contents, classDeclaration, language) {
32  const isJava = language === 'java';
33  let start = isJava ? contents.indexOf(` new ${classDeclaration}(`) : contents.search(new RegExp(` (object\\s*:\\s*)?${classDeclaration}\\(`));
34  if (start < 0) {
35    return null;
36  }
37  // `+ 1` for the prefix space
38  start += 1;
39  let end = (0, _matchBrackets().findMatchingBracketPosition)(contents, '(', start);
40
41  // For anonymous class, should search further to the {} block.
42  // ```java
43  // new Foo() {
44  //   @Override
45  //   protected void interfaceMethod {}
46  // };
47  // ```
48  //
49  // ```kotlin
50  // object : Foo() {
51  //   override fun interfaceMethod {}
52  // }
53  // ```
54  const nextBrace = contents.indexOf('{', end + 1);
55  const isAnonymousClass = nextBrace >= end && !!contents.substring(end + 1, nextBrace).match(/^\s*$/);
56  if (isAnonymousClass) {
57    end = (0, _matchBrackets().findMatchingBracketPosition)(contents, '{', end);
58  }
59  return {
60    start,
61    end,
62    code: contents.substring(start, end + 1)
63  };
64}
65
66/**
67 * Append contents to the end of code declaration block, support class or method declarations.
68 *
69 * @param srcContents source contents
70 * @param declaration class declaration or method declaration
71 * @param insertion code to append
72 * @returns updated contents
73 */
74function appendContentsInsideDeclarationBlock(srcContents, declaration, insertion) {
75  const start = srcContents.search(new RegExp(`\\s*${declaration}.*?[\\(\\{]`));
76  if (start < 0) {
77    throw new Error(`Unable to find code block - declaration[${declaration}]`);
78  }
79  const end = (0, _matchBrackets().findMatchingBracketPosition)(srcContents, '{', start);
80  return (0, _commonCodeMod().insertContentsAtOffset)(srcContents, insertion, end);
81}
82function addImports(source, imports, isJava) {
83  const lines = source.split('\n');
84  const lineIndexWithPackageDeclaration = lines.findIndex(line => line.match(/^package .*;?$/));
85  for (const javaImport of imports) {
86    if (!source.includes(javaImport)) {
87      const importStatement = `import ${javaImport}${isJava ? ';' : ''}`;
88      lines.splice(lineIndexWithPackageDeclaration + 1, 0, importStatement);
89    }
90  }
91  return lines.join('\n');
92}
93//# sourceMappingURL=codeMod.js.map