Code Monkey home page Code Monkey logo

tree-sitter-example's Introduction

tree-sitter-example

Tree house image

Small and silly parser based on the Tree Sitter

JavaScript parser implementation

What does it do

This parser will make an attempt to find a specified lib require statement and then will find lines in a code for every variable reference (or a particular one if specified) for the object assigned to the require statement.

For Example if you have file test.js in the same folder as the script:

const path = require('path');

module.exports = {
    entry: './src/app.js',
    output: {
        path: path.resolve(__dirname, 'bin'),
        filename: 'app.bundle.js',
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: './index.html',
            template: path.resolve(configDirs.APP_DIR_TEMPLATES, './index.prod.html'),
            inject: true,
            publicPath: path.join(configDirs.PUBLIC_PATH, 'public'),
        }),
        new HtmlWebpackInjector(),
    ],
};

The following command will find path lib resolve calls

./bin/silly-parser -f ./test.js -l path -p resolve

Should produce an output:

[]AllOccurrences of path referencing resolve:
  <line 5, path: path.resolve(__dirname, 'bin'),
  <line 11, template: path.resolve(configDirs.APP_DIR_TEMPLATES, './index.prod.html'),

The following command will find all path lib calls

./bin/silly-parser -f ./test.js -l path

Should produce an output:

[]AllOccurrences of path referencing resolve:
  <line 5, path: path.resolve(__dirname, 'bin'),
  <line 11, template: path.resolve(configDirs.APP_DIR_TEMPLATES, './index.prod.html'),
  <line 13, publicPath: path.join(configDirs.PUBLIC_PATH, 'public'),

Usage:

Usage: silly-parser -f [str] -l [str] -p [str] -v

Options:
      --help      Show help                                            [boolean]
      --version   Show version number                                  [boolean]
  -f, --file      Path to a file                                      [required]
  -l, --libName   Lib name                                            [required]
  -p, --propName  Prop name

Examples:
  silly-parser -f ../examples/example1.js -l fs/promises -p readFile
  will return code lines within example.js for object readFile() files for
  fs/promises lib

Quick start

Install node modules npm install.

$ ./bin/silly-parser -f ./examples/example1.js -l fs/promises -p readFile

For developers

Tests

npm run test

How to run the code without CLI

Install Node dependencies

$ npm install
$ ts-node ./index.js -f ./examples/example1.js -l fs/promises -p readFile -v

Build command

npm run build

Using lib in code instead of CLI

import SillyParser from './sillyParser';

const sourceCode = `
const path = require('path');

module.exports = {
    entry: './src/app.js',
    output: {
        path: path.resolve(__dirname, 'bin'),
        filename: 'app.bundle.js',
    },
};
`;

const parser = new SillyParser({ sourceCode });
const result = parser.findRequireObjectPropertiesLOC('path', 'resolve');

console.log(`[]AllOccurrences of path calling resolve ${result}`);

Limitations

This is a basic implementation and does not take into consideration if variables are reassigned

Example:

'use strict';

const fs = require('fs/promises');
const fs1 = fs;
const app = express();

const router = express.Router();
router.get('/', async (req, res) => {
    const f = await fs1.readFile('/tmp/somefile.txt');
    res.send(f.toString('utf-8'));
});

Calling ./bin/silly-parser -f ../examples/example1.js -l fs/promises -p readFile would yield no results because of the const fs1 = fs; line

Also more tests are required

Also it is not published to NPM. This CLI command cannot be installed using npm install -g

License

MIT license; see LICENSE.

tree-sitter-example's People

Contributors

anvk avatar

Watchers

 avatar

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.