-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
166 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import type { CodemodOptions } from '../../types/index.js'; | ||
import { createOptions } from '../../utils/steps/create-options.js'; | ||
import { updateClasses } from '../../utils/steps/update-classes.js'; | ||
import { updateProject } from '../../utils/steps/update-project.js'; | ||
|
||
export function migrateEmberApp(codemodOptions: CodemodOptions): void { | ||
const options = createOptions(codemodOptions); | ||
|
||
updateClasses(['app/**/*.{js,ts}'], options); | ||
updateProject(['app/**/*.{js,ts}'], options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import type { CodemodOptions } from '../../types/index.js'; | ||
import { createOptions } from '../../utils/steps/create-options.js'; | ||
import { updateClasses } from '../../utils/steps/update-classes.js'; | ||
import { updateProject } from '../../utils/steps/update-project.js'; | ||
|
||
export function migrateEmberV2Addon(codemodOptions: CodemodOptions): void { | ||
const options = createOptions(codemodOptions); | ||
|
||
updateClasses(['src/**/*.{js,ts}'], options); | ||
updateProject(['src/**/*.{js,ts}'], options); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { AST } from '@codemod-utils/ast-javascript'; | ||
|
||
function isValueImport( | ||
importKind: 'type' | 'typeof' | 'value' | undefined, | ||
): boolean { | ||
return importKind === undefined || importKind === 'value'; | ||
} | ||
|
||
function updateImportStatement( | ||
file: string, | ||
data: { | ||
isTypeScript: boolean; | ||
}, | ||
): { | ||
localName: string | undefined; | ||
newFile: string; | ||
} { | ||
let localName: string | undefined; | ||
|
||
const traverse = AST.traverse(data.isTypeScript); | ||
|
||
const ast = traverse(file, { | ||
visitImportDeclaration(path) { | ||
const { importKind, source, specifiers } = path.node; | ||
|
||
if (!isValueImport(importKind)) { | ||
return false; | ||
} | ||
|
||
if (source.type !== 'Literal' && source.type !== 'StringLiteral') { | ||
return false; | ||
} | ||
|
||
if (source.value !== '@ember/service' || !Array.isArray(specifiers)) { | ||
return false; | ||
} | ||
|
||
path.node.specifiers = specifiers.map((specifier) => { | ||
// @ts-expect-error: 'specifier.importKind' exists | ||
if (!isValueImport(specifier.importKind)) { | ||
return specifier; | ||
} | ||
|
||
if (specifier.type !== 'ImportSpecifier') { | ||
return specifier; | ||
} | ||
|
||
if ( | ||
specifier.imported.name !== 'inject' && | ||
specifier.imported.name !== 'service' | ||
) { | ||
return specifier; | ||
} | ||
|
||
localName = specifier.local!.name as string; | ||
|
||
return AST.builders.importSpecifier(AST.builders.identifier('service')); | ||
}); | ||
|
||
return false; | ||
}, | ||
}); | ||
|
||
return { | ||
localName, | ||
newFile: AST.print(ast), | ||
}; | ||
} | ||
|
||
function updateServiceDecorators( | ||
file: string, | ||
data: { | ||
isTypeScript: boolean; | ||
localName: string; | ||
}, | ||
): string { | ||
const traverse = AST.traverse(data.isTypeScript); | ||
|
||
const ast = traverse(file, { | ||
visitCallExpression(node) { | ||
this.traverse(node); | ||
|
||
switch (node.value.callee.type) { | ||
case 'Identifier': { | ||
if (node.value.callee.name === data.localName) { | ||
node.value.callee.name = 'service'; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
return false; | ||
}, | ||
|
||
visitClassProperty(node) { | ||
if ( | ||
!Array.isArray(node.value.decorators) || | ||
node.value.decorators.length !== 1 | ||
) { | ||
return false; | ||
} | ||
|
||
const decorator = node.value.decorators[0]; | ||
let isMatch = false; | ||
|
||
switch (decorator.expression.type) { | ||
case 'CallExpression': { | ||
if (decorator.expression.callee.name === data.localName) { | ||
decorator.expression.callee.name = 'service'; | ||
isMatch = true; | ||
} | ||
|
||
break; | ||
} | ||
|
||
case 'Identifier': { | ||
if (decorator.expression.name === data.localName) { | ||
decorator.expression.name = 'service'; | ||
isMatch = true; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (isMatch && data.isTypeScript) { | ||
node.value.accessibility = null; | ||
node.value.declare = true; | ||
node.value.definite = null; | ||
node.value.readonly = null; | ||
} | ||
|
||
return false; | ||
}, | ||
}); | ||
|
||
return AST.print(ast); | ||
} | ||
|
||
export function updateClass(file: string, isTypeScript: boolean): string { | ||
// eslint-disable-next-line prefer-const | ||
let { localName, newFile } = updateImportStatement(file, { | ||
isTypeScript, | ||
}); | ||
|
||
if (!localName) { | ||
return file; | ||
} | ||
|
||
newFile = updateServiceDecorators(newFile, { | ||
isTypeScript, | ||
localName, | ||
}); | ||
|
||
return newFile; | ||
} |