tree_sitter_stack_graphs_javascript/
lib.rs

1// -*- coding: utf-8 -*-
2// ------------------------------------------------------------------------------------------------
3// Copyright © 2023, stack-graphs authors.
4// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6// ------------------------------------------------------------------------------------------------
7
8use tree_sitter_stack_graphs::loader::LanguageConfiguration;
9use tree_sitter_stack_graphs::loader::LoadError;
10use tree_sitter_stack_graphs::CancellationFlag;
11
12use crate::npm_package::NpmPackageAnalyzer;
13
14mod npm_package;
15mod util;
16
17/// The stack graphs tsg source for this language.
18pub const STACK_GRAPHS_TSG_PATH: &str = "src/stack-graphs.tsg";
19/// The stack graphs tsg source for this language.
20pub const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");
21
22/// The stack graphs builtins configuration for this language.
23pub const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg");
24/// The stack graphs builtins path for this language
25pub const STACK_GRAPHS_BUILTINS_PATH: &str = "src/builtins.js";
26/// The stack graphs builtins source for this language.
27pub const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.js");
28
29/// The name of the project name global variable
30pub const PROJECT_NAME_VAR: &str = "PROJECT_NAME";
31
32pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
33    try_language_configuration(cancellation_flag).unwrap_or_else(|err| panic!("{}", err))
34}
35
36pub fn try_language_configuration(
37    cancellation_flag: &dyn CancellationFlag,
38) -> Result<LanguageConfiguration, LoadError> {
39    let mut lc = LanguageConfiguration::from_sources(
40        tree_sitter_javascript::LANGUAGE.into(),
41        Some(String::from("source.js")),
42        None,
43        vec![String::from("js")],
44        STACK_GRAPHS_TSG_PATH.into(),
45        STACK_GRAPHS_TSG_SOURCE,
46        Some((
47            STACK_GRAPHS_BUILTINS_PATH.into(),
48            STACK_GRAPHS_BUILTINS_SOURCE,
49        )),
50        Some(STACK_GRAPHS_BUILTINS_CONFIG),
51        cancellation_flag,
52    )?;
53    lc.special_files
54        .add("package.json".to_string(), NpmPackageAnalyzer {});
55    Ok(lc)
56}