|
| 1 | +/** Rewrite the imports for modules we moved. */ |
| 2 | + |
| 3 | +let parser = require('recast/parsers/typescript'); |
| 4 | +const j = require("jscodeshift").withParser(parser); |
| 5 | + |
| 6 | +function transform(source) { |
| 7 | + const root = j(source); |
| 8 | + |
| 9 | + // Start by eliminating the classic class `layout` property and corresponding |
| 10 | + // import of the template. |
| 11 | + let layoutProperty = findClassicLayout(root); |
| 12 | + if (layoutProperty) { |
| 13 | + let importDeclaration = findMatchingImport( |
| 14 | + root, |
| 15 | + layoutProperty.node.value.name |
| 16 | + ); |
| 17 | + if (importDeclaration) { |
| 18 | + importDeclaration.parent.prune(); |
| 19 | + } |
| 20 | + layoutProperty.prune(); |
| 21 | + } |
| 22 | + |
| 23 | + // Then do the same for `layout` property set on an ES class via assignment |
| 24 | + // and corresponding import of the template. |
| 25 | + let layoutClassProperties = findClassPropertyLayout(root); |
| 26 | + layoutClassProperties.forEach((lcp) => { |
| 27 | + let importDeclaration = findMatchingImport(root, lcp.node.value.name); |
| 28 | + if (importDeclaration) { |
| 29 | + importDeclaration.parent.prune(); |
| 30 | + } |
| 31 | + lcp.prune(); |
| 32 | + }); |
| 33 | + |
| 34 | + // Finally, remove the `layout` decorator import, its usage, and the import |
| 35 | + // for whatever template was used in the decorator invocation. |
| 36 | + let layoutDecoratorSpecifier = findLayoutDecorator(root); |
| 37 | + if (layoutDecoratorSpecifier) { |
| 38 | + let decoratorUsages = findLayoutDecoratorUsage(root, layoutDecoratorSpecifier); |
| 39 | + decoratorUsages.forEach((decorator) => { |
| 40 | + let layoutName = decorator.node.expression.arguments[0].name; |
| 41 | + let importDeclaration = findMatchingImport(root, layoutName); |
| 42 | + if (importDeclaration) { |
| 43 | + importDeclaration.parent.prune(); |
| 44 | + } |
| 45 | + decorator.prune(); |
| 46 | + }); |
| 47 | + |
| 48 | + if (layoutDecoratorSpecifier.parentPath.node.specifiers.length > 1) { |
| 49 | + layoutDecoratorSpecifier.prune(); |
| 50 | + } else { |
| 51 | + layoutDecoratorSpecifier.parentPath.parentPath.prune(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return root.toSource(); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + ID `layout` properties on classic objects like this: |
| 60 | + |
| 61 | + ```js |
| 62 | + export default Component.extend({ |
| 63 | + layout |
| 64 | + }); |
| 65 | + ``` |
| 66 | + */ |
| 67 | +function findClassicLayout(root) { |
| 68 | + let layoutProperty; |
| 69 | + |
| 70 | + root |
| 71 | + .find(j.CallExpression, { |
| 72 | + callee: { |
| 73 | + property: { |
| 74 | + name: "extend", |
| 75 | + }, |
| 76 | + }, |
| 77 | + }) |
| 78 | + .forEach((path) => { |
| 79 | + path.get("arguments").filter((argumentPath) => { |
| 80 | + if (argumentPath.node.type === "ObjectExpression") { |
| 81 | + let properties = argumentPath.get("properties"); |
| 82 | + let matches = properties.filter((p) => p.node.key.name === "layout"); |
| 83 | + |
| 84 | + layoutProperty = matches[0]; |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + return layoutProperty; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + ID `layout` properties on modern classes like this: |
| 94 | + |
| 95 | + ```js |
| 96 | + export default class MyThing extends Component { |
| 97 | + layout |
| 98 | + } |
| 99 | + ``` |
| 100 | + */ |
| 101 | +function findClassPropertyLayout(root) { |
| 102 | + return root.find(j.ClassDeclaration).map((path) => { |
| 103 | + let properties = path.get("body").get("body"); |
| 104 | + return properties.filter((p) => |
| 105 | + j.match(p, { type: "ClassProperty", key: { name: "layout" } }) |
| 106 | + ); |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + ID imports with a given name -- useful for mapping back to the import for |
| 112 | + whatever is bound to a given class property (whether classic or ES classes). |
| 113 | + */ |
| 114 | +function findMatchingImport(root, name) { |
| 115 | + let importPath; |
| 116 | + |
| 117 | + root |
| 118 | + .find(j.ImportDeclaration) |
| 119 | + .forEach((importDeclaration) => { |
| 120 | + let specifiers = importDeclaration.get("specifiers"); |
| 121 | + let matches = specifiers.filter((specifierPath) => |
| 122 | + j.match(specifierPath, { |
| 123 | + type: "ImportDefaultSpecifier", |
| 124 | + local: { name }, |
| 125 | + }) |
| 126 | + ); |
| 127 | + |
| 128 | + if (matches[0]) { |
| 129 | + importPath = matches[0]; |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + return importPath; |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + ID `layout` decorator imports, since it can be renamed after importing and if |
| 138 | + so we need the local name. For example: |
| 139 | + |
| 140 | + ```js |
| 141 | + import { layout as templateLayout } from '@ember-decorators/component'; |
| 142 | + ``` |
| 143 | + */ |
| 144 | +function findLayoutDecorator(root) { |
| 145 | + let importSpecifier; |
| 146 | + |
| 147 | + root |
| 148 | + .find(j.ImportDeclaration, { |
| 149 | + source: { value: "@ember-decorators/component" }, |
| 150 | + }) |
| 151 | + .forEach((importPath) => { |
| 152 | + let specifiers = importPath.get("specifiers"); |
| 153 | + let matches = specifiers.filter((specifierPath) => |
| 154 | + j.match(specifierPath, { imported: { name: "layout" } }) |
| 155 | + ); |
| 156 | + |
| 157 | + importSpecifier = matches[0]; |
| 158 | + }); |
| 159 | + |
| 160 | + return importSpecifier; |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + ID `layout` decorator imports, since it can be renamed after importing and ifn |
| 165 | + so we need the local name |
| 166 | + |
| 167 | + ```js |
| 168 | + @layout(someTemplateValue) |
| 169 | + export default class MyThing extends Component { |
| 170 | + // ... |
| 171 | + } |
| 172 | + ``` |
| 173 | + */ |
| 174 | +function findLayoutDecoratorUsage(root, layoutDecorator) { |
| 175 | + return root.find(j.ClassDeclaration).map((classPath) => { |
| 176 | + if (classPath.node.decorators === undefined) { |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + let decorators = classPath.get("decorators"); |
| 181 | + let matches = decorators.filter((decoratorPath) => |
| 182 | + j.match(decoratorPath, { |
| 183 | + expression: { |
| 184 | + callee: { name: layoutDecorator.node.local.name }, |
| 185 | + }, |
| 186 | + }) |
| 187 | + ); |
| 188 | + |
| 189 | + return matches; |
| 190 | + }); |
| 191 | +} |
| 192 | + |
| 193 | +module.exports = { transform }; |
0 commit comments