<commit_before>import CurlParser, { CurlFileReference } from './CurlParser'

@registerImporter
class CurlImporter {
  static identifier = 'com.luckymarmot.PawExtensions.cURLImporter';
  static title = 'cURL Importer';

  constructor() {
    this.options = {}
  }

  _resolveFileReference(value) {
    if (value instanceof CurlFileReference) {
      const dv = new DynamicValue("com.luckymarmot.FileContentDynamicValue", {})
      return new DynamicString(dv)
    }
    return value
  }

  _escapeSequenceDynamicValue(seq) {
    let escapeSequence = ''
    for (let i = 0; i < seq.length; i++) {
      const char = seq[i]
      if (char === "\n") { escapeSequence += "\\n" }
      else if (char === "\r") { escapeSequence += "\\r" }
      else if (char === "\t") { escapeSequence += "\\t" }
      else {
        let hexChar = char.charCodeAt(0).toString(16)
        if (hexChar.length === 1) {
          hexChar = '0' + hexChar
        }
        escapeSequence += "\\x" + hexChar
      }
    };
    return new DynamicValue("com.luckymarmot.EscapeSequenceDynamicValue", {
      escapeSequence: escapeSequence
    })
  }

  _toDynamicString(string, defaultToEmpty, resolveFileRefs) {
    if (!string) {
      if (defaultToEmpty) {
        return new DynamicString('')
      }
      return null
    }

    // resolve file references
    if (resolveFileRefs) {
      const resolvedString = this._resolveFileReference(string)
      if ("string" !== typeof resolvedString) {
        return resolvedString
      }
    }

    // split around special characters
    const re = /([^\x00-\x1f]+)|([\x00-\x1f]+)/gm
    let components = []
    let m
    while ((m = re.exec(string)) !== null) {
      if (m[1]) {
        components.push(m[1])
      }
      else {
        components.push(this._escapeSequenceDynamicValue(m[2]))
      }
    }
    return new DynamicString(...components)
  }

  _importPawRequest(context, curlRequest) {
    const headers = curlRequest.get('headers')
    const auth = curlRequest.get('auth')
    const bodyType = curlRequest.get('bodyType')
    const body = curlRequest.get('body')
    const contentType = headers.get('Content-Type')
    const timeout = curlRequest.get('timeout')

    // url + method
    let pawRequest = context.createRequest(
      "cURL",
      curlRequest.get('method'),
      this._toDynamicString(curlRequest.get('url'), true, true),
    )

    // set to parent + order
    if (this.options.parent) {
      this.options.parent.appendChild(pawRequest)
    }
    if (this.options.order) {
      pawRequest.order = this.options.order
      this.options.order += 1
    }
<commit_msg>Fix this.options.parent when options is null<commit_after>import CurlParser, { CurlFileReference } from './CurlParser'

@registerImporter
class CurlImporter {
  static identifier = 'com.luckymarmot.PawExtensions.cURLImporter';
  static title = 'cURL Importer';

  constructor() {
    this.options = {}
  }

  _resolveFileReference(value) {
    if (value instanceof CurlFileReference) {
      const dv = new DynamicValue("com.luckymarmot.FileContentDynamicValue", {})
      return new DynamicString(dv)
    }
    return value
  }

  _escapeSequenceDynamicValue(seq) {
    let escapeSequence = ''
    for (let i = 0; i < seq.length; i++) {
      const char = seq[i]
      if (char === "\n") { escapeSequence += "\\n" }
      else if (char === "\r") { escapeSequence += "\\r" }
      else if (char === "\t") { escapeSequence += "\\t" }
      else {
        let hexChar = char.charCodeAt(0).toString(16)
        if (hexChar.length === 1) {
          hexChar = '0' + hexChar
        }
        escapeSequence += "\\x" + hexChar
      }
    };
    return new DynamicValue("com.luckymarmot.EscapeSequenceDynamicValue", {
      escapeSequence: escapeSequence
    })
  }

  _toDynamicString(string, defaultToEmpty, resolveFileRefs) {
    if (!string) {
      if (defaultToEmpty) {
        return new DynamicString('')
      }
      return null
    }

    // resolve file references
    if (resolveFileRefs) {
      const resolvedString = this._resolveFileReference(string)
      if ("string" !== typeof resolvedString) {
        return resolvedString
      }
    }

    // split around special characters
    const re = /([^\x00-\x1f]+)|([\x00-\x1f]+)/gm
    let components = []
    let m
    while ((m = re.exec(string)) !== null) {
      if (m[1]) {
        components.push(m[1])
      }
      else {
        components.push(this._escapeSequenceDynamicValue(m[2]))
      }
    }
    return new DynamicString(...components)
  }

  _importPawRequest(context, curlRequest) {
    const headers = curlRequest.get('headers')
    const auth = curlRequest.get('auth')
    const bodyType = curlRequest.get('bodyType')
    const body = curlRequest.get('body')
    const contentType = headers.get('Content-Type')
    const timeout = curlRequest.get('timeout')

    // url + method
    let pawRequest = context.createRequest(
      "cURL",
      curlRequest.get('method'),
      this._toDynamicString(curlRequest.get('url'), true, true),
    )

    // set to parent + order
    if (this.options && this.options.parent) {
      this.options.parent.appendChild(pawRequest)
    }
    if (this.options && this.options.order) {
      pawRequest.order = this.options.order
      this.options.order += 1
    }

{'fraction_non_alphanumeric': 0.13652649443711107, 'fraction_numerical': 0.006034320196115406, 'mean_word_length': 2.5934959349593494, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1262135', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1607, 'n_words': 349}