Skip to content

Commit e770b1d

Browse files
committed
Merge branch 'undefinedzilla' of https://github.com/Microsoft/TypeScript into undefinedzilla
2 parents c59c7ec + 301f8c2 commit e770b1d

File tree

79 files changed

+4091
-674
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+4091
-674
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@
3434
path = tests/cases/user/axios-src/axios-src
3535
url = https://github.com/axios/axios.git
3636
ignore = all
37+
[submodule "tests/cases/user/prettier/prettier"]
38+
path = tests/cases/user/prettier/prettier
39+
url = https://github.com/prettier/prettier.git
40+
ignore = all

issue_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Please help us by doing the following steps before logging an issue:
2424
-->
2525

2626
<!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->
27-
**TypeScript Version:** 2.7.0-dev.201xxxxx
27+
**TypeScript Version:** 2.9.0-dev.201xxxxx
2828

2929
<!-- Search terms you tried before logging this (so others can find this issue more easily) -->
3030
**Search Terms:**

src/compiler/checker.ts

Lines changed: 259 additions & 137 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,10 @@
971971
"category": "Error",
972972
"code": 1342
973973
},
974+
"The 'import.meta' meta-property is only allowed using 'ESNext' for the 'target' and 'module' compiler options.": {
975+
"category": "Error",
976+
"code": 1343
977+
},
974978

975979
"Duplicate identifier '{0}'.": {
976980
"category": "Error",

src/compiler/parser.ts

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ namespace ts {
540540
const newSourceFile = IncrementalParser.updateSourceFile(sourceFile, newText, textChangeRange, aggressiveChecks);
541541
// Because new source file node is created, it may not have the flag PossiblyContainDynamicImport. This is the case if there is no new edit to add dynamic import.
542542
// We will manually port the flag to the new source file.
543-
newSourceFile.flags |= (sourceFile.flags & NodeFlags.PossiblyContainsDynamicImport);
543+
newSourceFile.flags |= (sourceFile.flags & NodeFlags.PermanentlySetIncrementalFlags);
544544
return newSourceFile;
545545
}
546546

@@ -2628,6 +2628,20 @@ namespace ts {
26282628
return token() === SyntaxKind.OpenParenToken || token() === SyntaxKind.LessThanToken;
26292629
}
26302630

2631+
function nextTokenIsDot() {
2632+
return nextToken() === SyntaxKind.DotToken;
2633+
}
2634+
2635+
function nextTokenIsOpenParenOrLessThanOrDot() {
2636+
switch (nextToken()) {
2637+
case SyntaxKind.OpenParenToken:
2638+
case SyntaxKind.LessThanToken:
2639+
case SyntaxKind.DotToken:
2640+
return true;
2641+
}
2642+
return false;
2643+
}
2644+
26312645
function parseTypeLiteral(): TypeLiteralNode {
26322646
const node = <TypeLiteralNode>createNode(SyntaxKind.TypeLiteral);
26332647
node.members = parseObjectTypeMembers();
@@ -3096,7 +3110,7 @@ namespace ts {
30963110
case SyntaxKind.Identifier:
30973111
return true;
30983112
case SyntaxKind.ImportKeyword:
3099-
return lookAhead(nextTokenIsOpenParenOrLessThan);
3113+
return lookAhead(nextTokenIsOpenParenOrLessThanOrDot);
31003114
default:
31013115
return isIdentifier();
31023116
}
@@ -3958,14 +3972,31 @@ namespace ts {
39583972
// 3)we have a MemberExpression which either completes the LeftHandSideExpression,
39593973
// or starts the beginning of the first four CallExpression productions.
39603974
let expression: MemberExpression;
3961-
if (token() === SyntaxKind.ImportKeyword && lookAhead(nextTokenIsOpenParenOrLessThan)) {
3962-
// We don't want to eagerly consume all import keyword as import call expression so we look a head to find "("
3963-
// For example:
3964-
// var foo3 = require("subfolder
3965-
// import * as foo1 from "module-from-node
3966-
// We want this import to be a statement rather than import call expression
3967-
sourceFile.flags |= NodeFlags.PossiblyContainsDynamicImport;
3968-
expression = parseTokenNode<PrimaryExpression>();
3975+
if (token() === SyntaxKind.ImportKeyword) {
3976+
if (lookAhead(nextTokenIsOpenParenOrLessThan)) {
3977+
// We don't want to eagerly consume all import keyword as import call expression so we look ahead to find "("
3978+
// For example:
3979+
// var foo3 = require("subfolder
3980+
// import * as foo1 from "module-from-node
3981+
// We want this import to be a statement rather than import call expression
3982+
sourceFile.flags |= NodeFlags.PossiblyContainsDynamicImport;
3983+
expression = parseTokenNode<PrimaryExpression>();
3984+
}
3985+
else if (lookAhead(nextTokenIsDot)) {
3986+
// This is an 'import.*' metaproperty (i.e. 'import.meta')
3987+
const fullStart = scanner.getStartPos();
3988+
nextToken(); // advance past the 'import'
3989+
nextToken(); // advance past the dot
3990+
const node = createNode(SyntaxKind.MetaProperty, fullStart) as MetaProperty;
3991+
node.keywordToken = SyntaxKind.ImportKeyword;
3992+
node.name = parseIdentifierName();
3993+
expression = finishNode(node);
3994+
3995+
sourceFile.flags |= NodeFlags.PossiblyContainsImportMeta;
3996+
}
3997+
else {
3998+
expression = parseMemberExpressionOrHigher();
3999+
}
39694000
}
39704001
else {
39714002
expression = token() === SyntaxKind.SuperKeyword ? parseSuperExpression() : parseMemberExpressionOrHigher();
@@ -4526,7 +4557,7 @@ namespace ts {
45264557
case SyntaxKind.FunctionKeyword:
45274558
return parseFunctionExpression();
45284559
case SyntaxKind.NewKeyword:
4529-
return parseNewExpression();
4560+
return parseNewExpressionOrNewDotTarget();
45304561
case SyntaxKind.SlashToken:
45314562
case SyntaxKind.SlashEqualsToken:
45324563
if (reScanSlashToken() === SyntaxKind.RegularExpressionLiteral) {
@@ -4677,7 +4708,7 @@ namespace ts {
46774708
return isIdentifier() ? parseIdentifier() : undefined;
46784709
}
46794710

4680-
function parseNewExpression(): NewExpression | MetaProperty {
4711+
function parseNewExpressionOrNewDotTarget(): NewExpression | MetaProperty {
46814712
const fullStart = scanner.getStartPos();
46824713
parseExpected(SyntaxKind.NewKeyword);
46834714
if (parseOptional(SyntaxKind.DotToken)) {
@@ -5125,7 +5156,7 @@ namespace ts {
51255156
return true;
51265157

51275158
case SyntaxKind.ImportKeyword:
5128-
return isStartOfDeclaration() || lookAhead(nextTokenIsOpenParenOrLessThan);
5159+
return isStartOfDeclaration() || lookAhead(nextTokenIsOpenParenOrLessThanOrDot);
51295160

51305161
case SyntaxKind.ConstKeyword:
51315162
case SyntaxKind.ExportKeyword:
@@ -6109,14 +6140,35 @@ namespace ts {
61096140
}
61106141

61116142
function setExternalModuleIndicator(sourceFile: SourceFile) {
6112-
sourceFile.externalModuleIndicator = forEach(sourceFile.statements, node =>
6113-
hasModifier(node, ModifierFlags.Export)
6114-
|| node.kind === SyntaxKind.ImportEqualsDeclaration && (<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference
6115-
|| node.kind === SyntaxKind.ImportDeclaration
6116-
|| node.kind === SyntaxKind.ExportAssignment
6117-
|| node.kind === SyntaxKind.ExportDeclaration
6143+
// Try to use the first top-level import/export when available, then
6144+
// fall back to looking for an 'import.meta' somewhere in the tree if necessary.
6145+
sourceFile.externalModuleIndicator =
6146+
forEach(sourceFile.statements, isAnExternalModuleIndicatorNode) ||
6147+
getImportMetaIfNecessary(sourceFile);
6148+
}
6149+
6150+
function isAnExternalModuleIndicatorNode(node: Node) {
6151+
return hasModifier(node, ModifierFlags.Export)
6152+
|| node.kind === SyntaxKind.ImportEqualsDeclaration && (<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference
6153+
|| node.kind === SyntaxKind.ImportDeclaration
6154+
|| node.kind === SyntaxKind.ExportAssignment
6155+
|| node.kind === SyntaxKind.ExportDeclaration
61186156
? node
6119-
: undefined);
6157+
: undefined;
6158+
}
6159+
6160+
function getImportMetaIfNecessary(sourceFile: SourceFile) {
6161+
return sourceFile.flags & NodeFlags.PossiblyContainsImportMeta ?
6162+
walkTreeForExternalModuleIndicators(sourceFile) :
6163+
undefined;
6164+
}
6165+
6166+
function walkTreeForExternalModuleIndicators(node: Node): Node | undefined {
6167+
return isImportMeta(node) ? node : forEachChild(node, walkTreeForExternalModuleIndicators);
6168+
}
6169+
6170+
function isImportMeta(node: Node): boolean {
6171+
return isMetaProperty(node) && node.keywordToken === SyntaxKind.ImportKeyword && node.name.escapedText === "meta";
61206172
}
61216173

61226174
const enum ParsingContext {

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ namespace ts {
984984
// moduleAugmentations has changed
985985
oldProgram.structureIsReused = StructureIsReused.SafeModules;
986986
}
987-
if ((oldSourceFile.flags & NodeFlags.PossiblyContainsDynamicImport) !== (newSourceFile.flags & NodeFlags.PossiblyContainsDynamicImport)) {
987+
if ((oldSourceFile.flags & NodeFlags.PermanentlySetIncrementalFlags) !== (newSourceFile.flags & NodeFlags.PermanentlySetIncrementalFlags)) {
988988
// dynamicImport has changed
989989
oldProgram.structureIsReused = StructureIsReused.SafeModules;
990990
}

src/compiler/transformers/destructuring.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace ts {
4646
value = node.right;
4747
}
4848
else {
49-
return value;
49+
return visitNode(value, visitor, isExpression);
5050
}
5151
}
5252
}

src/compiler/types.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -490,19 +490,21 @@ namespace ts {
490490
ThisNodeOrAnySubNodesHasError = 1 << 17, // If this node or any of its children had an error
491491
HasAggregatedChildData = 1 << 18, // If we've computed data from children and cached it in this node
492492

493-
// This flag will be set when the parser encounters a dynamic import expression so that module resolution
494-
// will not have to walk the tree if the flag is not set. However, this flag is just a approximation because
495-
// once it is set, the flag never gets cleared (hence why it's named "PossiblyContainsDynamicImport").
496-
// During editing, if dynamic import is removed, incremental parsing will *NOT* update this flag. This means that the tree will always be traversed
497-
// during module resolution. However, the removal operation should not occur often and in the case of the
493+
// These flags will be set when the parser encounters a dynamic import expression or 'import.meta' to avoid
494+
// walking the tree if the flags are not set. However, these flags are just a approximation
495+
// (hence why it's named "PossiblyContainsDynamicImport") because once set, the flags never get cleared.
496+
// During editing, if a dynamic import is removed, incremental parsing will *NOT* clear this flag.
497+
// This means that the tree will always be traversed during module resolution, or when looking for external module indicators.
498+
// However, the removal operation should not occur often and in the case of the
498499
// removal, it is likely that users will add the import anyway.
499500
// The advantage of this approach is its simplicity. For the case of batch compilation,
500501
// we guarantee that users won't have to pay the price of walking the tree if a dynamic import isn't used.
501-
/* @internal */
502-
PossiblyContainsDynamicImport = 1 << 19,
503-
JSDoc = 1 << 20, // If node was parsed inside jsdoc
504-
/* @internal */ Ambient = 1 << 21, // If node was inside an ambient context -- a declaration file, or inside something with the `declare` modifier.
505-
/* @internal */ InWithStatement = 1 << 22, // If any ancestor of node was the `statement` of a WithStatement (not the `expression`)
502+
/* @internal */ PossiblyContainsDynamicImport = 1 << 19,
503+
/* @internal */ PossiblyContainsImportMeta = 1 << 20,
504+
505+
JSDoc = 1 << 21, // If node was parsed inside jsdoc
506+
/* @internal */ Ambient = 1 << 22, // If node was inside an ambient context -- a declaration file, or inside something with the `declare` modifier.
507+
/* @internal */ InWithStatement = 1 << 23, // If any ancestor of node was the `statement` of a WithStatement (not the `expression`)
506508

507509
BlockScoped = Let | Const,
508510

@@ -514,6 +516,11 @@ namespace ts {
514516

515517
// Exclude these flags when parsing a Type
516518
TypeExcludesFlags = YieldContext | AwaitContext,
519+
520+
// Represents all flags that are potentially set once and
521+
// never cleared on SourceFiles which get re-used in between incremental parses.
522+
// See the comment above on `PossiblyContainsDynamicImport` and `PossiblyContainsImportMeta`.
523+
/* @internal */ PermanentlySetIncrementalFlags = PossiblyContainsDynamicImport | PossiblyContainsImportMeta,
517524
}
518525

519526
export const enum ModifierFlags {
@@ -1756,7 +1763,7 @@ namespace ts {
17561763
// for the same reasons we treat NewExpression as a PrimaryExpression.
17571764
export interface MetaProperty extends PrimaryExpression {
17581765
kind: SyntaxKind.MetaProperty;
1759-
keywordToken: SyntaxKind.NewKeyword;
1766+
keywordToken: SyntaxKind.NewKeyword | SyntaxKind.ImportKeyword;
17601767
name: Identifier;
17611768
}
17621769

@@ -2557,7 +2564,11 @@ namespace ts {
25572564
languageVersion: ScriptTarget;
25582565
/* @internal */ scriptKind: ScriptKind;
25592566

2560-
// The first node that causes this file to be an external module
2567+
/**
2568+
* The first "most obvious" node that makes a file an external module.
2569+
* This is intended to be the first top-level import/export,
2570+
* but could be arbitrarily nested (e.g. `import.meta`).
2571+
*/
25612572
/* @internal */ externalModuleIndicator?: Node;
25622573
// The first node that causes this file to be a CommonJS module
25632574
/* @internal */ commonJsModuleIndicator?: Node;
@@ -3623,6 +3634,9 @@ namespace ts {
36233634
BooleanLike = Boolean | BooleanLiteral,
36243635
EnumLike = Enum | EnumLiteral,
36253636
ESSymbolLike = ESSymbol | UniqueESSymbol,
3637+
VoidLike = Void | Undefined,
3638+
/* @internal */
3639+
DisjointDomains = NonPrimitive | StringLike | NumberLike | BooleanLike | ESSymbolLike | VoidLike | Null,
36263640
UnionOrIntersection = Union | Intersection,
36273641
StructuredType = Object | Union | Intersection,
36283642
TypeVariable = TypeParameter | IndexedAccess,
@@ -3639,7 +3653,15 @@ namespace ts {
36393653
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
36403654
/* @internal */
36413655
PropagatingFlags = ContainsWideningType | ContainsObjectLiteral | ContainsAnyFunctionType,
3656+
// The following flags are used for different purposes during union and intersection type construction
3657+
/* @internal */
3658+
NonWideningType = ContainsWideningType,
3659+
/* @internal */
3660+
Wildcard = ContainsObjectLiteral,
3661+
/* @internal */
3662+
EmptyObject = ContainsAnyFunctionType,
36423663
/* @internal */
3664+
ConstructionFlags = NonWideningType | Wildcard | EmptyObject
36433665
}
36443666

36453667
export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;

src/compiler/utilities.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,13 @@ namespace ts {
19001900

19011901
export function getJSDocHost(node: JSDocTag): HasJSDoc {
19021902
while (node.parent.kind === SyntaxKind.JSDocTypeLiteral) {
1903-
node = node.parent.parent.parent as JSDocParameterTag;
1903+
if (node.parent.parent.kind === SyntaxKind.JSDocTypedefTag) {
1904+
node = node.parent.parent as JSDocTypedefTag;
1905+
}
1906+
else {
1907+
// node.parent.parent is a type expression, child of a parameter type
1908+
node = node.parent.parent.parent as JSDocParameterTag;
1909+
}
19041910
}
19051911
Debug.assert(node.parent.kind === SyntaxKind.JSDocComment);
19061912
return node.parent.parent;

src/lib/es5.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,15 @@ interface TemplateStringsArray extends ReadonlyArray<string> {
507507
readonly raw: ReadonlyArray<string>;
508508
}
509509

510+
/**
511+
* The type of `import.meta`.
512+
*
513+
* If you need to declare that a given property exists on `import.meta`,
514+
* this type may be augmented via interface merging.
515+
*/
516+
interface ImportMeta {
517+
}
518+
510519
interface Math {
511520
/** The mathematical constant e. This is Euler's number, the base of natural logarithms. */
512521
readonly E: number;

src/loc/lcl/chs/diagnosticMessages/diagnosticMessages.generated.json.lcl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,15 @@
12511251
</Str>
12521252
<Disp Icon="Str" />
12531253
</Item>
1254+
<Item ItemId=";An_element_access_expression_should_take_an_argument_1011" ItemType="0" PsrId="306" Leaf="true">
1255+
<Str Cat="Text">
1256+
<Val><![CDATA[An element access expression should take an argument.]]></Val>
1257+
<Tgt Cat="Text" Stat="Loc" Orig="New">
1258+
<Val><![CDATA[元素访问表达式应采用参数。]]></Val>
1259+
</Tgt>
1260+
</Str>
1261+
<Disp Icon="Str" />
1262+
</Item>
12541263
<Item ItemId=";An_enum_member_cannot_have_a_numeric_name_2452" ItemType="0" PsrId="306" Leaf="true">
12551264
<Str Cat="Text">
12561265
<Val><![CDATA[An enum member cannot have a numeric name.]]></Val>
@@ -6507,6 +6516,9 @@
65076516
<Item ItemId=";Resolve_keyof_to_string_valued_property_names_only_no_numbers_or_symbols_6195" ItemType="0" PsrId="306" Leaf="true">
65086517
<Str Cat="Text">
65096518
<Val><![CDATA[Resolve 'keyof' to string valued property names only (no numbers or symbols).]]></Val>
6519+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6520+
<Val><![CDATA[只将 "keyof" 解析为字符串值的属性名称(不含数字或符号)。]]></Val>
6521+
</Tgt>
65106522
</Str>
65116523
<Disp Icon="Str" />
65126524
</Item>
@@ -8760,6 +8772,15 @@
87608772
</Str>
87618773
<Disp Icon="Str" />
87628774
</Item>
8775+
<Item ItemId=";_0_is_declared_but_never_used_6196" ItemType="0" PsrId="306" Leaf="true">
8776+
<Str Cat="Text">
8777+
<Val><![CDATA['{0}' is declared but never used.]]></Val>
8778+
<Tgt Cat="Text" Stat="Loc" Orig="New">
8779+
<Val><![CDATA[“{0}”已声明,但从未使用过。]]></Val>
8780+
</Tgt>
8781+
</Str>
8782+
<Disp Icon="Str" />
8783+
</Item>
87638784
<Item ItemId=";_0_is_not_a_valid_meta_property_for_keyword_1_Did_you_mean_2_17012" ItemType="0" PsrId="306" Leaf="true">
87648785
<Str Cat="Text">
87658786
<Val><![CDATA['{0}' is not a valid meta-property for keyword '{1}'. Did you mean '{2}'?]]></Val>

0 commit comments

Comments
 (0)