fix(es/parser): Fix parsing of const in ambient context (#3883)

This commit is contained in:
Donny/강동윤 2022-03-06 23:33:14 +09:00 committed by GitHub
parent cc84e9595e
commit aea59b844c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 4 deletions

View File

@ -887,7 +887,17 @@ impl Config {
///
/// - typescript: `tsx` will be modified if file extension is `ts`.
pub fn adjust(&mut self, file: &Path) {
if let Some(Syntax::Typescript(TsConfig { tsx, .. })) = &mut self.jsc.syntax {
if let Some(Syntax::Typescript(TsConfig { tsx, dts, .. })) = &mut self.jsc.syntax {
let is_dts = file
.file_name()
.and_then(|f| f.to_str())
.map(|s| s.ends_with(".d.ts"))
.unwrap_or(false);
if is_dts {
*dts = true;
}
let is_ts = file.extension().map(|v| v == "ts").unwrap_or(false);
if is_ts {
*tsx = false;

View File

@ -0,0 +1,21 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": true
},
"target": "es2022",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6",
"strict": false,
"strictMode": false
},
"minify": false
}

View File

@ -0,0 +1,7 @@
// filename: types.d.ts
export const aThing: IAnInterface[];
export interface IAnInterface {
anAttribute: string;
anotherAttribute: boolean;
}

View File

@ -0,0 +1 @@
export const aThing;

View File

@ -11,7 +11,7 @@ use crate::{
error::SyntaxError,
lexer::Lexer,
token::{Token, Word},
Context, EsVersion, Syntax,
Context, EsVersion, Syntax, TsConfig,
};
#[cfg(test)]
extern crate test;
@ -61,7 +61,17 @@ impl<'a, I: Input> Parser<Lexer<'a, I>> {
}
impl<I: Tokens> Parser<I> {
pub fn new_from(input: I) -> Self {
pub fn new_from(mut input: I) -> Self {
let in_declare = matches!(
input.syntax(),
Syntax::Typescript(TsConfig { dts: true, .. })
);
let ctx = Context {
in_declare,
..input.ctx()
};
input.set_ctx(ctx);
Parser {
state: Default::default(),
input: Buffer::new(input),

View File

@ -809,7 +809,8 @@ impl<'a, I: Tokens> Parser<I> {
// typescript allows `declare` vars not to have initializers.
if self.ctx().in_declare {
None
} else if kind == VarDeclKind::Const && self.ctx().strict {
} else if kind == VarDeclKind::Const && self.ctx().strict && !self.ctx().in_declare
{
self.emit_err(
span!(self, start),
SyntaxError::ConstDeclarationsRequireInitialization,