<commit_before>
var at = require('../../ast').css;

//
// export handler
//

module.exports = function(flow){
  var packages = flow.css.packages;
  var fconsole = flow.console;

  // process files in reverse order
  for (var i = packages.length - 1, file; file = packages[i]; i--)
  {
    fconsole.start(file.relOutputFilename);

    buildFile(file, flow, file.theme);

    fconsole.endl();
  }
}

module.exports.handlerName = '[css] Linear files';

//
// main part
//

function relpath(file){
  return file.relPath;
}

function buildFile(file, flow, theme, context){
  if (!context)
    context = [];
  else
  {
    if (context.indexOf(file) != -1)
    {
      flow.console.log('# [WARN] Recursion ' + context.map(relpath).join(' -> ') + ' -> ' + file.relPath);
      return [
        {}, 'stylesheet',
        [{}, 's', ''],
        at.packComment(' [WARN] Recursion: ' + context.map(relpath).join(' -> ') + ' -> ' + file.relPath + ' '),
        [{}, 's', '\n\n']
      ];
    }
  }

  if (file.used && file.used[theme])
  {
    var msg = '[DUP] ' + (file.filename ? file.relpath : '[inline style]') + ' ignored as already used';
    flow.console.log('# ' + msg);
    return [
      {}, 'stylesheet',
      [{}, 's', ''],
      at.packComment(' ' + msg + ' '),
      [{}, 's', '\n\n']
    ];
  }

  if (!file.used)
    file.used = {};
  file.used[theme] = true;

  context.push(file);

  for (var i = file.imports.length - 1, importToken; importToken = file.imports[i]; i--)
  {
    var injection = buildFile(importToken.file, flow, theme, context).slice(2);

    if (importToken.media.length)
    {
      injection.unshift([{}, 's', '\n']);
      injection.push([{}, 's', '\n']);
      injection = [
        [{}, 'atruler',
          [{}, 'atkeyword',
            [{}, 'ident', 'media' ]
          ],
          [{}, 'atrulerq'].concat(importToken.media),
          [{}, 'atrulers'].concat(injection)
        ]
      ];
    }

    // inject
    injection.unshift(importToken.pos, 1,
      at.packComment(importToken.code),
      [{}, 's', '\n\n']
    );
    importToken.token.splice.apply(importToken.token, injection);
  }

  context.pop();

  return file.ast;
}
<commit_msg>clone ast if it use for more than one package, to avoid crash on compression<commit_after>
var at = require('../../ast').css;

//
// export handler
//

module.exports = function(flow){
  var packages = flow.css.packages;
  var fconsole = flow.console;

  // process files in reverse order
  for (var i = packages.length - 1, file; file = packages[i]; i--)
  {
    fconsole.start(file.relOutputFilename);

    buildFile(file, flow, file.theme);

    fconsole.endl();
  }
}

module.exports.handlerName = '[css] Linear files';

//
// main part
//

function relpath(file){
  return file.relPath;
}

function buildFile(file, flow, theme, context){
  if (!context)
    context = [];
  else
  {
    if (context.indexOf(file) != -1)
    {
      flow.console.log('# [WARN] Recursion ' + context.map(relpath).join(' -> ') + ' -> ' + file.relPath);
      return [
        {}, 'stylesheet',
        [{}, 's', ''],
        at.packComment(' [WARN] Recursion: ' + context.map(relpath).join(' -> ') + ' -> ' + file.relPath + ' '),
        [{}, 's', '\n\n']
      ];
    }
  }

  if (file.used && file.used[theme])
  {
    var msg = '[DUP] ' + (file.filename ? file.relpath : '[inline style]') + ' ignored as already used';
    flow.console.log('# ' + msg);
    return [
      {}, 'stylesheet',
      [{}, 's', ''],
      at.packComment(' ' + msg + ' '),
      [{}, 's', '\n\n']
    ];
  }

  if (!file.used)
    file.used = {};
  file.used[theme] = true;

  context.push(file);

  for (var i = file.imports.length - 1, importToken; importToken = file.imports[i]; i--)
  {
    var injection = buildFile(importToken.file, flow, theme, context).slice(2);

    if (importToken.media.length)
    {
      injection.unshift([{}, 's', '\n']);
      injection.push([{}, 's', '\n']);
      injection = [
        [{}, 'atruler',
          [{}, 'atkeyword',
            [{}, 'ident', 'media' ]
          ],
          [{}, 'atrulerq'].concat(importToken.media),
          [{}, 'atrulers'].concat(injection)
        ]
      ];
    }

    // inject
    injection.unshift(importToken.pos, 1,
      at.packComment(importToken.code),
      [{}, 's', '\n\n']
    );
    importToken.token.splice.apply(importToken.token, injection);
  }

  context.pop();

  // if used before, make a clone
  if (file.ast.used)
    return JSON.parse(JSON.stringify(file.ast)); // TODO: change for deep clone

  file.ast.used = true;
  return file.ast;
}

{'fraction_non_alphanumeric': 0.19686192468619246, 'fraction_numerical': 0.0020920502092050207, 'mean_word_length': 2.037484116899619, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '7022620', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1572, 'n_words': 311}