Put doc-comments under metadata flag.

This commit is contained in:
Stephen Chung 2021-04-09 22:49:47 +08:00
parent 2b1555cff8
commit 29d186b361
4 changed files with 36 additions and 10 deletions

View File

@ -17,6 +17,7 @@ Breaking changes
* `ModuleResolver` trait methods take an additional parameter `source_path` that contains the path of the current environment. This is to facilitate loading other script files always from the current directory. * `ModuleResolver` trait methods take an additional parameter `source_path` that contains the path of the current environment. This is to facilitate loading other script files always from the current directory.
* `FileModuleResolver` now resolves relative paths under the source path if there is no base path set. * `FileModuleResolver` now resolves relative paths under the source path if there is no base path set.
* `FileModuleResolver::base_path` now returns `Option<&str>` which is `None` if there is no base path set. * `FileModuleResolver::base_path` now returns `Option<&str>` which is `None` if there is no base path set.
* Doc-comments now require the `metadata` feature.
New features New features
------------ ------------

View File

@ -11,7 +11,6 @@ use crate::stdlib::{
iter::empty, iter::empty,
num::{NonZeroU8, NonZeroUsize}, num::{NonZeroU8, NonZeroUsize},
ops::{Add, AddAssign}, ops::{Add, AddAssign},
string::String,
vec, vec,
vec::Vec, vec::Vec,
}; };
@ -65,7 +64,9 @@ pub struct ScriptFnDef {
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
pub externals: crate::stdlib::collections::BTreeSet<Identifier>, pub externals: crate::stdlib::collections::BTreeSet<Identifier>,
/// Function doc-comments (if any). /// Function doc-comments (if any).
pub comments: StaticVec<String>, #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
pub comments: StaticVec<crate::stdlib::string::String>,
} }
impl fmt::Display for ScriptFnDef { impl fmt::Display for ScriptFnDef {
@ -103,6 +104,8 @@ pub struct ScriptFnMetadata<'a> {
/// ///
/// Leading white-spaces are stripped, and each string slice always starts with the corresponding /// Leading white-spaces are stripped, and each string slice always starts with the corresponding
/// doc-comment leader: `///` or `/**`. /// doc-comment leader: `///` or `/**`.
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
pub comments: Vec<&'a str>, pub comments: Vec<&'a str>,
/// Function access mode. /// Function access mode.
pub access: FnAccess, pub access: FnAccess,
@ -134,6 +137,8 @@ impl<'a> Into<ScriptFnMetadata<'a>> for &'a ScriptFnDef {
#[inline(always)] #[inline(always)]
fn into(self) -> ScriptFnMetadata<'a> { fn into(self) -> ScriptFnMetadata<'a> {
ScriptFnMetadata { ScriptFnMetadata {
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments: self.comments.iter().map(|s| s.as_str()).collect(), comments: self.comments.iter().map(|s| s.as_str()).collect(),
access: self.access, access: self.access,
name: &self.name, name: &self.name,

View File

@ -1002,6 +1002,8 @@ pub fn optimize_into_ast(
lib: None, lib: None,
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
mods: Default::default(), mods: Default::default(),
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments: Default::default(), comments: Default::default(),
}) })
.for_each(|fn_def| { .for_each(|fn_def| {

View File

@ -16,7 +16,7 @@ use crate::stdlib::{
hash::{Hash, Hasher}, hash::{Hash, Hasher},
iter::empty, iter::empty,
num::{NonZeroU8, NonZeroUsize}, num::{NonZeroU8, NonZeroUsize},
string::{String, ToString}, string::ToString,
vec, vec,
vec::Vec, vec::Vec,
}; };
@ -2483,10 +2483,10 @@ fn parse_stmt(
) -> Result<Stmt, ParseError> { ) -> Result<Stmt, ParseError> {
use AccessMode::{ReadOnly, ReadWrite}; use AccessMode::{ReadOnly, ReadWrite};
let mut _comments: StaticVec<String> = Default::default();
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
{ #[cfg(feature = "metadata")]
let comments = {
let mut comments: StaticVec<crate::stdlib::string::String> = Default::default();
let mut comments_pos = Position::NONE; let mut comments_pos = Position::NONE;
// Handle doc-comments. // Handle doc-comments.
@ -2505,7 +2505,7 @@ fn parse_stmt(
match input.next().unwrap().0 { match input.next().unwrap().0 {
Token::Comment(comment) => { Token::Comment(comment) => {
_comments.push(comment); comments.push(comment);
match input.peek().unwrap() { match input.peek().unwrap() {
(Token::Fn, _) | (Token::Private, _) => break, (Token::Fn, _) | (Token::Private, _) => break,
@ -2516,7 +2516,9 @@ fn parse_stmt(
_ => unreachable!(), _ => unreachable!(),
} }
} }
}
comments
};
let (token, token_pos) = match input.peek().unwrap() { let (token, token_pos) = match input.peek().unwrap() {
(Token::EOF, pos) => return Ok(Stmt::Noop(*pos)), (Token::EOF, pos) => return Ok(Stmt::Noop(*pos)),
@ -2572,7 +2574,17 @@ fn parse_stmt(
pos: pos, pos: pos,
}; };
let func = parse_fn(input, &mut new_state, lib, access, settings, _comments)?; let func = parse_fn(
input,
&mut new_state,
lib,
access,
settings,
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments,
)?;
let hash = calc_fn_hash(empty(), &func.name, func.params.len()); let hash = calc_fn_hash(empty(), &func.name, func.params.len());
if lib.contains_key(&hash) { if lib.contains_key(&hash) {
@ -2727,7 +2739,9 @@ fn parse_fn(
lib: &mut FunctionsLib, lib: &mut FunctionsLib,
access: FnAccess, access: FnAccess,
mut settings: ParseSettings, mut settings: ParseSettings,
comments: StaticVec<String>, #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments: StaticVec<crate::stdlib::string::String>,
) -> Result<ScriptFnDef, ParseError> { ) -> Result<ScriptFnDef, ParseError> {
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?; settings.ensure_level_within_max_limit(state.max_expr_depth)?;
@ -2814,6 +2828,8 @@ fn parse_fn(
lib: None, lib: None,
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
mods: Default::default(), mods: Default::default(),
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments, comments,
}) })
} }
@ -2967,6 +2983,8 @@ fn parse_anon_fn(
lib: None, lib: None,
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
mods: Default::default(), mods: Default::default(),
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments: Default::default(), comments: Default::default(),
}; };