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

// 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 Down Expand 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 Down Expand 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 deletion src/compiler/types.ts
Original file line number Diff line number Diff 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 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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);