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 14, 2021
1 parent bdf3734 commit de0bc58
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 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 SpecialIdentifiers {
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 Down Expand Up @@ -314,16 +320,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 === SpecialIdentifiers.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
|| change.definedIn.indexOf(typeDef.containerName) !== -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 (!content[matchPosition - 1].trim() || content[matchPosition - 1] === SpecialIdentifiers.MemberAccess);
return matchPosition;
};

//#endregion

0 comments on commit de0bc58

Please sign in to comment.