All files / codemods styled-flowfixme.js

100% Statements 26/26
91.67% Branches 11/12
100% Functions 7/7
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                      4x     4x 3x       3x 3x     1x       5x 5x 5x     1x 5x 5x   5x 4x 4x 3x 3x 3x     3x     3x 3x 3x         5x     1x  
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
/* global module */
 
// @flow
 
function getStyledImportName(j, root) {
  const baseImports = root.find(j.ImportDeclaration, {
    source: {value: 'baseui'},
  });
  if (baseImports.length) {
    const styledImports = baseImports.find(j.ImportSpecifier, {
      imported: {name: 'styled'},
    });
 
    Eif (styledImports.length) {
      return styledImports.get(0).node.local.name;
    }
  }
  return null;
}
 
function containsFlowComment(j, root) {
  const getBodyNode = () => root.find(j.Program).get('body', 0).node;
  const comments = getBodyNode().comments || [];
  return comments.filter(e => e.value.indexOf('@flow') !== -1).length > 0;
}
 
module.exports = function(file, api, options) {
  const j = api.jscodeshift;
  const root = j(file.source);
 
  if (containsFlowComment(j, root)) {
    const styledImportName = getStyledImportName(j, root);
    if (styledImportName) {
      const comment = j.commentLine(' $FlowFixMe', true, false);
      const styledComponents = root.find(j.VariableDeclaration).filter(path => {
        const calls = j(path).find(j.CallExpression, {
          callee: {name: styledImportName},
        });
        return Boolean(calls.length);
      });
 
      styledComponents.forEach(path => {
        path.node.comments = path.node.comments || [];
        path.node.comments.push(comment);
      });
    }
  }
 
  return root.toSource();
};
 
module.exports.parser = 'flow';