Skip to content

Commit

Permalink
fix(migrations): migrating chained members
Browse files Browse the repository at this point in the history
  • Loading branch information
jackofdiamond5 committed Jul 20, 2021
1 parent bdf3734 commit ae6e1e5
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions projects/igniteui-angular/migrations/common/tsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export const NG_CORE_PACKAGE_NAME = '@angular/core';
export const CUSTOM_TS_PLUGIN_PATH = './tsPlugin';
export const CUSTOM_TS_PLUGIN_NAME = 'igx-ts-plugin';

enum SynaxTokens {
ClosingParenthesis = ')',
MemberAccess = '.'
}

/** Returns a source file */
// export function getFileSource(sourceText: string): ts.SourceFile {
// return ts.createSourceFile('', sourceText, ts.ScriptTarget.Latest, true);
Expand Down Expand Up @@ -245,7 +250,8 @@ const getTypeDefinitions = (langServ: tss.LanguageService, entryPath: string, po
* @param position Index of identifier
*/
export const getTypeDefinitionAtPosition =
(langServ: tss.LanguageService, entryPath: string, position: number): Pick<tss.DefinitionInfo, 'name' | 'fileName'> | null => {
(langServ: tss.LanguageService, entryPath: string, position: number):
Pick<tss.DefinitionInfo, 'name' | 'fileName' | 'containerName'> | null => {
const definition = langServ.getDefinitionAndBoundSpan(entryPath, position)?.definitions[0];
if (!definition) {
return null;
Expand All @@ -265,7 +271,12 @@ export const getTypeDefinitionAtPosition =

// quick info (and getImplementationAtPosition) have the return type as last of the displayParts
// check if it's a className (potentially Ignite comp) and use for definition name:
definition.name = maybeReturnType.kind === 'className' ? maybeReturnType.text : '';
// TODO: consider methods that return enums?
definition.name = maybeReturnType.kind === 'className'
|| maybeReturnType.kind === 'interfaceName'
? maybeReturnType.text
: '';

return definition;
}
let typeDefs = getTypeDefinitions(langServ, definition.fileName || entryPath, definition.textSpan.start);
Expand Down Expand Up @@ -314,16 +325,45 @@ export const getTypeDefinitionAtPosition =
return null;
};


/**
* Determines if a member belongs to a type in the `igniteui-angular` toolkit.
*
* @param change The change that will be applied.
* @param langServ The Typescript/Angular Language Service
* @param entryPath Relative file path.
* @param matchPosition The position of the identifier.
*/
export const isMemberIgniteUI =
(change: MemberChange, langServ: tss.LanguageService, entryPath: string, matchPosition: number): boolean => {
const prevChar = langServ.getProgram().getSourceFile(entryPath).getText().substr(matchPosition - 2, 1);
if (prevChar === ')') {
const content = langServ.getProgram().getSourceFile(entryPath).getText();
matchPosition = shiftMatchPosition(matchPosition, content);
const prevChar = content.substr(matchPosition - 1, 1);
if (prevChar === SynaxTokens.ClosingParenthesis) {
// methodCall().identifier
matchPosition = langServ.getBraceMatchingAtPosition(entryPath, matchPosition - 2)[0]?.start ?? matchPosition;
matchPosition = langServ.getBraceMatchingAtPosition(entryPath, matchPosition - 1)[0]?.start ?? matchPosition;
}

const typeDef = getTypeDefinitionAtPosition(langServ, entryPath, matchPosition);
if (!typeDef) {
return false;
}
const typeDef = getTypeDefinitionAtPosition(langServ, entryPath, matchPosition - 1);
return !typeDef ? false : typeDef.fileName.includes(IG_PACKAGE_NAME) && change.definedIn.indexOf(typeDef.name) !== -1;

return typeDef.fileName.includes(IG_PACKAGE_NAME)
&& change.definedIn.indexOf(typeDef.name) !== -1;
};

/**
* Shifts the match position of the identifier to the left
* until any character other than an empty string or a '.' is reached. #9347
*/
const shiftMatchPosition = (matchPosition: number, content: string): number => {
do {
matchPosition--;
} while (matchPosition > 0
&& !content[matchPosition - 1].trim()
|| content[matchPosition - 1] === SynaxTokens.MemberAccess);
return matchPosition;
};

//#endregion

0 comments on commit ae6e1e5

Please sign in to comment.