mod diagnostics;
mod expressions;
mod signatures;
mod templates;
mod types;
pub use diagnostics::{Diagnostic, DiagnosticKind, Fix, Report};
use crate::ast::Template;
use crate::shape::{TypeDesc, TypeRegistry};
use crate::span::{SourceMap, Span};
use std::collections::{BTreeMap, BTreeSet};
use types::{Environment, Ty};
#[derive(Clone, Debug, Default)]
pub struct Scope {
pub bindings: BTreeMap<String, TypeDesc>,
pub absent: BTreeSet<String>,
}
impl Scope {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, name: String, ty: TypeDesc) -> Option<TypeDesc> {
self.absent.remove(&name);
self.bindings.insert(name, ty)
}
fn binding_type(&self, name: &str, ty: &TypeDesc, direction: crate::shape::Direction) -> Ty {
let ty = Ty::shape(ty.clone(), direction);
if self.absent.contains(name) {
Ty::Absent(Box::new(ty))
} else {
ty
}
}
}
impl From<BTreeMap<String, TypeDesc>> for Scope {
fn from(bindings: BTreeMap<String, TypeDesc>) -> Self {
Self {
bindings,
..Self::default()
}
}
}
impl<const N: usize> From<[(String, TypeDesc); N]> for Scope {
fn from(bindings: [(String, TypeDesc); N]) -> Self {
BTreeMap::from(bindings).into()
}
}
impl<'a> IntoIterator for &'a Scope {
type Item = (&'a String, &'a TypeDesc);
type IntoIter = std::collections::btree_map::Iter<'a, String, TypeDesc>;
fn into_iter(self) -> Self::IntoIter {
self.bindings.iter()
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Signature {
pub parameters: Vec<TypeDesc>,
pub variadic: Option<TypeDesc>,
pub returns: TypeDesc,
}
impl Signature {
pub fn new(parameters: Vec<TypeDesc>, returns: TypeDesc) -> Self {
Self {
parameters,
variadic: None,
returns,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct ComponentDefinition<'a> {
pub template: Option<&'a Template>,
pub props: Option<TypeDesc>,
pub slots: BTreeMap<String, bool>,
pub helpers: BTreeMap<String, Signature>,
pub computed: Scope,
}
pub trait Resolver {
fn include(&self, name: &str) -> Option<&Template>;
fn component(&self, name: &str) -> Option<ComponentDefinition<'_>>;
fn directive(&self, _name: &str, _arguments: Option<Span>) -> Option<&Template> {
None
}
}
impl Resolver for () {
fn include(&self, _: &str) -> Option<&Template> {
None
}
fn component(&self, _: &str) -> Option<ComponentDefinition<'_>> {
None
}
}
#[derive(Clone, Debug)]
pub struct Placeholder {
pub name: String,
pub ty: TypeDesc,
pub optional: bool,
pub model_key: Option<String>,
}
pub type Routes = BTreeMap<String, Vec<Placeholder>>;
pub struct Checker<'a> {
pub types: &'a TypeRegistry,
pub sources: &'a SourceMap,
pub resolver: &'a dyn Resolver,
pub routes: Routes,
pub shared: Scope,
pub middleware: BTreeSet<String>,
pub helpers: BTreeMap<String, Signature>,
pub methods: BTreeMap<(String, String), Signature>,
pub conditions: BTreeMap<String, Signature>,
pub paginators: BTreeMap<String, TypeDesc>,
pub warn_unused_fields: bool,
pub constants: Scope,
}
impl<'a> Checker<'a> {
pub fn new(
types: &'a TypeRegistry,
sources: &'a SourceMap,
resolver: &'a dyn Resolver,
) -> Self {
Self {
types,
sources,
resolver,
routes: Routes::new(),
shared: Scope::new(),
middleware: BTreeSet::new(),
helpers: BTreeMap::new(),
methods: BTreeMap::new(),
conditions: BTreeMap::new(),
paginators: BTreeMap::new(),
constants: Scope::new(),
warn_unused_fields: true,
}
}
pub fn check(&self, template: &Template, scope: &Scope) -> Report {
self.check_views([(template, scope)])
}
pub fn check_views<'b>(
&self,
roots: impl IntoIterator<Item = (&'b Template, &'b Scope)>,
) -> Report {
let mut run = Run::new(self);
for (template, scope) in roots {
let mut env = run.root();
for (name, ty) in scope {
env.bind_model(
name,
scope.binding_type(name, ty, crate::shape::Direction::Serialize),
);
}
run.frames.push(Frame::default());
run.template(template, &mut env);
let frame = run.frames.pop().unwrap_or_default();
if self.warn_unused_fields {
for name in scope
.bindings
.keys()
.filter(|name| !frame.reads.contains(*name))
{
run.warn(
"unused-view-field",
Span::new(template.span.file, template.span.start, template.span.start),
format!("view-model field `{name}` is never read"),
);
}
}
}
run.report.templates = run.visited.len();
run.report
}
pub fn scope(&self, shape: TypeDesc) -> Scope {
types::shape_fields(self.types, &shape, crate::shape::Direction::Serialize)
}
}
#[derive(Clone, Default)]
struct Frame {
diagnostics: BTreeSet<usize>,
reads: BTreeSet<String>,
}
struct Memo {
scope: Environment,
frame: Frame,
}
struct Run<'a, 'config> {
config: &'a Checker<'config>,
report: Report,
frames: Vec<Frame>,
active: Vec<Span>,
visited: BTreeSet<u32>,
memo: BTreeMap<(u32, u32, u32), Vec<Memo>>,
}
impl<'a, 'config> Run<'a, 'config> {
fn new(config: &'a Checker<'config>) -> Self {
Self {
config,
report: Report::default(),
frames: Vec::new(),
active: Vec::new(),
visited: BTreeSet::new(),
memo: BTreeMap::new(),
}
}
fn root(&self) -> Environment {
let mut env = Environment::default();
env.bind("errors".into(), Ty::Errors, false);
for (name, ty) in &self.config.shared {
env.bind(
name.clone(),
self.config
.shared
.binding_type(name, ty, crate::shape::Direction::Serialize),
false,
);
if self.config.middleware.contains(name) {
env.mark_middleware(name);
}
}
env
}
fn issue(
&mut self,
kind: DiagnosticKind,
code: &'static str,
span: Span,
message: impl Into<String>,
) -> usize {
let index = self
.report
.add(self.config.sources, kind, code, span, message.into());
if let Some(frame) = self.frames.last_mut() {
frame.diagnostics.insert(index);
}
index
}
fn error(&mut self, code: &'static str, span: Span, message: impl Into<String>) {
self.issue(DiagnosticKind::Error, code, span, message);
}
fn warn(&mut self, code: &'static str, span: Span, message: impl Into<String>) {
self.issue(DiagnosticKind::Warning, code, span, message);
}
fn unchecked(&mut self, code: &'static str, span: Span, message: impl Into<String>) {
self.issue(DiagnosticKind::Unchecked, code, span, message);
}
}