address PR comments

This commit is contained in:
Jesse Trinity 2020-06-02 19:06:12 -07:00
parent ca58c0e03c
commit d88ea4e1f8
12 changed files with 37 additions and 16 deletions

View File

@ -104,9 +104,9 @@ namespace ts.codefix {
return modifierFlags;
}
export function getAccessorConvertiblePropertyAtPosition(file: SourceFile, start: number, end: number, userRequested = true): Info | undefined {
export function getAccessorConvertiblePropertyAtPosition(file: SourceFile, start: number, end: number, considerEmptySpans = true): Info | undefined {
const node = getTokenAtPosition(file, start);
const cursorRequest = start === end && userRequested;
const cursorRequest = start === end && considerEmptySpans;
const declaration = findAncestor(node.parent, isAcceptedDeclaration);
// make sure declaration have AccessibilityModifier or Static Modifier or Readonly Modifier
const meaning = ModifierFlags.AccessibilityModifier | ModifierFlags.Static | ModifierFlags.Readonly;

View File

@ -70,12 +70,12 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
return { renameFilename: undefined, renameLocation: undefined, edits };
}
function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number, userRequested = true): Info | undefined {
function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number, considerFunctionBodies = true): Info | undefined {
const node = getTokenAtPosition(file, startPosition);
const func = getContainingFunction(node);
// Only offer a refactor in the function body on explicit refactor requests.
if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node)
|| (rangeContainsRange(func.body, node) && !userRequested))) return undefined;
|| (rangeContainsRange(func.body, node) && !considerFunctionBodies))) return undefined;
if (isExpression(func.body)) {
return {

View File

@ -27,12 +27,11 @@ namespace ts.refactor {
readonly exportingModuleSymbol: Symbol;
}
function getInfo(context: RefactorContext, userRequested = true): Info | undefined {
function getInfo(context: RefactorContext, considerPartialSpans = true): Info | undefined {
const { file } = context;
const span = getRefactorContextSpan(context);
const token = getTokenAtPosition(file, span.start);
const cursorRequest = userRequested && span;
const exportNode = !!(getSyntacticModifierFlags(token.parent) & ModifierFlags.Export) && cursorRequest ? token.parent : getParentNodeInSpan(token, file, span);
const exportNode = !!(token.parent && getSyntacticModifierFlags(token.parent) & ModifierFlags.Export) && considerPartialSpans ? token.parent : getParentNodeInSpan(token, file, span);
if (!exportNode || (!isSourceFile(exportNode.parent) && !(isModuleBlock(exportNode.parent) && isAmbientModule(exportNode.parent.parent)))) {
return undefined;
}

View File

@ -19,12 +19,11 @@ namespace ts.refactor {
});
// Can convert imports of the form `import * as m from "m";` or `import d, { x, y } from "m";`.
function getImportToConvert(context: RefactorContext, userRequested = true): NamedImportBindings | undefined {
function getImportToConvert(context: RefactorContext, considerPartialSpans = true): NamedImportBindings | undefined {
const { file } = context;
const span = getRefactorContextSpan(context);
const token = getTokenAtPosition(file, span.start);
const cursorRequest = userRequested && span.length === 0;
const importDecl = cursorRequest ? findAncestor(token, isImportDeclaration) : getParentNodeInSpan(token, file, span);
const importDecl = considerPartialSpans ? findAncestor(token, isImportDeclaration) : getParentNodeInSpan(token, file, span);
if (!importDecl || !isImportDeclaration(importDecl) || (importDecl.getEnd() < span.start + span.length)) return undefined;
const { importClause } = importDecl;
return importClause && importClause.namedBindings;

View File

@ -186,12 +186,12 @@ namespace ts.refactor.extractSymbol {
* not shown to the user, but can be used by us diagnostically)
*/
// exported only for tests
export function getRangeToExtract(sourceFile: SourceFile, span: TextSpan, userRequested = true): RangeToExtract {
export function getRangeToExtract(sourceFile: SourceFile, span: TextSpan, considerEmptySpans = true): RangeToExtract {
const { length } = span;
if (length === 0 && !userRequested) {
if (length === 0 && !considerEmptySpans) {
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.cannotExtractEmpty)] };
}
const cursorRequest = length === 0 && userRequested;
const cursorRequest = length === 0 && considerEmptySpans;
// Walk up starting from the the start position until we find a non-SourceFile node that subsumes the selected span.
// This may fail (e.g. you select two statements in the root of a source file)

View File

@ -58,12 +58,12 @@ namespace ts.refactor {
type Info = TypeAliasInfo | InterfaceInfo;
function getRangeToExtract(context: RefactorContext, userRequested = true): Info | undefined {
function getRangeToExtract(context: RefactorContext, considerEmptySpans = true): Info | undefined {
const { file, startPosition } = context;
const isJS = isSourceFileJS(file);
const current = getTokenAtPosition(file, startPosition);
const range = createTextRangeFromSpan(getRefactorContextSpan(context));
const cursorRequest = range.pos === range.end && userRequested;
const cursorRequest = range.pos === range.end && considerEmptySpans;
const selection = findAncestor(current, (node => node.parent && isTypeNode(node) && !rangeContainsSkipTrivia(range, node.parent, file) &&
(cursorRequest || nodeOverlapsWithStartEnd(current, file, range.pos, range.end))));

View File

@ -2,7 +2,7 @@
//// const a = (a: number) => { return/*a*//*b*/ a; };
// Only offer refactor for empty span if explicity requested
// Only offer refactor for empty span in body if explicity requested
goTo.select("a", "b");
verify.not.refactorAvailableForTriggerReason("implicit", "Add or remove braces in an arrow function");
verify.refactorAvailableForTriggerReason("invoked", "Add or remove braces in an arrow function", "Remove braces from arrow function");

View File

@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />
//// const a = (a: number) => { re/*a*/tur/*b*/n a; };
// Only offer refactor in body if explicity requested
goTo.select("a", "b");
verify.not.refactorAvailableForTriggerReason("implicit", "Add or remove braces in an arrow function");
verify.refactorAvailableForTriggerReason("invoked", "Add or remove braces in an arrow function", "Remove braces from arrow function");

View File

@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />
////import d, * as /*a*/n/*b*/ from "m";
// Only offer refactor for sub span if explicity requested
goTo.select("a", "b");
verify.not.refactorAvailableForTriggerReason("implicit", "Convert import");
verify.refactorAvailableForTriggerReason("invoked", "Convert import");

View File

@ -0,0 +1,7 @@
/// <reference path='fourslash.ts' />
//// var x: s/*a*/tr/*b*/ing;
goTo.select("a", "b");
verify.refactorAvailableForTriggerReason("implicit", "Extract type");
verify.refactorAvailableForTriggerReason("invoked", "Extract type");