Skip to content

Commit

Permalink
Merge pull request #315 from github/new_js
Browse files Browse the repository at this point in the history
Add JavaScript
  • Loading branch information
BekaValentine authored Oct 19, 2023
2 parents ac9628f + 169c94f commit 16ee564
Show file tree
Hide file tree
Showing 208 changed files with 9,125 additions and 17 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/publish-tree-sitter-stack-graphs-javascript.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Publish tree-sitter-stack-graphs-javascript release

on:
push:
tags:
- tree-sitter-stack-graphs-javascript-v*

jobs:
publish-crate:
runs-on: ubuntu-latest
env:
CARGO_TERM_COLOR: always
CARGO_INCREMENTAL: 0
CRATE_DIR: './languages/tree-sitter-stack-graphs-javascript'
steps:
- name: Install Rust environment
uses: hecrj/setup-rust-action@v1
- name: Checkout repository
uses: actions/checkout@v3
# TODO Verify the crate version matches the tag
- name: Test crate
run: cargo test --all-features
working-directory: ${{ env.CRATE_DIR }}
- name: Verify publish crate
run: cargo publish --dry-run
working-directory: ${{ env.CRATE_DIR }}
- name: Publish crate
run: cargo publish
working-directory: ${{ env.CRATE_DIR }}
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
create-release:
needs: publish-crate
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Create GitHub release
uses: ncipollo/release-action@v1
with:
body: |
Find more info on all releases at https://crates.io/crates/tree-sitter-stack-graphs-javascript.
token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ Cargo.lock
/.crates.toml
/.crates2.json
/bin/

# MacOS stuff
.DS_Store
5 changes: 5 additions & 0 deletions languages/tree-sitter-stack-graphs-javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.html
/Cargo.lock
/node_modules
/package-lock.json
/target
6 changes: 6 additions & 0 deletions languages/tree-sitter-stack-graphs-javascript/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog for tree-sitter-stack-graphs-javascript

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
39 changes: 39 additions & 0 deletions languages/tree-sitter-stack-graphs-javascript/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "tree-sitter-stack-graphs-javascript"
version = "0.1.0"
description = "Stack graphs definition for JavaScript using tree-sitter-javascript"
readme = "README.md"
keywords = ["tree-sitter", "stack-graphs", "javascript"]
authors = ["Beka Valentine <[email protected]>"]
license = "MIT OR Apache-2.0"
edition = "2018"

[[bin]]
name = "tree-sitter-stack-graphs-javascript"
path = "rust/bin.rs"
required-features = ["cli"]

[lib]
path = "rust/lib.rs"
test = false

[[test]]
name = "test"
path = "rust/test.rs"
harness = false

[features]
cli = ["anyhow", "clap", "tree-sitter-stack-graphs/cli"]

[dependencies]
anyhow = { version = "1.0", optional = true }
clap = { version = "4", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
stack-graphs = { version = "0.12", path = "../../stack-graphs" }
tree-sitter-stack-graphs = { version = "0.7", path = "../../tree-sitter-stack-graphs" }
tree-sitter-javascript = { git = "https://github.com/tree-sitter/tree-sitter-javascript", rev = "5720b249490b3c17245ba772f6be4a43edb4e3b7" }

[dev-dependencies]
anyhow = "1.0"
tree-sitter-stack-graphs = { version = "0.7", path = "../../tree-sitter-stack-graphs", features = ["cli"] }
98 changes: 98 additions & 0 deletions languages/tree-sitter-stack-graphs-javascript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# tree-sitter-stack-graphs definition for JavaScript

This project defines tree-sitter-stack-graphs rules for JavaScript using the [tree-sitter-javascript][] grammar.

[tree-sitter-javascript]: https://crates.io/crates/tree-sitter-javascript

## Usage

To use this library, add the following to your `Cargo.toml`:

``` toml
[dependencies]
tree-sitter-stack-graphs-javascript = "0.0.1"
```

Check out our [documentation](https://docs.rs/tree-sitter-stack-graphs-javascript/*/) for more details on how to use this library.

## Command-line Program

The command-line program for `tree-sitter-stack-graphs-javascript` lets you do stack graph based analysis and lookup from the command line.

Install the program using `cargo install` as follows:

``` sh
$ cargo install --features cli tree-sitter-stack-graphs-javascript
$ tree-sitter-stack-graphs-javascript --help
```

## Development

The project is written in Rust, and requires a recent version installed. Rust can be installed and updated using [rustup][].

[rustup]: https://rustup.rs/

The project is organized as follows:

- The stack graph rules are defined in `src/stack-graphs.tsg`.
- Builtins sources and configuration are defined in `src/builtins.js` and `builtins.cfg` respectively.
- Tests are put into the `test` directory.

### Building and Running Tests

Build the project by running:

``` sh
$ cargo build
```

Run the tests as follows:

``` sh
$ cargo test
```

The project consists of a library and a CLI. By default, running `cargo` only applies to the library. To run `cargo` commands on the CLI as well, add `--features cli` or `--all-features`.

Run the CLI from source as follows:

``` sh
$ cargo run --features cli -- ARGS
```

Sources are formatted using the standard Rust formatted, which is applied by running:

``` sh
$ cargo fmt
```

### Writing TSG

The stack graph rules are written in [tree-sitter-graph][]. Checkout the [examples][],
which contain self-contained TSG rules for specific language features. A VSCode
[extension][] is available that provides syntax highlighting for TSG files.

[tree-sitter-graph]: https://github.com/tree-sitter/tree-sitter-graph
[examples]: https://github.com/github/stack-graphs/blob/main/tree-sitter-stack-graphs/examples/
[extension]: https://marketplace.visualstudio.com/items?itemName=tree-sitter.tree-sitter-graph

Parse and test a single file by executing the following commands:

``` sh
$ cargo run --features cli -- parse FILES...
$ cargo run --features cli -- test TESTFILES...
```

Generate a visualization to debug failing tests by passing the `-V` flag:

``` sh
$ cargo run --features cli -- test -V TESTFILES...
```

To generate the visualization regardless of test outcome, execute:

``` sh
$ cargo run --features cli -- test -V --output-mode=always TESTFILES...
```

Go to https://crates.io/crates/tree-sitter-stack-graphs for links to examples and documentation.
33 changes: 33 additions & 0 deletions languages/tree-sitter-stack-graphs-javascript/rust/bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2023, stack-graphs authors.
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
// ------------------------------------------------------------------------------------------------

use anyhow::anyhow;
use clap::Parser;
use tree_sitter_stack_graphs::cli::database::default_user_database_path_for_crate;
use tree_sitter_stack_graphs::cli::provided_languages::Subcommands;
use tree_sitter_stack_graphs::NoCancellation;

fn main() -> anyhow::Result<()> {
let lc = match tree_sitter_stack_graphs_javascript::try_language_configuration(&NoCancellation)
{
Ok(lc) => lc,
Err(err) => {
eprintln!("{}", err.display_pretty());
return Err(anyhow!("Language configuration error"));
}
};
let cli = Cli::parse();
let default_db_path = default_user_database_path_for_crate(env!("CARGO_PKG_NAME"))?;
cli.subcommand.run(default_db_path, vec![lc])
}

#[derive(Parser)]
#[clap(about, version)]
pub struct Cli {
#[clap(subcommand)]
subcommand: Subcommands,
}
56 changes: 56 additions & 0 deletions languages/tree-sitter-stack-graphs-javascript/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2023, stack-graphs authors.
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
// ------------------------------------------------------------------------------------------------

use tree_sitter_stack_graphs::loader::FileAnalyzers;
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
use tree_sitter_stack_graphs::loader::LoadError;
use tree_sitter_stack_graphs::CancellationFlag;

use crate::npm_package::NpmPackageAnalyzer;

mod npm_package;
mod util;

/// The stack graphs tsg source for this language.
pub const STACK_GRAPHS_TSG_PATH: &str = "src/stack-graphs.tsg";
/// The stack graphs tsg source for this language.
pub const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");

/// The stack graphs builtins configuration for this language.
pub const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg");
/// The stack graphs builtins path for this language
pub const STACK_GRAPHS_BUILTINS_PATH: &str = "src/builtins.js";
/// The stack graphs builtins source for this language.
pub const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.js");

/// The name of the file path global variable.
pub const FILE_PATH_VAR: &str = "FILE_PATH";
pub const PROJECT_NAME_VAR: &str = "PROJECT_NAME";

pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
try_language_configuration(cancellation_flag).unwrap_or_else(|err| panic!("{}", err))
}

pub fn try_language_configuration(
cancellation_flag: &dyn CancellationFlag,
) -> Result<LanguageConfiguration, LoadError> {
LanguageConfiguration::from_sources(
tree_sitter_javascript::language(),
Some(String::from("source.js")),
None,
vec![String::from("js")],
STACK_GRAPHS_TSG_PATH.into(),
STACK_GRAPHS_TSG_SOURCE,
Some((
STACK_GRAPHS_BUILTINS_PATH.into(),
STACK_GRAPHS_BUILTINS_SOURCE,
)),
Some(STACK_GRAPHS_BUILTINS_CONFIG),
FileAnalyzers::new().add("package.json".to_string(), NpmPackageAnalyzer {}),
cancellation_flag,
)
}
Loading

0 comments on commit 16ee564

Please sign in to comment.