Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update type-only import semantics to allow type queries#36092

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
andrewbranch merged 27 commits intomicrosoft:masterfromandrewbranch:type-only-2
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
27 commits
Select commitHold shift + click to select a range
b40284c
Change type-only semantics to allow type queries
andrewbranchJan 8, 2020
2ae9edc
Don’t error using type-only import in ambient context
andrewbranchJan 8, 2020
db93eb2
Fix default import
andrewbranchJan 8, 2020
a2548c8
Fix namespace import
andrewbranchJan 8, 2020
8d3f167
Update more baselines
andrewbranchJan 8, 2020
b56ad7d
Prevent circular resolution
andrewbranchJan 8, 2020
0547a3d
Track const enum expression usage
andrewbranchJan 9, 2020
124dcd6
Update baselines
andrewbranchJan 9, 2020
2dd3690
Perf tuning 1
andrewbranchJan 10, 2020
eefa335
Test commit for perf impact
andrewbranchJan 10, 2020
9a24cba
Weave type-only alias declaration finding into alias resolution
andrewbranchJan 13, 2020
875349d
Fix namespace import of type-only exported symbols
andrewbranchJan 14, 2020
171e314
type-only exports do not contribute to the module object type
andrewbranchJan 14, 2020
910dd84
Update APIs
andrewbranchJan 14, 2020
2a7c472
Fix enum casing, remove type-only conversion suggestion
andrewbranchJan 14, 2020
11ba11a
Short circuit type-only checks in resolveEntityName faster
andrewbranchJan 14, 2020
0c28994
Fix casing in API
andrewbranchJan 14, 2020
40a2c3c
Remove unused parameter
andrewbranchJan 14, 2020
1cbfb81
Merge branch 'master' into type-only-2
andrewbranchJan 14, 2020
124d746
Fix error on qualified names in type queries
andrewbranchJan 14, 2020
bbbcbd5
Merge branch 'master' into type-only-2
andrewbranchJan 15, 2020
1044c5c
Allow type-only imports in computed property names
andrewbranchJan 15, 2020
a5ca492
Fix computed property names of types and abstract members
andrewbranchJan 16, 2020
8571a8a
Remove unused util
andrewbranchJan 16, 2020
8b4c235
Commit missing baselines
andrewbranchJan 16, 2020
be5f50f
Merge branch master into type-only-2
andrewbranchJan 21, 2020
19b3206
Rename “check” functions so as not to overload the word “check”
andrewbranchJan 23, 2020
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
PrevPrevious commit
NextNext commit
Track const enum expression usage
  • Loading branch information
@andrewbranch
andrewbranch committedJan 9, 2020
commit0547a3dc2dfdcbd3d52ac37827d5e1a1f802bce2
43 changes: 26 additions & 17 deletionssrc/compiler/checker.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2532,6 +2532,15 @@ namespace ts {
}
}

// Aliases that resolve to const enums are not marked as referenced because they are not emitted,
// but their usage in value positions must be tracked to determine if the import can be type-only.
function markConstEnumAliasAsReferenced(symbol: Symbol) {
const links = getSymbolLinks(symbol);
if (!links.constEnumReferenced) {
links.constEnumReferenced = true;
}
}

// This function is only for imports with entity names
function getSymbolOfPartOfRightHandSideOfImportEquals(entityName: EntityName, dontResolveAlias?: boolean): Symbol | undefined {
// There are three things we might try to look for. In the following examples,
Expand DownExpand Up@@ -20211,12 +20220,13 @@ namespace ts {
}

function markAliasReferenced(symbol: Symbol, location: Node) {
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value) &&
!isInTypeQuery(location) &&
((compilerOptions.preserveConstEnums && isExportOrExportExpression(location)) || !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) &&
!getTypeOnlyAliasDeclaration(symbol)
) {
markAliasSymbolAsReferenced(symbol);
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value) && !isInTypeQuery(location) && !getTypeOnlyAliasDeclaration(symbol)) {
if (compilerOptions.preserveConstEnums && isExportOrExportExpression(location) || !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) {
markAliasSymbolAsReferenced(symbol);
}
else {
markConstEnumAliasAsReferenced(symbol);
}
}
}

Expand DownExpand Up@@ -33045,17 +33055,15 @@ namespace ts {
}

function importClauseContainsReferencedImport(importClause: ImportClause) {
return importClause.name && isReferenced(importClause)
|| importClause.namedBindings && namedBindingsContainsReferencedImport(importClause.namedBindings);
return forEachImportClauseDeclaration(importClause, declaration => {
return !!getSymbolOfNode(declaration).isReferenced;
});
}

function isReferenced(declaration: Declaration) {
return !!getMergedSymbol(getSymbolOfNode(declaration)).isReferenced;
}
function namedBindingsContainsReferencedImport(namedBindings: NamedImportBindings) {
return isNamespaceImport(namedBindings)
? isReferenced(namedBindings)
: some(namedBindings.elements, isReferenced);
}
function importClauseContainsConstEnumUsedAsValue(importClause: ImportClause) {
return forEachImportClauseDeclaration(importClause, declaration => {
return !!getSymbolLinks(getSymbolOfNode(declaration)).constEnumReferenced;
});
}

function checkImportsForTypeOnlyConversion(sourceFile: SourceFile) {
Expand All@@ -33065,7 +33073,8 @@ namespace ts {
statement.importClause &&
!statement.importClause.isTypeOnly &&
importClauseContainsReferencedImport(statement.importClause) &&
!isReferencedAliasDeclaration(statement.importClause, /*checkChildren*/ true)
!isReferencedAliasDeclaration(statement.importClause, /*checkChildren*/ true) &&
!importClauseContainsConstEnumUsedAsValue(statement.importClause)
) {
const isError = compilerOptions.importsNotUsedAsValues === importsNotUsedAsValues.Error;
errorOrSuggestion(
Expand Down
3 changes: 2 additions & 1 deletionsrc/compiler/types.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4059,7 +4059,8 @@ namespace ts {
instantiations?: Map<Type>; // Instantiations of generic type alias (undefined if non-generic)
inferredClassSymbol?: Map<TransientSymbol>; // Symbol of an inferred ES5 constructor function
mapper?: TypeMapper; // Type mapper for instantiation alias
referenced?: boolean; // True if alias symbol has been referenced as a value
referenced?: boolean; // True if alias symbol has been referenced as a value that can be emitted
constEnumReferenced?: boolean; // True if alias symbol resolves to a const enum and is referenced as a value ('referenced' will be false)
containingType?: UnionOrIntersectionType; // Containing union or intersection type for synthetic property
leftSpread?: Symbol; // Left source for synthetic spread property
rightSpread?: Symbol; // Right source for synthetic spread property
Expand Down
13 changes: 13 additions & 0 deletionssrc/compiler/utilities.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2273,6 +2273,19 @@ namespace ts {
return node.kind === SyntaxKind.ImportDeclaration && !!node.importClause && !!node.importClause.name;
}

export function forEachImportClauseDeclaration<T>(node: ImportClause, action: (declaration: ImportClause | NamespaceImport | ImportSpecifier) => T | undefined): T | undefined {
if (node.name) {
const result = action(node);
if (result) return result;
}
if (node.namedBindings) {
const result = isNamespaceImport(node.namedBindings)
? action(node.namedBindings)
: forEach(node.namedBindings.elements, action);
if (result) return result;
}
}

export function hasQuestionToken(node: Node) {
if (node) {
switch (node.kind) {
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,7 @@
export default class {}
export class A {}
export type B = {};
export const enum C { One, Two }

// @Filename: /b.ts
import { A, B } from './a'; // Error
Expand All@@ -26,3 +27,17 @@ console.log(a, b);

// @Filename: /e.ts
import { A, B } from './a'; // noUnusedLocals error only

// @Filename: /f.ts
import { C } from './a';
import type { C as D } from './a';
C.One;
let c: D = C.Two;
let d: D.Two = C.Two;
console.log(c, d);

// @Filename: /g.ts
import { C } from './a';
let c: C;
let d: C.Two;
console.log(c, d);

[8]ページ先頭

©2009-2025 Movatter.jp