{"version":3,"file":"index.cjs","sources":["../src/compile.js","../src/languages.js","../src/Node.js","../src/parse.js","../src/index.js"],"sourcesContent":["const compile = (cst, options = {}) => {\n  const keepProtected = options.safe === true || options.keepProtected === true;\n  let firstSeen = false;\n\n  const walk = (node, parent) => {\n    let output = '';\n    let inner;\n    let lines;\n\n    for (const child of node.nodes) {\n      switch (child.type) {\n        case 'block':\n          if (options.first && firstSeen === true) {\n            output += walk(child, node);\n            break;\n          }\n\n          if (options.preserveNewlines === true) {\n            inner = walk(child, node);\n            lines = inner.split('\\n');\n            output += '\\n'.repeat(lines.length - 1);\n            break;\n          }\n\n          if (keepProtected === true && child.protected === true) {\n            output += walk(child, node);\n            break;\n          }\n\n          firstSeen = true;\n          break;\n        case 'line':\n          if (options.first && firstSeen === true) {\n            output += child.value;\n            break;\n          }\n\n          if (keepProtected === true && child.protected === true) {\n            output += child.value;\n          }\n\n          firstSeen = true;\n          break;\n        case 'open':\n        case 'close':\n        case 'text':\n        case 'newline':\n        default: {\n          output += child.value || '';\n          break;\n        }\n      }\n    }\n\n    return output;\n  };\n\n  return walk(cst);\n};\n\nexport default compile;\n","export const ada = { LINE_REGEX: /^--.*/ };\nexport const apl = { LINE_REGEX: /^⍝.*/ };\n\nexport const applescript = {\n  BLOCK_OPEN_REGEX: /^\\(\\*/,\n  BLOCK_CLOSE_REGEX: /^\\*\\)/\n};\n\nexport const csharp = {\n  LINE_REGEX: /^\\/\\/.*/\n};\n\nexport const haskell = {\n  BLOCK_OPEN_REGEX: /^\\{-/,\n  BLOCK_CLOSE_REGEX: /^-\\}/,\n  LINE_REGEX: /^--.*/\n};\n\nexport const javascript = {\n  BLOCK_OPEN_REGEX: /^\\/\\*\\*?(!?)/,\n  BLOCK_CLOSE_REGEX: /^\\*\\/(\\n?)/,\n  LINE_REGEX: /^\\/\\/(!?).*/\n};\n\nexport const lua = {\n  BLOCK_OPEN_REGEX: /^--\\[\\[/,\n  BLOCK_CLOSE_REGEX: /^\\]\\]/,\n  LINE_REGEX: /^--.*/\n};\n\nexport const matlab = {\n  BLOCK_OPEN_REGEX: /^%{/,\n  BLOCK_CLOSE_REGEX: /^%}/,\n  LINE_REGEX: /^%.*/\n};\n\nexport const perl = {\n  LINE_REGEX: /^#.*/\n};\n\nexport const php = {\n  ...javascript,\n  LINE_REGEX: /^(#|\\/\\/).*?(?=\\?>|\\n)/\n};\n\nexport const python = {\n  BLOCK_OPEN_REGEX: /^\"\"\"/,\n  BLOCK_CLOSE_REGEX: /^\"\"\"/,\n  LINE_REGEX: /^#.*/\n};\n\nexport const ruby = {\n  BLOCK_OPEN_REGEX: /^=begin/,\n  BLOCK_CLOSE_REGEX: /^=end/,\n  LINE_REGEX: /^#.*/\n};\n\nexport const shebang = {\n  LINE_REGEX: /^#!.*/\n};\n\nexport const hashbang = shebang;\n\nexport const c = javascript;\nexport const css = javascript;\nexport const java = javascript;\nexport const js = javascript;\nexport const less = javascript;\nexport const pascal = applescript;\nexport const ocaml = applescript;\nexport const sass = javascript;\nexport const sql = ada;\nexport const swift = javascript;\nexport const ts = javascript;\nexport const typscript = javascript;\n","class Node {\n  constructor(node) {\n    this.type = node.type;\n    if (node.value) this.value = node.value;\n    if (node.match) this.match = node.match;\n    this.newline = node.newline || '';\n  }\n  get protected() {\n    return Boolean(this.match) && this.match[1] === '!';\n  }\n}\n\nclass Block extends Node {\n  constructor(node) {\n    super(node);\n    this.nodes = node.nodes || [];\n  }\n  push(node) {\n    this.nodes.push(node);\n  }\n  get protected() {\n    return this.nodes.length > 0 && this.nodes[0].protected === true;\n  }\n}\n\nexport { Node, Block };\n","import * as languages from './languages';\n\nimport { Block, Node } from './Node';\n\nconst constants = {\n  ESCAPED_CHAR_REGEX: /^\\\\./,\n  QUOTED_STRING_REGEX: /^(['\"`])((?:\\\\\\1|[^\\1])+?)(\\1)/,\n  NEWLINE_REGEX: /^\\r*\\n/\n};\n\nconst parse = (input, options = {}) => {\n  if (typeof input !== 'string') {\n    throw new TypeError('Expected input to be a string');\n  }\n\n  const cst = new Block({ type: 'root', nodes: [] });\n  const stack = [cst];\n  const name = (options.language || 'javascript').toLowerCase();\n  const lang = languages[name];\n\n  if (typeof lang === 'undefined') {\n    throw new Error(`Language \"${name}\" is not supported by strip-comments`);\n  }\n\n  const { LINE_REGEX, BLOCK_OPEN_REGEX, BLOCK_CLOSE_REGEX } = lang;\n  let block = cst;\n  let remaining = input;\n  let token;\n  let prev;\n\n  const source = [BLOCK_OPEN_REGEX, BLOCK_CLOSE_REGEX].filter(Boolean);\n  let tripleQuotes = false;\n\n  if (source.every((regex) => regex.source === '^\"\"\"')) {\n    tripleQuotes = true;\n  }\n\n  /**\n   * Helpers\n   */\n\n  const consume = (value = remaining[0] || '') => {\n    remaining = remaining.slice(value.length);\n    return value;\n  };\n\n  const scan = (regex, type = 'text') => {\n    const match = regex.exec(remaining);\n    if (match) {\n      consume(match[0]);\n      return { type, value: match[0], match };\n    }\n  };\n\n  const push = (node) => {\n    if (prev && prev.type === 'text' && node.type === 'text') {\n      prev.value += node.value;\n      return;\n    }\n    block.push(node);\n    if (node.nodes) {\n      stack.push(node);\n      block = node;\n    }\n    prev = node;\n  };\n\n  const pop = () => {\n    if (block.type === 'root') {\n      throw new SyntaxError('Unclosed block comment');\n    }\n    stack.pop();\n    block = stack[stack.length - 1];\n  };\n\n  /**\n   * Parse input string\n   */\n\n  while (remaining !== '') {\n    // escaped characters\n    if ((token = scan(constants.ESCAPED_CHAR_REGEX, 'text'))) {\n      push(new Node(token));\n      continue;\n    }\n\n    // quoted strings\n    if (\n      block.type !== 'block' &&\n      // @ts-ignore\n      (!prev || !/\\w$/.test(prev.value)) &&\n      !(tripleQuotes && remaining.startsWith('\"\"\"'))\n    ) {\n      if ((token = scan(constants.QUOTED_STRING_REGEX, 'text'))) {\n        push(new Node(token));\n        continue;\n      }\n    }\n\n    // newlines\n    if ((token = scan(constants.NEWLINE_REGEX, 'newline'))) {\n      push(new Node(token));\n      continue;\n    }\n\n    // block comment open\n    if (\n      BLOCK_OPEN_REGEX &&\n      options.block &&\n      !(tripleQuotes && block.type === 'block')\n    ) {\n      if ((token = scan(BLOCK_OPEN_REGEX, 'open'))) {\n        push(new Block({ type: 'block' }));\n        push(new Node(token));\n        continue;\n      }\n    }\n\n    // block comment close\n    if (BLOCK_CLOSE_REGEX && block.type === 'block' && options.block) {\n      if ((token = scan(BLOCK_CLOSE_REGEX, 'close'))) {\n        token.newline = token.match[1] || '';\n        push(new Node(token));\n        pop();\n        continue;\n      }\n    }\n\n    // line comment\n    if (LINE_REGEX && block.type !== 'block' && options.line) {\n      if ((token = scan(LINE_REGEX, 'line'))) {\n        push(new Node(token));\n        continue;\n      }\n    }\n\n    // Plain text (skip \"C\" since some languages use \"C\" to start comments)\n    if ((token = scan(/^[a-zABD-Z0-9\\t ]+/, 'text'))) {\n      push(new Node(token));\n      continue;\n    }\n\n    push(new Node({ type: 'text', value: consume(remaining[0]) }));\n  }\n\n  return cst;\n};\n\nexport default parse;\n","/*!\n * strip-comments <https://github.com/jonschlinkert/strip-comments>\n * Copyright (c) 2014-present, Jon Schlinkert.\n * Released under the MIT License.\n */\n\nimport compile from './compile';\nimport parse from './parse';\n\n/**\n * Strip all code comments from the given `input`, including protected\n * comments that start with `!`, unless disabled by setting `options.keepProtected`\n * to true.\n *\n * ```js\n * const str = strip('const foo = \"bar\";// this is a comment\\n /* me too *\\/');\n * console.log(str);\n * // => 'const foo = \"bar\";'\n * ```\n * @name  strip\n * @param  {String | number} `input` string from which to strip comments\n * @param  {Object} `options` optional options, passed to [extract-comments][extract-comments]\n * @option {Boolean} [options] `line` if `false` strip only block comments, default `true`\n * @option {Boolean} [options] `block` if `false` strip only line comments, default `true`\n * @option {Boolean} [options] `keepProtected` Keep ignored comments (e.g. `/*!` and `//!`)\n * @option {Boolean} [options] `preserveNewlines` Preserve newlines after comments are stripped\n * @return {String} modified input\n * @api public\n */\n\nexport const strip = (input, options) => {\n  const opts = { ...options, block: true, line: true };\n  return compile(parse(input, opts), opts);\n};\n\n/**\n * Strip only block comments.\n *\n * ```js\n * const strip = require('..');\n * const str = strip.block('const foo = \"bar\";// this is a comment\\n /* me too *\\/');\n * console.log(str);\n * // => 'const foo = \"bar\";// this is a comment'\n * ```\n * @name  .block\n * @param  {String | number} `input` string from which to strip comments\n * @param  {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)\n * @return {String} modified string\n * @api public\n */\n\nexport const block = (input, options) => {\n  const opts = { ...options, block: true };\n  return compile(parse(input, opts), opts);\n};\n\n/**\n * Strip only line comments.\n *\n * ```js\n * const str = strip.line('const foo = \"bar\";// this is a comment\\n /* me too *\\/');\n * console.log(str);\n * // => 'const foo = \"bar\";\\n/* me too *\\/'\n * ```\n * @name  .line\n * @param  {String | number} `input` string from which to strip comments\n * @param  {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `//!`)\n * @return {String} modified string\n * @api public\n */\n\nexport const line = (input, options) => {\n  const opts = { ...options, line: true };\n  return compile(parse(input, opts), opts);\n};\n\n/**\n * Strip the first comment from the given `input`. Or, if `opts.keepProtected` is true,\n * the first non-protected comment will be stripped.\n *\n * ```js\n * const output = strip.first(input, { keepProtected: true });\n * console.log(output);\n * // => '//! first comment\\nfoo; '\n * ```\n * @name .first\n * @param {String} `input`\n * @param {Object} `options` pass `opts.keepProtected: true` to keep comments with `!`\n * @return {String}\n * @api public\n */\n\nexport const first = (input, options) => {\n  const opts = { ...options, block: true, line: true, first: true };\n  return compile(parse(input, opts), opts);\n};\n\n/**\n * Parses a string and returns a basic CST (Concrete Syntax Tree).\n *\n * ```js\n * const strip = require('..');\n * const str = strip.block('const foo = \"bar\";// this is a comment\\n /* me too *\\/');\n * console.log(str);\n * // => 'const foo = \"bar\";// this is a comment'\n * ```\n * @name  .block\n * @param  {String} `input` string from which to strip comments\n * @param  {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)\n * @return {String} modified string\n * @api public\n */\n\nexport { parse };\n"],"names":["compile","cst","options","keepProtected","safe","firstSeen","walk","node","parent","lines","_step","output","_iterator","_createForOfIteratorHelperLoose","nodes","done","child","value","type","first","preserveNewlines","split","repeat","length","ada","LINE_REGEX","applescript","BLOCK_OPEN_REGEX","BLOCK_CLOSE_REGEX","javascript","shebang","_extends","Node","this","match","newline","Block","_createClass","key","get","Boolean","_Node","_this","call","push","parse","input","TypeError","name","language","toLowerCase","lang","languages","token","block","filter","every","regex","source","tripleQuotes","consume","remaining","slice","scan","exec","prev","stack","pop","SyntaxError","constants","test","startsWith","line","opts"],"mappings":"++BAAA,IAAMA,EAAU,SAACC,EAAKC,QAAAA,IAAAA,IAAAA,EAAU,CAAA,GAC9B,IAAMC,GAAiC,IAAjBD,EAAQE,OAA2C,IAA1BF,EAAQC,cACnDE,GAAY,EAuDhB,OArDa,SAAPC,EAAQC,EAAMC,GAKlB,IAJA,IAEIC,EAE4BC,EAJ5BC,EAAS,GAIbC,2qBAAAC,CAAoBN,EAAKO,SAAOJ,EAAAE,KAAAG,MAAA,CAAA,IAAhBC,EAAAN,EAAAO,MACd,OAAQD,EAAME,MACZ,IAAK,QACH,GAAIhB,EAAQiB,QAAuB,IAAdd,EAAoB,CACvCM,GAAUL,EAAKU,GACf,KACF,CAEA,IAAiC,IAA7Bd,EAAQkB,iBAA2B,CAErCX,EADQH,EAAKU,GACCK,MAAM,MACpBV,GAAU,KAAKW,OAAOb,EAAMc,OAAS,GACrC,KACF,CAEA,IAAsB,IAAlBpB,IAA8C,IAApBa,EAAe,UAAW,CACtDL,GAAUL,EAAKU,GACf,KACF,CAEAX,GAAY,EACZ,MACF,IAAK,OACH,GAAIH,EAAQiB,QAAuB,IAAdd,EAAoB,CACvCM,GAAUK,EAAMC,MAChB,KACF,EAEsB,IAAlBd,IAA8C,IAApBa,EAAe,YAC3CL,GAAUK,EAAMC,OAGlBZ,GAAY,EACZ,MAKF,QACEM,GAAUK,EAAMC,OAAS,GAI/B,CAEA,OAAON,CACT,CAEOL,CAAKL,EACd,EC1DauB,EAA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