Skip to content

Commit

Permalink
Merge branch 'main' into scan-bigger/fewer-jsdoc-tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
sandersn committed Mar 6, 2023
2 parents dd92575 + 7e4592e commit 76813c0
Show file tree
Hide file tree
Showing 30 changed files with 1,694 additions and 274 deletions.
380 changes: 190 additions & 190 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ function initFlowNode<T extends FlowNode>(node: T) {
return node;
}

const binder = createBinder();
const binder = /* @__PURE__ */ createBinder();

/** @internal */
export function bindSourceFile(file: SourceFile, options: CompilerOptions) {
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19107,6 +19107,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return isTypeRelatedTo(source, target, subtypeRelation);
}

function isTypeStrictSubtypeOf(source: Type, target: Type): boolean {
return isTypeRelatedTo(source, target, strictSubtypeRelation);
}

function isTypeAssignableTo(source: Type, target: Type): boolean {
return isTypeRelatedTo(source, target, assignableRelation);
}
Expand Down Expand Up @@ -27266,7 +27270,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// prototype object types.
const directlyRelated = mapType(matching || type, checkDerived ?
t => isTypeDerivedFrom(t, c) ? t : isTypeDerivedFrom(c, t) ? c : neverType :
t => isTypeSubtypeOf(c, t) && !isTypeIdenticalTo(c, t) ? c : isTypeSubtypeOf(t, c) ? t : neverType);
t => isTypeStrictSubtypeOf(t, c) ? t : isTypeStrictSubtypeOf(c, t) ? c : isTypeSubtypeOf(t, c) ? t : isTypeSubtypeOf(c, t) ? c : neverType);
// If no constituents are directly related, create intersections for any generic constituents that
// are related by constraint.
return directlyRelated.flags & TypeFlags.Never ?
Expand Down
2 changes: 2 additions & 0 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4500,6 +4500,8 @@ function tryGetObjectLikeCompletionContainer(contextToken: Node | undefined): Ob
break;
case SyntaxKind.AsteriskToken:
return isMethodDeclaration(parent) ? tryCast(parent.parent, isObjectLiteralExpression) : undefined;
case SyntaxKind.AsyncKeyword:
return tryCast(parent.parent, isObjectLiteralExpression);
case SyntaxKind.Identifier:
return (contextToken as Identifier).text === "async" && isShorthandPropertyAssignment(contextToken.parent)
? contextToken.parent.parent : undefined;
Expand Down
67 changes: 49 additions & 18 deletions src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function organizeImports(
const shouldCombine = shouldSort; // These are currently inseparable, but I draw a distinction for clarity and in case we add modes in the future.
const shouldRemove = mode === OrganizeImportsMode.RemoveUnused || mode === OrganizeImportsMode.All;
// All of the old ImportDeclarations in the file, in syntactic order.
const topLevelImportGroupDecls = groupImportsByNewlineContiguous(sourceFile, sourceFile.statements.filter(isImportDeclaration));
const topLevelImportGroupDecls = groupByNewlineContiguous(sourceFile, sourceFile.statements.filter(isImportDeclaration));

const comparer = getOrganizeImportsComparerWithDetection(preferences, shouldSort ? () => detectSortingWorker(topLevelImportGroupDecls, preferences) === SortKind.CaseInsensitive : undefined);

Expand All @@ -105,14 +105,14 @@ export function organizeImports(
// Exports are always used
if (mode !== OrganizeImportsMode.RemoveUnused) {
// All of the old ExportDeclarations in the file, in syntactic order.
const topLevelExportDecls = sourceFile.statements.filter(isExportDeclaration);
organizeImportsWorker(topLevelExportDecls, group => coalesceExportsWorker(group, comparer));
getTopLevelExportGroups(sourceFile).forEach(exportGroupDecl =>
organizeImportsWorker(exportGroupDecl, group => coalesceExportsWorker(group, comparer)));
}

for (const ambientModule of sourceFile.statements.filter(isAmbientModule)) {
if (!ambientModule.body) continue;

const ambientModuleImportGroupDecls = groupImportsByNewlineContiguous(sourceFile, ambientModule.body.statements.filter(isImportDeclaration));
const ambientModuleImportGroupDecls = groupByNewlineContiguous(sourceFile, ambientModule.body.statements.filter(isImportDeclaration));
ambientModuleImportGroupDecls.forEach(importGroupDecl => organizeImportsWorker(importGroupDecl, processImportsOfSameModuleSpecifier));

// Exports are always used
Expand Down Expand Up @@ -146,7 +146,7 @@ export function organizeImports(
? stableSort(oldImportGroups, (group1, group2) => compareModuleSpecifiersWorker(group1[0].moduleSpecifier, group2[0].moduleSpecifier, comparer))
: oldImportGroups;
const newImportDecls = flatMap(sortedImportGroups, importGroup =>
getExternalModuleName(importGroup[0].moduleSpecifier)
getExternalModuleName(importGroup[0].moduleSpecifier) || importGroup[0].moduleSpecifier === undefined
? coalesce(importGroup)
: importGroup);

Expand Down Expand Up @@ -175,30 +175,30 @@ export function organizeImports(
}
}

function groupImportsByNewlineContiguous(sourceFile: SourceFile, importDecls: ImportDeclaration[]): ImportDeclaration[][] {
function groupByNewlineContiguous<T extends ImportDeclaration | ExportDeclaration>(sourceFile: SourceFile, decls: T[]): T[][] {
const scanner = createScanner(sourceFile.languageVersion, /*skipTrivia*/ false, sourceFile.languageVariant);
const groupImports: ImportDeclaration[][] = [];
const group: T[][] = [];
let groupIndex = 0;
for (const topLevelImportDecl of importDecls) {
if (groupImports[groupIndex] && isNewGroup(sourceFile, topLevelImportDecl, scanner)) {
for (const decl of decls) {
if (group[groupIndex] && isNewGroup(sourceFile, decl, scanner)) {
groupIndex++;
}

if (!groupImports[groupIndex]) {
groupImports[groupIndex] = [];
if (!group[groupIndex]) {
group[groupIndex] = [];
}

groupImports[groupIndex].push(topLevelImportDecl);
group[groupIndex].push(decl);
}

return groupImports;
return group;
}

// a new group is created if an import includes at least two new line
// a new group is created if an import/export includes at least two new line
// new line from multi-line comment doesn't count
function isNewGroup(sourceFile: SourceFile, topLevelImportDecl: ImportDeclaration, scanner: Scanner) {
const startPos = topLevelImportDecl.getFullStart();
const endPos = topLevelImportDecl.getStart();
function isNewGroup(sourceFile: SourceFile, decl: ImportDeclaration | ExportDeclaration, scanner: Scanner) {
const startPos = decl.getFullStart();
const endPos = decl.getStart();
scanner.setText(sourceFile.text, startPos, endPos - startPos);

let numberOfNewLines = 0;
Expand Down Expand Up @@ -617,7 +617,7 @@ function getModuleSpecifierExpression(declaration: AnyImportOrRequireStatement):
/** @internal */
export function detectSorting(sourceFile: SourceFile, preferences: UserPreferences): SortKind {
return detectSortingWorker(
groupImportsByNewlineContiguous(sourceFile, sourceFile.statements.filter(isImportDeclaration)),
groupByNewlineContiguous(sourceFile, sourceFile.statements.filter(isImportDeclaration)),
preferences);
}

Expand Down Expand Up @@ -824,3 +824,34 @@ function getOrganizeImportsComparerWithDetection(preferences: UserPreferences, d
const ignoreCase = typeof preferences.organizeImportsIgnoreCase === "boolean" ? preferences.organizeImportsIgnoreCase : detectIgnoreCase?.() ?? false;
return getOrganizeImportsComparer(preferences, ignoreCase);
}

function getTopLevelExportGroups(sourceFile: SourceFile) {
const topLevelExportGroups: ExportDeclaration[][] = [];
const statements = sourceFile.statements;
const len = length(statements);

let i = 0;
let groupIndex = 0;
while (i < len) {
if (isExportDeclaration(statements[i])) {
if (topLevelExportGroups[groupIndex] === undefined) {
topLevelExportGroups[groupIndex] = [];
}
const exportDecl = statements[i] as ExportDeclaration;
if (exportDecl.moduleSpecifier) {
topLevelExportGroups[groupIndex].push(exportDecl);
i++;
}
else {
while (i < len && isExportDeclaration(statements[i])) {
topLevelExportGroups[groupIndex].push(statements[i++] as ExportDeclaration);
}
groupIndex++;
}
}
else {
i++;
}
}
return flatMap(topLevelExportGroups, exportGroupDecls => groupByNewlineContiguous(sourceFile, exportGroupDecls));
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ if (isObject2(obj3)) {
>obj3 : Record<string, unknown>

obj3;
>obj3 : {}
>obj3 : Record<string, unknown>

obj3['attr'];
>obj3['attr'] : any
>obj3 : {}
>obj3['attr'] : unknown
>obj3 : Record<string, unknown>
>'attr' : "attr"
}
// check type after conditional block
Expand All @@ -78,11 +78,11 @@ if (isObject2(obj4)) {
>obj4 : Record<string, unknown> | undefined

obj4;
>obj4 : {}
>obj4 : Record<string, unknown>

obj4['attr'];
>obj4['attr'] : any
>obj4 : {}
>obj4['attr'] : unknown
>obj4 : Record<string, unknown>
>'attr' : "attr"
}
// check type after conditional block
Expand Down
51 changes: 50 additions & 1 deletion tests/baselines/reference/correlatedUnions.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,38 @@ const getStringAndNumberFromOriginalAndMapped = <
nestedKey: N
): [Original[K][N], MappedFromOriginal[K][N]] => {
return [original[key][nestedKey], mappedFromOriginal[key][nestedKey]];
};
};

// repro from #31675
interface Config {
string: string;
number: number;
}

function getConfigOrDefault<T extends keyof Config>(
userConfig: Partial<Config>,
key: T,
defaultValue: Config[T]
): Config[T] {
const userValue = userConfig[key];
const assertedCheck = userValue ? userValue! : defaultValue;
return assertedCheck;
}

// repro from #47523

type Foo1 = {
x: number;
y: string;
};

function getValueConcrete<K extends keyof Foo1>(
o: Partial<Foo1>,
k: K
): Foo1[K] | undefined {
return o[k];
}


//// [correlatedUnions.js]
"use strict";
Expand Down Expand Up @@ -392,6 +423,14 @@ var BAR_LOOKUP = makeCompleteLookupMapping(ALL_BARS, 'name');
var getStringAndNumberFromOriginalAndMapped = function (original, mappedFromOriginal, key, nestedKey) {
return [original[key][nestedKey], mappedFromOriginal[key][nestedKey]];
};
function getConfigOrDefault(userConfig, key, defaultValue) {
var userValue = userConfig[key];
var assertedCheck = userValue ? userValue : defaultValue;
return assertedCheck;
}
function getValueConcrete(o, k) {
return o[k];
}


//// [correlatedUnions.d.ts]
Expand Down Expand Up @@ -562,3 +601,13 @@ type SameKeys<T> = {
};
type MappedFromOriginal = SameKeys<Original>;
declare const getStringAndNumberFromOriginalAndMapped: <K extends keyof Original, N extends keyof Original[K]>(original: Original, mappedFromOriginal: MappedFromOriginal, key: K, nestedKey: N) => [Original[K][N], SameKeys<Original>[K][N]];
interface Config {
string: string;
number: number;
}
declare function getConfigOrDefault<T extends keyof Config>(userConfig: Partial<Config>, key: T, defaultValue: Config[T]): Config[T];
type Foo1 = {
x: number;
y: string;
};
declare function getValueConcrete<K extends keyof Foo1>(o: Partial<Foo1>, k: K): Foo1[K] | undefined;
Loading

0 comments on commit 76813c0

Please sign in to comment.