Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Fix parsing of rules that had JsStringLiteral productions
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Nov 22, 2021
1 parent 06df8b8 commit cc95b9b
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 22 deletions.
4 changes: 2 additions & 2 deletions crates/rslint_parser/src/syntax/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub const STARTS_EXPR: TokenSet = token_set![
// "foo"
// 'bar'
// null
pub fn literal(p: &mut Parser) -> Option<CompletedMarker> {
pub fn literal_expression(p: &mut Parser) -> Option<CompletedMarker> {
let literal_kind = match p.cur_tok().kind {
SyntaxKind::JS_NUMBER_LITERAL => {
if p.cur_src().ends_with('n') {
Expand Down Expand Up @@ -923,7 +923,7 @@ pub fn expr(p: &mut Parser) -> Option<CompletedMarker> {

/// A primary expression such as a literal, an object, an array, or `this`.
pub fn primary_expr(p: &mut Parser) -> Option<CompletedMarker> {
if let Some(m) = literal(p) {
if let Some(m) = literal_expression(p) {
return Some(m);
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rslint_parser/src/syntax/object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::syntax::decl::{formal_param_pat, parameter_list, BASE_METHOD_RECOVERY_SET};
use crate::syntax::expr::{assign_expr, expr, identifier_name, literal};
use crate::syntax::expr::{assign_expr, expr, identifier_name, literal_expression};
use crate::syntax::function::{function_body, ts_parameter_types, ts_return_type};
use crate::{CompletedMarker, Parser, ParserState, TokenSet};
use rslint_syntax::SyntaxKind::*;
Expand Down Expand Up @@ -198,7 +198,7 @@ fn setter_object_member(p: &mut Parser) -> CompletedMarker {
// let a = {"foo": foo, [6 + 6]: foo, bar: foo, 7: foo}
pub fn object_prop_name(p: &mut Parser, binding: bool) -> Option<CompletedMarker> {
match p.cur() {
JS_STRING_LITERAL | JS_NUMBER_LITERAL => literal(p),
JS_STRING_LITERAL | JS_NUMBER_LITERAL => literal_expression(p),
T!['['] => Some(computed_member_name(p)),
_ if binding => super::pat::binding_identifier(p),
_ => identifier_name(p),
Expand Down
8 changes: 3 additions & 5 deletions crates/rslint_parser/src/syntax/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use syntax::stmt::FOLLOWS_LET;

use super::expr::{assign_expr, expr, identifier_name, literal, primary_expr};
use super::expr::{assign_expr, expr, identifier_name, primary_expr};
use super::pat::binding_identifier;
use super::stmt::{semi, statements, variable_declaration_statement};
use super::typescript::*;
Expand Down Expand Up @@ -181,16 +181,14 @@ pub fn import_decl(p: &mut Parser) -> CompletedMarker {
p.bump_remap(T![from]);
}

if !p.at(JS_STRING_LITERAL) {
if !p.eat(JS_STRING_LITERAL) {
let err = p
.err_builder(
"expected a source for a `from` clause in an import statement, but found none",
)
.primary(p.cur_tok().range, "");

p.error(err);
} else {
literal(p);
}

if p.cur_src() == "assert" {
Expand Down Expand Up @@ -506,7 +504,7 @@ pub fn export_decl(p: &mut Parser) -> CompletedMarker {
fn from_clause_and_semi(p: &mut Parser, start: usize) {
debug_assert_eq!(p.cur_src(), "from");
p.bump_remap(T![from]);
literal(p);
p.expect(T![js_string_literal]);
semi(p, start..p.cur_tok().range.start);
}

Expand Down
13 changes: 8 additions & 5 deletions crates/rslint_parser/src/syntax/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! TypeScript specific functions.

use super::decl::*;
use super::expr::{assign_expr, identifier_name, lhs_expr, literal};
use super::expr::{assign_expr, identifier_name, lhs_expr, literal_expression};
use super::stmt::{semi, statements, variable_declaration_statement};
use crate::syntax::class::class_declaration;
use crate::syntax::expr::any_reference_member;
Expand Down Expand Up @@ -463,7 +463,7 @@ fn ts_property_or_method_sig(p: &mut Parser, m: Marker, readonly: bool) -> Optio
} else {
match p.cur() {
JS_STRING_LITERAL | JS_NUMBER_LITERAL => {
literal(p);
literal_expression(p);
}
_ => {
let mut complete = any_reference_member(p)?;
Expand Down Expand Up @@ -953,9 +953,12 @@ pub fn ts_non_array_type(p: &mut Parser) -> Option<CompletedMarker> {
ts_type_ref(p, None)
}
}
JS_NUMBER_LITERAL | JS_STRING_LITERAL | TRUE_KW | FALSE_KW | JS_REGEX_LITERAL => {
Some(literal(p).unwrap().precede(p).complete(p, TS_LITERAL))
}
JS_NUMBER_LITERAL | JS_STRING_LITERAL | TRUE_KW | FALSE_KW | JS_REGEX_LITERAL => Some(
literal_expression(p)
.unwrap()
.precede(p)
.complete(p, TS_LITERAL),
),
BACKTICK => {
let m = p.start();
p.bump_any();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
0: [email protected] "pain" [] [Whitespace(" ")]
2: [email protected] "}" [] [Whitespace(" ")]
3: [email protected] "from" [] [Whitespace(" ")]
4: [email protected]
0: [email protected] "\"life\"" [] []
4: [email protected] "\"life\"" [] []
5: [email protected] ";" [] []
2: [email protected] "}" [Whitespace("\n")] []
3: [email protected] "" [Whitespace("\n")] []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
0: [email protected]
0: [email protected] "foo" [] [Whitespace(" ")]
2: [email protected] "from" [] [Whitespace(" ")]
3: [email protected]
0: [email protected] "\"bar\"" [] []
3: [email protected] "\"bar\"" [] []
4: [email protected] ";" [] []
2: [email protected] "}" [Whitespace("\n")] []
3: [email protected] "" [Whitespace("\n")] []
Expand Down
3 changes: 1 addition & 2 deletions crates/rslint_parser/test_data/inline/ok/export.rast
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
0: [email protected] "foo" [] [Whitespace(" ")]
2: [email protected] "}" [] [Whitespace(" ")]
3: [email protected] "from" [] [Whitespace(" ")]
4: [email protected]
0: [email protected] "\"bla\"" [] []
4: [email protected] "\"bla\"" [] []
5: [email protected] ";" [] []
3: [email protected] "" [Whitespace("\n")] []
3 changes: 1 addition & 2 deletions crates/rslint_parser/test_data/inline/ok/import_decl.rast
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
2: [email protected]
0: [email protected] "foo" [] [Whitespace(" ")]
2: [email protected] "from" [] [Whitespace(" ")]
3: [email protected]
0: [email protected] "\"bla\"" [] []
3: [email protected] "\"bla\"" [] []
4: [email protected] ";" [] []
3: [email protected] "" [Whitespace("\n")] []

0 comments on commit cc95b9b

Please sign in to comment.