diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/Cargo.toml | 4 | ||||
-rw-r--r-- | cli/doc/class.rs | 4 | ||||
-rw-r--r-- | cli/doc/function.rs | 2 | ||||
-rw-r--r-- | cli/doc/tests.rs | 98 | ||||
-rw-r--r-- | cli/doc/ts_type.rs | 15 |
5 files changed, 118 insertions, 5 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 38207c579..374aa69c2 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -29,7 +29,7 @@ byteorder = "1.3.4" clap = "2.33.0" dirs = "2.0.2" dlopen = "0.1.8" -dprint-plugin-typescript = "0.17.2" +dprint-plugin-typescript = "0.18.3" futures = { version = "0.3.4", features = ["compat", "io-compat"] } glob = "0.3.0" http = "0.2.1" @@ -60,7 +60,7 @@ walkdir = "2.3.1" warp = "0.2.2" semver-parser = "0.9.0" uuid = { version = "0.8.1", features = ["v4"] } -swc_ecma_visit = "0.1.0" +swc_ecma_visit = "0.4.0" [target.'cfg(windows)'.dependencies] winapi = "0.3.8" diff --git a/cli/doc/class.rs b/cli/doc/class.rs index 8ebb49f65..bfcbfde71 100644 --- a/cli/doc/class.rs +++ b/cli/doc/class.rs @@ -123,10 +123,10 @@ pub fn class_to_class_def( let mut params = vec![]; for param in &ctor.params { - use crate::swc_ecma_ast::PatOrTsParamProp::*; + use crate::swc_ecma_ast::ParamOrTsParamProp::*; let param_def = match param { - Pat(pat) => pat_to_param_def(pat), + Param(param) => pat_to_param_def(¶m.pat), TsParamProp(ts_param_prop) => { use swc_ecma_ast::TsParamPropParam; diff --git a/cli/doc/function.rs b/cli/doc/function.rs index 4c101ab08..e8abf56f9 100644 --- a/cli/doc/function.rs +++ b/cli/doc/function.rs @@ -25,7 +25,7 @@ pub fn function_to_function_def( let mut params = vec![]; for param in &function.params { - let param_def = pat_to_param_def(param); + let param_def = pat_to_param_def(¶m.pat); params.push(param_def); } diff --git a/cli/doc/tests.rs b/cli/doc/tests.rs index 2bae11ec9..f8e693c9a 100644 --- a/cli/doc/tests.rs +++ b/cli/doc/tests.rs @@ -1435,3 +1435,101 @@ export function fooFn(a: number) { .contains("function fooFn(a: number)") ); } + +#[tokio::test] +async fn ts_lit_types() { + let source_code = r#" +export type boolLit = false; +export type strLit = "text"; +export type tplLit = `text`; +export type numLit = 5; +"#; + let loader = + TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]); + let entries = DocParser::new(loader).parse("test.ts").await.unwrap(); + let actual = serde_json::to_value(entries).unwrap(); + let expected_json = json!([ + { + "kind": "typeAlias", + "name": "boolLit", + "location": { + "filename": "test.ts", + "line": 2, + "col": 0 + }, + "jsDoc": null, + "typeAliasDef": { + "tsType": { + "repr": "false", + "kind": "literal", + "literal": { + "kind": "boolean", + "boolean": false + } + }, + "typeParams": [] + } + }, { + "kind": "typeAlias", + "name": "strLit", + "location": { + "filename": "test.ts", + "line": 3, + "col": 0 + }, + "jsDoc": null, + "typeAliasDef": { + "tsType": { + "repr": "text", + "kind": "literal", + "literal": { + "kind": "string", + "string": "text" + } + }, + "typeParams": [] + } + }, { + "kind": "typeAlias", + "name": "tplLit", + "location": { + "filename": "test.ts", + "line": 4, + "col": 0 + }, + "jsDoc": null, + "typeAliasDef": { + "tsType": { + "repr": "text", + "kind": "literal", + "literal": { + "kind": "string", + "string": "text" + } + }, + "typeParams": [] + } + }, { + "kind": "typeAlias", + "name": "numLit", + "location": { + "filename": "test.ts", + "line": 5, + "col": 0 + }, + "jsDoc": null, + "typeAliasDef": { + "tsType": { + "repr": "5", + "kind": "literal", + "literal": { + "kind": "number", + "number": 5.0 + } + }, + "typeParams": [] + } + } + ]); + assert_eq!(actual, expected_json); +} diff --git a/cli/doc/ts_type.rs b/cli/doc/ts_type.rs index b5f5a8807..ec1a5e7e9 100644 --- a/cli/doc/ts_type.rs +++ b/cli/doc/ts_type.rs @@ -69,6 +69,21 @@ impl Into<TsTypeDef> for &TsLitType { boolean: None, }, ), + TsLit::Tpl(tpl) => { + // A template literal in a type is not allowed to have + // expressions, so there will only be one quasi. + let quasi = tpl.quasis.get(0).expect("Expected tpl to have a quasi."); + let text = quasi.raw.value.to_string(); + ( + text.clone(), + LiteralDef { + kind: LiteralDefKind::String, // semantically the same + number: None, + string: Some(text), + boolean: None, + }, + ) + } TsLit::Bool(bool_) => ( bool_.value.to_string(), LiteralDef { |