Giter Site home page Giter Site logo

jsx-info's Issues

Add option to return prop values

I'm using this package with jest to check that all used strings by react-intl are in file with localization.

So, I'm using this package to find all <FormattedMessage id="..." defaultMessage="..."> component usage, extract it by id and compare that this id is exist in all localized files.

Unfortunately, analyze() method doesn't include prop values and almost methods are private, so I'm using too dirty way to use your package with rewire package :)

// All methods are private, so rewire is needed to access into them
const jsxInfoApi = rewire('jsx-info/dist/src/api');

const parseJsxInfo = jsxInfoApi.__get__('parse');
const createPropJsxInfo = jsxInfoApi.__get__('createProp');

...

const filenames = await globby([
    'containers/**/*.jsx',
    'components/**/*.jsx',
    'mappings.jsx',
  ], {
    absolute: true,
    onlyFiles: true,
    gitignore: true,
    ignore: [],
    cwd: directory,
  });

  filenames.forEach((filename) => {
    try {
      const code = fs.readFileSync(path.resolve(directory, filename), 'utf8');

      parseJsxInfo(code, {
        babelPlugins: [],
        typescript: filename.endsWith('.tsx') || filename.endsWith('.ts'),
        onlyComponents: [
          'FormattedMessage',
          'CustomHeadingComponent',
          'FormRow',
          'DataRow',
          'InfoRow',
        ],
        onComponent: ({ node }) => {
          node.openingElement.attributes.forEach((propNode) => {
            const propValue = propNode.value.value;
            const propName = createPropJsxInfo(propNode);

            // ... here propName is validated and propValue is used
          });
        },
      });
    } catch (error) {
      throw error;
    }
  });

It covers 99% of my cases, except variables.

What do you think is better?

  1. Update analyze() method to include prop values as well? It might be good, but how to deal with non-string props?
  2. To export private methods?
  3. Keep as is :)

Thanks for this awesome package!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.