1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//! Extra utlities for untyped syntax nodes, syntax tokens, and AST nodes.

use crate::*;

pub use rslint_lexer::color;

/// Extensions to rowan's SyntaxNode
pub trait SyntaxNodeExt {
    #[doc(hidden)]
    fn to_node(&self) -> &SyntaxNode;

    /// Get all of the tokens of this node, recursively, including whitespace and comments.
    fn tokens(&self) -> Vec<SyntaxToken> {
        self.to_node()
            .descendants_with_tokens()
            .filter_map(|x| x.into_token())
            .collect()
    }

    /// Get all the tokens of this node, recursively, not including whitespace and comments.
    fn lossy_tokens(&self) -> Vec<SyntaxToken> {
        self.to_node()
            .descendants_with_tokens()
            .filter_map(|x| x.into_token().filter(|token| !token.kind().is_trivia()))
            .collect()
    }

    /// Get the first non-whitespace child token.
    fn first_lossy_token(&self) -> Option<SyntaxToken> {
        self.to_node()
            .children_with_tokens()
            .filter_map(|it| it.into_token().filter(|x| !x.kind().is_trivia()))
            .next()
    }

    /// Check if the node is a certain AST node and that it can be casted to it.
    fn is<T: AstNode>(&self) -> bool {
        T::can_cast(self.to_node().kind())
    }

    /// Cast this node to a certain AST node.
    ///
    /// # Panics
    /// Panics if the underlying node cannot be cast to the AST node
    fn to<T: AstNode>(&self) -> T {
        T::cast(self.to_node().to_owned()).unwrap_or_else(|| {
            panic!(
                "Tried to cast node as `{:?}` but was unable to cast",
                stringify!(T)
            )
        })
    }

    /// Try to cast this node to a certain AST node
    fn try_to<T: AstNode>(&self) -> Option<T> {
        T::cast(self.to_node().to_owned())
    }

    /// Compare two syntax nodes by comparing their underlying non-whitespace tokens.
    ///
    /// This is a more accurate way of comparing nodes because it does not count whitespace.
    /// Text based equality counts `foo. bar` and `foo.bar` as different, while this counts them as the same.
    ///
    /// # Examples
    ///
    /// ```
    /// use rslint_parser::{SyntaxNodeExt, parse_expr};
    ///
    /// let left = parse_expr("foo. bar", 0).syntax();
    /// let right = parse_expr("foo.bar", 0).syntax();
    ///
    /// assert!(left.lexical_eq(&right));
    ///
    /// assert_ne!(left.text(), right.text());
    /// ```
    fn lexical_eq(&self, right: &SyntaxNode) -> bool {
        let left = self.lossy_tokens();
        let right = right.lossy_tokens();

        if left.len() == right.len() {
            left.iter()
                .zip(right.iter())
                .all(|(l, r)| l.text() == r.text())
        } else {
            false
        }
    }

    /// Syntax highlight the node's text into an ANSI string.
    /// If stdout and stderr are not terminals, this will return the raw
    /// node text.
    fn color(&self) -> String {
        color(&self.to_node().text().to_string())
    }

    /// Get the text range of this node, not including any leading or trailing whitespace.
    ///
    /// # Examples
    ///
    /// ```
    /// use rslint_parser::{SyntaxNodeExt, parse_expr, TextRange};
    ///
    /// let node = parse_expr(" foo. bar  ", 0).syntax();
    ///
    /// assert_eq!(node.trimmed_range(), TextRange::new(1.into(), 9.into()));
    ///
    /// assert_eq!(node.text_range(), TextRange::new(0.into(), 11.into()));
    /// ```
    fn trimmed_range(&self) -> TextRange {
        let node = self.to_node();
        let tokens = node.lossy_tokens();
        let start = tokens
            .first()
            .map(|t| t.text_range().start())
            .unwrap_or_else(|| 0.into());
        let end = tokens
            .last()
            .map(|t| t.text_range().end())
            .unwrap_or_else(|| 0.into());

        TextRange::new(start, end)
    }

    /// Get the text of this node, not including leading or trailing whitespace
    ///
    /// # Examples
    /// ```
    /// use rslint_parser::{SyntaxNodeExt, parse_expr, TextRange};
    ///
    /// let node = parse_expr(" foo. bar  ", 0).syntax();
    ///
    /// assert_eq!(node.trimmed_text(), "foo. bar");
    /// ```
    fn trimmed_text(&self) -> SyntaxText {
        let trimmed = self.to_node().trimmed_range();
        let offset = self.to_node().text_range().start();
        self.to_node().text().slice(TextRange::new(
            trimmed.start().checked_sub(offset).unwrap_or_default(),
            trimmed.end().checked_sub(offset).unwrap_or_default(),
        ))
    }

    /// Check whether this node's kind is contained in a token set.
    fn in_ts(&self, set: TokenSet) -> bool {
        set.contains(self.to_node().kind())
    }

    /// A human readable name for this node's kind. e.g.:
    /// `BREAK_STMT` => `Break statement`
    ///
    /// Returns a capitalized name without an underscore for anything not a statement. e.g.:
    /// `FN_DECL` => `Fn decl`
    fn readable_stmt_name(&self) -> String {
        let mut string = format!("{:?}", self.to_node().kind())
            .to_ascii_lowercase()
            .replace("_", " ");
        // Safety: the kind cannot produce an empty string and all kinds are ascii uppercase letters.
        unsafe {
            string.as_bytes_mut()[0] = string.as_bytes()[0] - 32;
        }

        if self.to_node().is::<ast::Stmt>() {
            string = string.replace("stmt", "statement");
        }

        string
    }

    /// Whether this node is an iteration statement.
    #[inline]
    fn is_loop(&self) -> bool {
        const ITERATION_STMT: [SyntaxKind; 5] = [
            SyntaxKind::FOR_IN_STMT,
            SyntaxKind::FOR_OF_STMT,
            SyntaxKind::FOR_STMT,
            SyntaxKind::WHILE_STMT,
            SyntaxKind::DO_WHILE_STMT,
        ];
        ITERATION_STMT.contains(&self.to_node().kind())
    }

    /// Go over the descendants of this node, at every descendant call `func`, and keep traversing
    /// the descendants of that node if the function's return is `true`. If the function returns false
    /// then stop traversing the descendants of that node go on to the next child.
    ///
    /// For example:
    /// ```ignore
    /// ROOT
    ///     CHILD // <-- Call `F` with CHILD, `F` returns `false` so go on to the next child...
    ///         SUBCHILD
    ///     CHILD // <-- Call `F` with CHILD, `F` return `true` so go on to the children of CHILD
    ///         SUBCHILD // <-- Same thing
    ///             SUBSUBCHILD
    ///     CHILD // Go on to the next child and do the same thing
    /// ```
    fn descendants_with<F>(&self, func: &mut F)
    where
        F: FnMut(&SyntaxNode) -> bool,
    {
        for node in self.to_node().children() {
            if func(&node) {
                node.descendants_with(func);
            }
        }
    }

    /// Separate all the lossy tokens of this node, then compare each token's text with the corresponding
    /// text in `tokens`.
    fn structural_lossy_token_eq(&self, tokens: &[impl AsRef<str>]) -> bool {
        let node_tokens = self.to_node().lossy_tokens();
        if node_tokens.len() == tokens.len() {
            node_tokens
                .iter()
                .zip(tokens.iter())
                .all(|(l, r)| l.text() == r.as_ref())
        } else {
            false
        }
    }

    /// Whether the node contains any comments.
    fn contains_comments(&self) -> bool {
        self.tokens()
            .iter()
            .any(|tok| tok.kind() == SyntaxKind::COMMENT)
    }

    /// Get the first child with a specific kind.
    fn child_with_kind(&self, kind: SyntaxKind) -> Option<SyntaxNode> {
        self.to_node().children().find(|child| child.kind() == kind)
    }

    /// Get the parent of this node, recursing through any grouping expressions
    fn expr_parent(&self) -> Option<SyntaxNode> {
        let parent = self.to_node().parent()?;
        if parent.kind() == SyntaxKind::GROUPING_EXPR {
            parent.parent()
        } else {
            Some(parent)
        }
    }

    /// Get the first child in this node that can be casted to an AST node
    fn child_with_ast<T: AstNode>(&self) -> Option<T> {
        self.to_node().children().find_map(|child| child.try_to())
    }

    /// Same as [`descendants_with`](Self::descendants_with) but considers tokens too.
    fn descendants_with_tokens_with<F>(&self, func: &mut F)
    where
        F: FnMut(&SyntaxElement) -> bool,
    {
        for elem in self.to_node().children_with_tokens() {
            match &elem {
                NodeOrToken::Node(node) => {
                    if func(&elem) {
                        node.descendants_with_tokens_with(func)
                    }
                }
                NodeOrToken::Token(_) => {
                    let _ = func(&elem);
                }
            }
        }
    }

    /// Get a specific token in the node which matches a syntax kind.
    ///
    /// This does not consider tokens in descendant nodes
    fn token_with_kind(&self, kind: SyntaxKind) -> Option<SyntaxToken> {
        self.to_node()
            .children_with_tokens()
            .find_map(|t| t.into_token().filter(|it| it.kind() == kind))
    }
}

impl SyntaxNodeExt for SyntaxNode {
    fn to_node(&self) -> &SyntaxNode {
        self
    }
}

pub trait SyntaxTokenExt {
    fn to_token(&self) -> &SyntaxToken;

    /// Convert a comment to a more detailed representation.
    fn comment(&self) -> Option<Comment> {
        if self.to_token().kind() != SyntaxKind::COMMENT {
            return None;
        }

        let token = self.to_token();
        let (kind, content) = match &token.text()[0..2] {
            "//" => (
                CommentKind::Inline,
                token
                    .text()
                    .get(2..)
                    .map(|x| x.to_string())
                    .unwrap_or_default(),
            ),
            "/*" if token.text().chars().nth(2) == Some('*') => {
                let len = token.text().len();
                let end = if token.text().get(len - 2..len) == Some("*/") {
                    len - 2
                } else {
                    len
                };
                (
                    CommentKind::JsDoc,
                    token
                        .text()
                        .get(3..end)
                        .map(|x| x.to_string())
                        .unwrap_or_default(),
                )
            }
            "/*" => {
                let len = token.text().len();
                let end = if token.text().get(len - 2..len) == Some("*/") {
                    len - 2
                } else {
                    len
                };
                (
                    CommentKind::JsDoc,
                    token
                        .text()
                        .get(3..end)
                        .map(|x| x.to_string())
                        .unwrap_or_default(),
                )
            }
            _ => return None,
        };
        Some(Comment {
            kind,
            content,
            token: self.to_token().clone(),
        })
    }

    /// Check whether this token's kind is contained in a token set.
    fn in_ts(&self, set: TokenSet) -> bool {
        set.contains(self.to_token().kind())
    }
}

impl SyntaxTokenExt for SyntaxToken {
    fn to_token(&self) -> &SyntaxToken {
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Comment {
    pub kind: CommentKind,
    pub content: String,
    pub token: SyntaxToken,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CommentKind {
    /// A block comment which starts with `/**`
    JsDoc,
    /// A block comment which starts with `/*`
    Multiline,
    /// An inline comment which starts with `//`
    Inline,
}

/// Concatenate tokens into a string
pub fn concat_tokens(tokens: &[SyntaxToken]) -> String {
    tokens
        .iter()
        .map(|token| token.text().to_string())
        .collect()
}

/// Check whether a string contains a valid js linebreak consisting of any of these characters:
/// `\n`, `\r`, `\u{2028}`, or `\u{2029}`
pub fn contains_js_linebreak(string: impl AsRef<str>) -> bool {
    let text = string.as_ref();
    text.contains('\n')
        || text.contains('\r')
        || text.contains('\u{2028}')
        || text.contains('\u{2029}')
}

/// Check whether a string contains a valid js whitespace character
// FIXME: this should account for stuff in the Zs unicode category
pub fn contains_js_whitespace(string: impl AsRef<str>) -> bool {
    let text = string.as_ref();
    text.contains(' ')
        || text.contains('\u{000B}')
        || text.contains('\u{000C}')
        || text.contains('\u{0020}')
        || text.contains('\u{00A0}')
        || text.contains('\u{FEFF}')
}