Skip to content

Cherry-pick #37064 into release-3.8 #37096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1922,10 +1922,11 @@ namespace ts {
if (!isValidTypeOnlyAliasUseSite(useSite)) {
const typeOnlyDeclaration = getTypeOnlyAliasDeclaration(symbol);
if (typeOnlyDeclaration) {
const message = typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier
const isExport = typeOnlyDeclarationIsExport(typeOnlyDeclaration);
const message = isExport
? Diagnostics._0_cannot_be_used_as_a_value_because_it_was_exported_using_export_type
: Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type;
const relatedMessage = typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier
const relatedMessage = isExport
? Diagnostics._0_was_exported_here
: Diagnostics._0_was_imported_here;
const unescapedName = unescapeLeadingUnderscores(name);
Expand Down Expand Up @@ -2272,12 +2273,14 @@ namespace ts {
function checkAndReportErrorForResolvingImportAliasToTypeOnlySymbol(node: ImportEqualsDeclaration, resolved: Symbol | undefined) {
if (markSymbolOfAliasDeclarationIfTypeOnly(node, /*immediateTarget*/ undefined, resolved, /*overwriteEmpty*/ false)) {
const typeOnlyDeclaration = getTypeOnlyAliasDeclaration(getSymbolOfNode(node))!;
const message = typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier
const isExport = typeOnlyDeclarationIsExport(typeOnlyDeclaration);
const message = isExport
? Diagnostics.An_import_alias_cannot_reference_a_declaration_that_was_exported_using_export_type
: Diagnostics.An_import_alias_cannot_reference_a_declaration_that_was_imported_using_import_type;
const relatedMessage = typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier
const relatedMessage = isExport
? Diagnostics._0_was_exported_here
: Diagnostics._0_was_imported_here;

// Non-null assertion is safe because the optionality comes from ImportClause,
// but if an ImportClause was the typeOnlyDeclaration, it had to have a `name`.
const name = unescapeLeadingUnderscores(typeOnlyDeclaration.name!.escapedText);
Expand Down Expand Up @@ -33359,6 +33362,7 @@ namespace ts {
grammarErrorOnFirstToken(node, Diagnostics.An_export_declaration_cannot_have_modifiers);
}

checkGrammarExportDeclaration(node);
if (!node.moduleSpecifier || checkExternalImportOrExportDeclaration(node)) {
if (node.exportClause) {
// export { x, y }
Expand Down Expand Up @@ -33391,6 +33395,14 @@ namespace ts {
}
}

function checkGrammarExportDeclaration(node: ExportDeclaration): boolean {
const isTypeOnlyExportStar = node.isTypeOnly && node.exportClause?.kind !== SyntaxKind.NamedExports;
if (isTypeOnlyExportStar) {
grammarErrorOnNode(node, Diagnostics.Only_named_exports_may_use_export_type);
}
return !isTypeOnlyExportStar;
}

function checkGrammarModuleElementContext(node: Statement, errorMessage: DiagnosticMessage): boolean {
const isInAppropriateContext = node.parent.kind === SyntaxKind.SourceFile || node.parent.kind === SyntaxKind.ModuleBlock || node.parent.kind === SyntaxKind.ModuleDeclaration;
if (!isInAppropriateContext) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,10 @@
"category": "Error",
"code": 1380
},
"Only named exports may use 'export type'.": {
"category": "Error",
"code": 1383
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down
7 changes: 4 additions & 3 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6565,18 +6565,19 @@ namespace ts {
return finishNode(node);
}

function parseNamespaceExport(): NamespaceExport {
const node = <NamespaceExport>createNode(SyntaxKind.NamespaceExport);
function parseNamespaceExport(pos: number): NamespaceExport {
const node = <NamespaceExport>createNode(SyntaxKind.NamespaceExport, pos);
node.name = parseIdentifier();
return finishNode(node);
}

function parseExportDeclaration(node: ExportDeclaration): ExportDeclaration {
node.kind = SyntaxKind.ExportDeclaration;
node.isTypeOnly = parseOptional(SyntaxKind.TypeKeyword);
const namespaceExportPos = scanner.getStartPos();
if (parseOptional(SyntaxKind.AsteriskToken)) {
if (parseOptional(SyntaxKind.AsKeyword)) {
node.exportClause = parseNamespaceExport();
node.exportClause = parseNamespaceExport(namespaceExportPos);
}
parseExpected(SyntaxKind.FromKeyword);
node.moduleSpecifier = parseModuleSpecifier();
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6172,6 +6172,10 @@ namespace ts {
|| !isExpressionNode(useSite);
}

export function typeOnlyDeclarationIsExport(typeOnlyDeclaration: Node) {
return typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier;
}

function isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(node: Node) {
while (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression) {
node = node.parent;
Expand Down
31 changes: 31 additions & 0 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,37 @@ namespace FourSlash {
}
}

public baselineRename(marker: string, options: FourSlashInterface.RenameOptions) {
const position = this.getMarkerByName(marker).position;
const locations = this.languageService.findRenameLocations(
this.activeFile.fileName,
position,
options.findInStrings ?? false,
options.findInComments ?? false,
options.providePrefixAndSuffixTextForRename);

if (!locations) {
this.raiseError(`baselineRename failed. Could not rename at the provided position.`);
}

const renamesByFile = ts.group(locations, l => l.fileName);
const baselineContent = renamesByFile.map(renames => {
const { fileName } = renames[0];
const sortedRenames = ts.sort(renames, (a, b) => b.textSpan.start - a.textSpan.start);
let baselineFileContent = this.getFileContent(fileName);
for (const { textSpan } of sortedRenames) {
const isOriginalSpan = fileName === this.activeFile.fileName && ts.textSpanIntersectsWithPosition(textSpan, position);
baselineFileContent =
baselineFileContent.slice(0, textSpan.start) +
(isOriginalSpan ? "[|RENAME|]" : "RENAME") +
baselineFileContent.slice(textSpan.start + textSpan.length);
}
return `/*====== ${fileName} ======*/\n\n${baselineFileContent}`;
}).join("\n\n") + "\n";

Harness.Baseline.runBaseline(this.getBaselineFileNameForContainingTestFile(), baselineContent);
}

public verifyQuickInfoExists(negative: boolean) {
const actualQuickInfo = this.languageService.getQuickInfoAtPosition(this.activeFile.fileName, this.currentCaretPosition);
if (negative) {
Expand Down
9 changes: 9 additions & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ namespace FourSlashInterface {
this.state.verifyRenameLocations(startRanges, options);
}

public baselineRename(marker: string, options: RenameOptions) {
this.state.baselineRename(marker, options);
}

public verifyQuickInfoDisplayParts(kind: string, kindModifiers: string, textSpan: FourSlash.TextSpan,
displayParts: ts.SymbolDisplayPart[], documentation: ts.SymbolDisplayPart[], tags: ts.JSDocTagInfo[]) {
this.state.verifyQuickInfoDisplayParts(kind, kindModifiers, textSpan, displayParts, documentation, tags);
Expand Down Expand Up @@ -1623,4 +1627,9 @@ namespace FourSlashInterface {
template: string
};
export type RenameLocationOptions = FourSlash.Range | { readonly range: FourSlash.Range, readonly prefixText?: string, readonly suffixText?: string };
export interface RenameOptions {
readonly findInStrings?: boolean;
readonly findInComments?: boolean;
readonly providePrefixAndSuffixTextForRename?: boolean;
};
}
10 changes: 6 additions & 4 deletions src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1927,10 +1927,12 @@ namespace ts.FindAllReferences {
}

const exportSpecifier = getDeclarationOfKind<ExportSpecifier>(symbol, SyntaxKind.ExportSpecifier);
const localSymbol = exportSpecifier && checker.getExportSpecifierLocalTargetSymbol(exportSpecifier);
if (localSymbol) {
const res = cbSymbol(localSymbol, /*rootSymbol*/ undefined, /*baseSymbol*/ undefined, EntryKind.Node);
if (res) return res;
if (!isForRenamePopulateSearchSymbolSet || exportSpecifier && !exportSpecifier.propertyName) {
const localSymbol = exportSpecifier && checker.getExportSpecifierLocalTargetSymbol(exportSpecifier);
if (localSymbol) {
const res = cbSymbol(localSymbol, /*rootSymbol*/ undefined, /*baseSymbol*/ undefined, EntryKind.Node);
if (res) return res;
}
}

// symbolAtLocation for a binding element is the local symbol. See if the search symbol is the property.
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/exportAsNamespace1(module=amd).symbols
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const b = 2;

=== tests/cases/conformance/es2020/modules/1.ts ===
export * as ns from './0';
>ns : Symbol(ns, Decl(1.ts, 0, 11))
>ns : Symbol(ns, Decl(1.ts, 0, 6))

ns.a;
ns.b;
Expand All @@ -18,15 +18,15 @@ import * as foo from './1'

foo.ns.a;
>foo.ns.a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))

foo.ns.b;
>foo.ns.b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const b = 2;

=== tests/cases/conformance/es2020/modules/1.ts ===
export * as ns from './0';
>ns : Symbol(ns, Decl(1.ts, 0, 11))
>ns : Symbol(ns, Decl(1.ts, 0, 6))

ns.a;
ns.b;
Expand All @@ -18,15 +18,15 @@ import * as foo from './1'

foo.ns.a;
>foo.ns.a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))

foo.ns.b;
>foo.ns.b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const b = 2;

=== tests/cases/conformance/es2020/modules/1.ts ===
export * as ns from './0';
>ns : Symbol(ns, Decl(1.ts, 0, 11))
>ns : Symbol(ns, Decl(1.ts, 0, 6))

ns.a;
ns.b;
Expand All @@ -18,15 +18,15 @@ import * as foo from './1'

foo.ns.a;
>foo.ns.a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))

foo.ns.b;
>foo.ns.b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const b = 2;

=== tests/cases/conformance/es2020/modules/1.ts ===
export * as ns from './0';
>ns : Symbol(ns, Decl(1.ts, 0, 11))
>ns : Symbol(ns, Decl(1.ts, 0, 6))

ns.a;
ns.b;
Expand All @@ -18,15 +18,15 @@ import * as foo from './1'

foo.ns.a;
>foo.ns.a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))

foo.ns.b;
>foo.ns.b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const b = 2;

=== tests/cases/conformance/es2020/modules/1.ts ===
export * as ns from './0';
>ns : Symbol(ns, Decl(1.ts, 0, 11))
>ns : Symbol(ns, Decl(1.ts, 0, 6))

ns.a;
ns.b;
Expand All @@ -18,15 +18,15 @@ import * as foo from './1'

foo.ns.a;
>foo.ns.a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))

foo.ns.b;
>foo.ns.b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))

10 changes: 5 additions & 5 deletions tests/baselines/reference/exportAsNamespace1(module=umd).symbols
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const b = 2;

=== tests/cases/conformance/es2020/modules/1.ts ===
export * as ns from './0';
>ns : Symbol(ns, Decl(1.ts, 0, 11))
>ns : Symbol(ns, Decl(1.ts, 0, 6))

ns.a;
ns.b;
Expand All @@ -18,15 +18,15 @@ import * as foo from './1'

foo.ns.a;
>foo.ns.a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))

foo.ns.b;
>foo.ns.b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))

10 changes: 5 additions & 5 deletions tests/baselines/reference/exportAsNamespace2(module=amd).symbols
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const b = 2;

=== tests/cases/conformance/es2020/modules/1.ts ===
export * as ns from './0';
>ns : Symbol(ns, Decl(1.ts, 0, 11))
>ns : Symbol(ns, Decl(1.ts, 0, 6))

ns.a;
ns.b;
Expand All @@ -18,15 +18,15 @@ import * as foo from './1'

foo.ns.a;
>foo.ns.a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>a : Symbol(foo.ns.a, Decl(0.ts, 0, 12))

foo.ns.b;
>foo.ns.b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>foo.ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>foo : Symbol(foo, Decl(2.ts, 0, 6))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 11))
>ns : Symbol(foo.ns, Decl(1.ts, 0, 6))
>b : Symbol(foo.ns.b, Decl(0.ts, 1, 12))

Loading