Skip to content

Commit

Permalink
feat: support Typescript (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps authored Sep 12, 2023
1 parent f4b2713 commit 2d99a5f
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 11 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ serde_json = "1.0.106"
snailquote = "0.3.1"
tree-sitter = "0.20.10"
tree-sitter-go = "0.19.1"
tree-sitter-typescript = "0.20.2"
tree-sitter-rust = "0.20.4"
walkdir = "2.4.0"
colored = "2"
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ With args:

`$ cargo build`

## Limitations
## Supported Languages

Currently only supports parsing Go code.
- Go
- TypeScript

Other language support is planned, but not yet implemented:

- [ ] Python [#43](https://github.com/flipt-io/ffs/issues/43)
- [ ] Java [#44](https://github.com/flipt-io/ffs/issues/44)
- [ ] Javascript/Typescript [#42](https://github.com/flipt-io/ffs/issues/42)
- [ ] Rust [#45](https://github.com/flipt-io/ffs/issues/45)

## Contributing
Expand All @@ -85,12 +85,11 @@ Currently, the CLI tool is split into two parts:

### Parsing

The parsing step look for instances of Flipt evaluation and flag retrieval methods:
The parsing step looks for instances of Flipt evaluation and flag retrieval methods:

- `GetFlag`
- `Evaluate` (v1 evaluation)
- `Boolean` (v2 evaluation)
- `Variant` (v2 evaluation)
- [`GetFlag`](https://www.flipt.io/docs/reference/flags/get-flag)
- [`Boolean`](https://www.flipt.io/docs/reference/evaluation/boolean-evaluation)
- [`Variant`](https://www.flipt.io/docs/reference/evaluation/variant-evaluation)

It accomplishes this by:

Expand Down
19 changes: 19 additions & 0 deletions examples/typescript/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FliptApiClient } from "@flipt-io/flipt";
import { DEFAULT_NAMESPACE } from "@flipt-io/flipt/constants";

const client = new FliptApiClient({
environment: "http://localhost:8080",
auth: {
credentials: {
username: "YOUR_USERNAME",
password: "YOUR_PASSWORD",
},
},
});

let response = await client.flags.get({
namespaceKey: DEFAULT_NAMESPACE,
key: "abc123",
});

console.log("Received response from Flipt!", response);
31 changes: 31 additions & 0 deletions examples/typescript/evaluation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { FliptApiClient } from "@flipt-io/flipt";
import { DEFAULT_NAMESPACE } from "@flipt-io/flipt/constants";
import { v4 as uuidv4 } from "uuid";

const client = new FliptApiClient({
environment: "http://localhost:8080",
auth: {
credentials: {
username: "YOUR_USERNAME",
password: "YOUR_PASSWORD",
},
},
});

let response = await client.evaluation.variant({
namespaceKey: DEFAULT_NAMESPACE,
flagKey: "abc123",
entityId: uuidv4(),
context: {},
});

console.log("Received variant response from Flipt!", response);

response = await client.evaluation.boolean({
namespaceKey: DEFAULT_NAMESPACE,
flagKey: "abc123",
entityId: uuidv4(),
context: {},
});

console.log("Received boolean response from Flipt!", response);
6 changes: 3 additions & 3 deletions rules/go.scm
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
body: (literal_value
(keyed_element
(field_identifier) @namespace_key (#eq? @namespace_key "NamespaceKey")
(interpreted_string_literal) @namespace_value)
(_) @namespace_value)
(keyed_element
(field_identifier) @flag_key (#match? @flag_key "^(Key|FlagKey)$")
(interpreted_string_literal) @flag_value)
(_) @flag_value)
)
)
)
Expand All @@ -31,7 +31,7 @@
body: (literal_value
(keyed_element
(field_identifier) @flag_key (#match? @flag_key "^(Key|FlagKey)$")
(interpreted_string_literal) @flag_value)
(_) @flag_value)
)
)
)
Expand Down
38 changes: 38 additions & 0 deletions rules/typescript.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(call_expression
function: (member_expression
property: (property_identifier) @method (#match? @method "(boolean|variant)")
)
arguments: (arguments
(object
(pair
key: (property_identifier) @namespace_key (#eq? @namespace_key "namespaceKey")
value: (_) @namespace_value
)
(pair
key: (property_identifier) @flag_key (#eq? @flag_key "flagKey")
value: (string) @flag_value
)
)
) ? @args
) @call

(call_expression
function: (member_expression
object: (member_expression
property: (property_identifier) @property (#eq? @property "flags")
)
property: (property_identifier) @method (#eq? @method "get")
)
arguments: (arguments
(object
(pair
key: (property_identifier) @namespace_key (#eq? @namespace_key "namespaceKey")
value: (_) @namespace_value
)
(pair
key: (property_identifier) @flag_key (#eq? @flag_key "key")
value: (string) @flag_value
)
)
) ? @args
) @call
7 changes: 7 additions & 0 deletions src/types/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ impl From<String> for Language {
tree_sitter: tree_sitter_go::language(),
file_extension: ".go".to_string(),
},
"typescript" => Language {
name: SupportedLanguage::Typescript,
tree_sitter: tree_sitter_typescript::language_typescript(),
file_extension: ".ts".to_string(),
},
&_ => todo!("Language not supported"),
}
}
Expand All @@ -22,13 +27,15 @@ impl From<String> for Language {
#[derive(clap::ValueEnum, Clone, Debug)]
pub enum SupportedLanguage {
Go,
Typescript,
// Rust,
}

impl fmt::Display for SupportedLanguage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SupportedLanguage::Go => write!(f, "go"),
SupportedLanguage::Typescript => write!(f, "typescript"),
}
}
}
12 changes: 12 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ fn test_scan_go() {

assert_eq!(results.len(), 6);
}

#[test]
fn test_scan_typescript() {
let results = Scanner::new(
SupportedLanguage::Typescript,
Some("examples/typescript".to_string()),
)
.scan()
.unwrap();

assert_eq!(results.len(), 3);
}

0 comments on commit 2d99a5f

Please sign in to comment.