Merge pull request #4377 from erik-krogh/babelCrash

JS: prevent crash when TemplateLiteral is used in import
This commit is contained in:
Erik Krogh Kristensen 2020-10-01 14:58:45 +02:00 коммит произвёл GitHub
Родитель 0158e2ffef fbd62abd64
Коммит d54a057457
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 267 добавлений и 1 удалений

Просмотреть файл

@ -1421,8 +1421,19 @@ public class TypeScriptASTConverter {
importStart = advance(importStart, m.group(0));
}
}
Node rawPath = convertChild(node, "argument");
ITypeExpression path;
if (rawPath instanceof ITypeExpression) {
path = (ITypeExpression)rawPath;
} else if (rawPath instanceof TemplateLiteral) {
// this is a type-error, so we just fall back to some behavior that does not crash the extractor.
path = new Literal(rawPath.getLoc(), TokenType.string, ((TemplateLiteral)rawPath).getQuasis().stream().map(q -> q.getRaw()).collect(Collectors.joining("")));
} else {
throw new ParseError("Unsupported syntax in import", getSourceLocation(node).getStart());
}
// Find the ending parenthesis in `import(path)` by skipping whitespace after `path`.
ITypeExpression path = convertChild(node, "argument");
String endSrc =
loc.getSource().substring(path.getLoc().getEnd().getOffset() - loc.getStart().getOffset());
Matcher m = WHITESPACE_END_PAREN.matcher(endSrc);

Просмотреть файл

@ -0,0 +1,3 @@
type X = import(3);
type Y = import(`Foo`);
type Z = import(Y);

Просмотреть файл

@ -0,0 +1,252 @@
#10000=@"/importNonStrings.ts;sourcefile"
files(#10000,"/importNonStrings.ts","importNonStrings","ts",0)
#10001=@"/;folder"
folders(#10001,"/","")
containerparent(#10001,#10000)
#10002=@"loc,{#10000},0,0,0,0"
locations_default(#10002,#10000,0,0,0,0)
hasLocation(#10000,#10002)
#20000=@"global_scope"
scopes(#20000,0)
#20001=@"script;{#10000},1,1"
#20002=*
lines(#20002,#20001,"type X = import(3);","
")
#20003=@"loc,{#10000},1,1,1,19"
locations_default(#20003,#10000,1,1,1,19)
hasLocation(#20002,#20003)
#20004=*
lines(#20004,#20001,"type Y = import(`Foo`);","
")
#20005=@"loc,{#10000},2,1,2,23"
locations_default(#20005,#10000,2,1,2,23)
hasLocation(#20004,#20005)
#20006=*
lines(#20006,#20001,"type Z = import(Y);","
")
#20007=@"loc,{#10000},3,1,3,19"
locations_default(#20007,#10000,3,1,3,19)
hasLocation(#20006,#20007)
numlines(#20001,3,3,0)
#20008=*
tokeninfo(#20008,7,#20001,0,"type")
#20009=@"loc,{#10000},1,1,1,4"
locations_default(#20009,#10000,1,1,1,4)
hasLocation(#20008,#20009)
#20010=*
tokeninfo(#20010,6,#20001,1,"X")
#20011=@"loc,{#10000},1,6,1,6"
locations_default(#20011,#10000,1,6,1,6)
hasLocation(#20010,#20011)
#20012=*
tokeninfo(#20012,8,#20001,2,"=")
#20013=@"loc,{#10000},1,8,1,8"
locations_default(#20013,#10000,1,8,1,8)
hasLocation(#20012,#20013)
#20014=*
tokeninfo(#20014,7,#20001,3,"import")
#20015=@"loc,{#10000},1,10,1,15"
locations_default(#20015,#10000,1,10,1,15)
hasLocation(#20014,#20015)
#20016=*
tokeninfo(#20016,8,#20001,4,"(")
#20017=@"loc,{#10000},1,16,1,16"
locations_default(#20017,#10000,1,16,1,16)
hasLocation(#20016,#20017)
#20018=*
tokeninfo(#20018,3,#20001,5,"3")
#20019=@"loc,{#10000},1,17,1,17"
locations_default(#20019,#10000,1,17,1,17)
hasLocation(#20018,#20019)
#20020=*
tokeninfo(#20020,8,#20001,6,")")
#20021=@"loc,{#10000},1,18,1,18"
locations_default(#20021,#10000,1,18,1,18)
hasLocation(#20020,#20021)
#20022=*
tokeninfo(#20022,8,#20001,7,";")
#20023=@"loc,{#10000},1,19,1,19"
locations_default(#20023,#10000,1,19,1,19)
hasLocation(#20022,#20023)
#20024=*
tokeninfo(#20024,7,#20001,8,"type")
#20025=@"loc,{#10000},2,1,2,4"
locations_default(#20025,#10000,2,1,2,4)
hasLocation(#20024,#20025)
#20026=*
tokeninfo(#20026,6,#20001,9,"Y")
#20027=@"loc,{#10000},2,6,2,6"
locations_default(#20027,#10000,2,6,2,6)
hasLocation(#20026,#20027)
#20028=*
tokeninfo(#20028,8,#20001,10,"=")
#20029=@"loc,{#10000},2,8,2,8"
locations_default(#20029,#10000,2,8,2,8)
hasLocation(#20028,#20029)
#20030=*
tokeninfo(#20030,7,#20001,11,"import")
#20031=@"loc,{#10000},2,10,2,15"
locations_default(#20031,#10000,2,10,2,15)
hasLocation(#20030,#20031)
#20032=*
tokeninfo(#20032,8,#20001,12,"(")
#20033=@"loc,{#10000},2,16,2,16"
locations_default(#20033,#10000,2,16,2,16)
hasLocation(#20032,#20033)
#20034=*
tokeninfo(#20034,4,#20001,13,"`Foo`")
#20035=@"loc,{#10000},2,17,2,21"
locations_default(#20035,#10000,2,17,2,21)
hasLocation(#20034,#20035)
#20036=*
tokeninfo(#20036,8,#20001,14,")")
#20037=@"loc,{#10000},2,22,2,22"
locations_default(#20037,#10000,2,22,2,22)
hasLocation(#20036,#20037)
#20038=*
tokeninfo(#20038,8,#20001,15,";")
#20039=@"loc,{#10000},2,23,2,23"
locations_default(#20039,#10000,2,23,2,23)
hasLocation(#20038,#20039)
#20040=*
tokeninfo(#20040,7,#20001,16,"type")
#20041=@"loc,{#10000},3,1,3,4"
locations_default(#20041,#10000,3,1,3,4)
hasLocation(#20040,#20041)
#20042=*
tokeninfo(#20042,6,#20001,17,"Z")
#20043=@"loc,{#10000},3,6,3,6"
locations_default(#20043,#10000,3,6,3,6)
hasLocation(#20042,#20043)
#20044=*
tokeninfo(#20044,8,#20001,18,"=")
#20045=@"loc,{#10000},3,8,3,8"
locations_default(#20045,#10000,3,8,3,8)
hasLocation(#20044,#20045)
#20046=*
tokeninfo(#20046,7,#20001,19,"import")
#20047=@"loc,{#10000},3,10,3,15"
locations_default(#20047,#10000,3,10,3,15)
hasLocation(#20046,#20047)
#20048=*
tokeninfo(#20048,8,#20001,20,"(")
#20049=@"loc,{#10000},3,16,3,16"
locations_default(#20049,#10000,3,16,3,16)
hasLocation(#20048,#20049)
#20050=*
tokeninfo(#20050,6,#20001,21,"Y")
#20051=@"loc,{#10000},3,17,3,17"
locations_default(#20051,#10000,3,17,3,17)
hasLocation(#20050,#20051)
#20052=*
tokeninfo(#20052,8,#20001,22,")")
#20053=@"loc,{#10000},3,18,3,18"
locations_default(#20053,#10000,3,18,3,18)
hasLocation(#20052,#20053)
#20054=*
tokeninfo(#20054,8,#20001,23,";")
#20055=@"loc,{#10000},3,19,3,19"
locations_default(#20055,#10000,3,19,3,19)
hasLocation(#20054,#20055)
#20056=*
tokeninfo(#20056,0,#20001,24,"")
#20057=@"loc,{#10000},4,1,4,0"
locations_default(#20057,#10000,4,1,4,0)
hasLocation(#20056,#20057)
toplevels(#20001,0)
#20058=@"loc,{#10000},1,1,4,0"
locations_default(#20058,#10000,1,1,4,0)
hasLocation(#20001,#20058)
#20059=@"local_type_name;{X};{#20000}"
local_type_names(#20059,"X",#20000)
#20060=@"local_type_name;{Y};{#20000}"
local_type_names(#20060,"Y",#20000)
#20061=@"local_type_name;{Z};{#20000}"
local_type_names(#20061,"Z",#20000)
#20062=*
stmts(#20062,35,#20001,0,"type X = import(3);")
hasLocation(#20062,#20003)
stmt_containers(#20062,#20001)
#20063=*
typeexprs(#20063,1,#20062,0,"X")
hasLocation(#20063,#20011)
enclosing_stmt(#20063,#20062)
expr_containers(#20063,#20001)
literals("X","X",#20063)
typedecl(#20063,#20059)
#20064=*
typeexprs(#20064,30,#20062,1,"import(3)")
#20065=@"loc,{#10000},1,10,1,18"
locations_default(#20065,#10000,1,10,1,18)
hasLocation(#20064,#20065)
enclosing_stmt(#20064,#20062)
expr_containers(#20064,#20001)
#20066=*
typeexprs(#20066,4,#20064,0,"3")
hasLocation(#20066,#20019)
enclosing_stmt(#20066,#20062)
expr_containers(#20066,#20001)
literals("3","3",#20066)
#20067=*
stmts(#20067,35,#20001,1,"type Y ... `Foo`);")
hasLocation(#20067,#20005)
stmt_containers(#20067,#20001)
#20068=*
typeexprs(#20068,1,#20067,0,"Y")
hasLocation(#20068,#20027)
enclosing_stmt(#20068,#20067)
expr_containers(#20068,#20001)
literals("Y","Y",#20068)
typedecl(#20068,#20060)
#20069=*
typeexprs(#20069,30,#20067,1,"import(`Foo`)")
#20070=@"loc,{#10000},2,10,2,22"
locations_default(#20070,#10000,2,10,2,22)
hasLocation(#20069,#20070)
enclosing_stmt(#20069,#20067)
expr_containers(#20069,#20001)
#20071=*
typeexprs(#20071,3,#20069,0,"`Foo`")
hasLocation(#20071,#20035)
enclosing_stmt(#20071,#20067)
expr_containers(#20071,#20001)
literals("Foo","`Foo`",#20071)
#20072=*
stmts(#20072,35,#20001,2,"type Z = import(Y);")
hasLocation(#20072,#20007)
stmt_containers(#20072,#20001)
#20073=*
typeexprs(#20073,1,#20072,0,"Z")
hasLocation(#20073,#20043)
enclosing_stmt(#20073,#20072)
expr_containers(#20073,#20001)
literals("Z","Z",#20073)
typedecl(#20073,#20061)
#20074=*
typeexprs(#20074,30,#20072,1,"import(Y)")
#20075=@"loc,{#10000},3,10,3,18"
locations_default(#20075,#10000,3,10,3,18)
hasLocation(#20074,#20075)
enclosing_stmt(#20074,#20072)
expr_containers(#20074,#20001)
#20076=*
typeexprs(#20076,0,#20074,0,"Y")
hasLocation(#20076,#20051)
enclosing_stmt(#20076,#20072)
expr_containers(#20076,#20001)
literals("Y","Y",#20076)
typebind(#20076,#20060)
#20077=*
entry_cfg_node(#20077,#20001)
#20078=@"loc,{#10000},1,1,1,0"
locations_default(#20078,#10000,1,1,1,0)
hasLocation(#20077,#20078)
#20079=*
exit_cfg_node(#20079,#20001)
hasLocation(#20079,#20057)
successor(#20072,#20079)
successor(#20067,#20072)
successor(#20062,#20067)
successor(#20077,#20062)
numlines(#10000,3,3,0)
filetype(#10000,"typescript")