ember-codemod-add-component-signatures 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +9 -0
- package/README.md +141 -0
- package/dist/bin/ember-codemod-add-component-signatures.js +38 -0
- package/dist/src/blueprints/ember-cli/template-only-component.ts +8 -0
- package/dist/src/index.js +14 -0
- package/dist/src/steps/analyze-project/analyze-components/find-arguments.js +98 -0
- package/dist/src/steps/analyze-project/analyze-components/find-blocks.js +27 -0
- package/dist/src/steps/analyze-project/analyze-components/find-element.js +21 -0
- package/dist/src/steps/analyze-project/analyze-components/index.js +3 -0
- package/dist/src/steps/analyze-project/analyze-components.js +66 -0
- package/dist/src/steps/analyze-project/filter-components.js +29 -0
- package/dist/src/steps/analyze-project/find-components.js +25 -0
- package/dist/src/steps/analyze-project/index.js +3 -0
- package/dist/src/steps/analyze-project.js +10 -0
- package/dist/src/steps/convert-to-typescript.js +22 -0
- package/dist/src/steps/create-options.js +22 -0
- package/dist/src/steps/create-registries/create-registry.js +17 -0
- package/dist/src/steps/create-registries/has-registry.js +18 -0
- package/dist/src/steps/create-registries/index.js +3 -0
- package/dist/src/steps/create-registries/rename-component/index.js +2 -0
- package/dist/src/steps/create-registries/rename-component/pass-component-name-to-base-component.js +56 -0
- package/dist/src/steps/create-registries/rename-component/update-references.js +34 -0
- package/dist/src/steps/create-registries/rename-component.js +17 -0
- package/dist/src/steps/create-registries.js +49 -0
- package/dist/src/steps/create-signatures/create-signature/builders.js +18 -0
- package/dist/src/steps/create-signatures/create-signature/index.js +3 -0
- package/dist/src/steps/create-signatures/create-signature/is-signature.js +7 -0
- package/dist/src/steps/create-signatures/create-signature/pass-signature-to-base-component.js +204 -0
- package/dist/src/steps/create-signatures/create-signature/update-constructor.js +19 -0
- package/dist/src/steps/create-signatures/create-signature/update-reference.js +42 -0
- package/dist/src/steps/create-signatures/create-signature.js +23 -0
- package/dist/src/steps/create-signatures/index.js +1 -0
- package/dist/src/steps/create-signatures.js +40 -0
- package/dist/src/steps/create-template-only-components.js +27 -0
- package/dist/src/steps/index.js +7 -0
- package/dist/src/steps/update-signatures/builders.js +60 -0
- package/dist/src/steps/update-signatures/index.js +1 -0
- package/dist/src/steps/update-signatures/update-signature.js +37 -0
- package/dist/src/steps/update-signatures.js +42 -0
- package/dist/src/types/index.js +1 -0
- package/dist/src/utils/blueprints/blueprints-root.js +4 -0
- package/dist/src/utils/blueprints.js +1 -0
- package/dist/src/utils/components/get-base-component.js +41 -0
- package/dist/src/utils/components/get-block-parameter.type.js +14 -0
- package/dist/src/utils/components/get-class-path.js +15 -0
- package/dist/src/utils/components/get-extension-map.js +15 -0
- package/dist/src/utils/components/get-html-interface.js +64 -0
- package/dist/src/utils/components/get-template-path.js +11 -0
- package/dist/src/utils/components/normalize-block-name.js +14 -0
- package/dist/src/utils/components.js +7 -0
- package/package.json +70 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Isaac J. Lee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
[](https://github.com/ijlee2/ember-codemod-add-component-signatures/actions/workflows/ci.yml)
|
|
2
|
+
|
|
3
|
+
# ember-codemod-add-component-signatures
|
|
4
|
+
|
|
5
|
+
_Codemod to add component signatures_
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## What is it?
|
|
9
|
+
|
|
10
|
+
To introduce Glint, you will need to write the [signature](https://typed-ember.gitbook.io/glint/environments/ember/component-signatures) and [template registry](https://typed-ember.gitbook.io/glint/environments/ember/template-registry) for each component. This can be an error-prone, onerous task for large projects.
|
|
11
|
+
|
|
12
|
+
You can run this codemod to get started.
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- Scaffolds signature for components
|
|
18
|
+
- Adds template registry for components
|
|
19
|
+
- Supports `<template>` tag components
|
|
20
|
+
|
|
21
|
+
<div align="center">
|
|
22
|
+
<figure>
|
|
23
|
+
<img alt="A code diff to show what template-only components can look like before and after running ember-codemod-add-component-signatures" src="https://github.com/ijlee2/ember-codemod-add-component-signatures/assets/16869656/55115b27-682e-4f5a-9a03-36d7aa1cc0c2" width="640">
|
|
24
|
+
<br>
|
|
25
|
+
<figcaption>Template-only components</figcaption>
|
|
26
|
+
</figure>
|
|
27
|
+
<br><br>
|
|
28
|
+
<figure>
|
|
29
|
+
<img alt="A code diff to show what Glimmer components can look like before and after running ember-codemod-add-component-signatures" src="https://github.com/ijlee2/ember-codemod-add-component-signatures/assets/16869656/4feb1fcd-5a31-419e-9feb-2af99073ae75" width="640">
|
|
30
|
+
<img alt="Another code diff for a Glimmer component" src="https://github.com/ijlee2/ember-codemod-add-component-signatures/assets/16869656/c8c1c9b0-28a1-4957-9c49-5cc853499a4f" width="640">
|
|
31
|
+
<br>
|
|
32
|
+
<figcaption>Glimmer components</figcaption>
|
|
33
|
+
</figure>
|
|
34
|
+
</div>
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
Step 1. Quickly migrate.
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
cd <path/to/your/project>
|
|
43
|
+
npx ember-codemod-add-component-signatures <arguments>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Step 2. Review the package.
|
|
47
|
+
|
|
48
|
+
- [x] Fill in missing type information.
|
|
49
|
+
- [x] Confirm that you can run all scripts in `package.json`.
|
|
50
|
+
|
|
51
|
+
For more information, please check the [FAQ](#frequently-asked-questions).
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
### Prerequisites
|
|
55
|
+
|
|
56
|
+
Must:
|
|
57
|
+
|
|
58
|
+
- Migrate to the Octane layout (flat or nested). You can leverage the codemods for [classic](https://github.com/ember-codemods/ember-component-template-colocation-migrator) and [pod](https://github.com/ijlee2/ember-codemod-pod-to-octane) layouts.
|
|
59
|
+
|
|
60
|
+
Nice to do:
|
|
61
|
+
|
|
62
|
+
- Refactor code (e.g. Glimmerize components, meet the linting rule `no-implicit-this`) to help the codemod parse code.
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
### Arguments
|
|
66
|
+
|
|
67
|
+
You must pass `--type` to indicate what type of project you have.
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
npx ember-codemod-add-component-signatures --type app
|
|
71
|
+
npx ember-codemod-add-component-signatures --type v1-addon
|
|
72
|
+
npx ember-codemod-add-component-signatures --type v2-addon
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
<details>
|
|
76
|
+
|
|
77
|
+
<summary>Optional: Specify the component structure</summary>
|
|
78
|
+
|
|
79
|
+
By default, an Octane project has the flat component structure. Pass `--component-structure` to indicate otherwise.
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
npx ember-codemod-add-component-signatures --component-structure nested
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
</details>
|
|
86
|
+
|
|
87
|
+
<details>
|
|
88
|
+
|
|
89
|
+
<summary>Optional: Convert <code>*.{js,gjs}</code> files</summary>
|
|
90
|
+
|
|
91
|
+
By default, the codemod ignores component classes written in `*.{js,gjs}`. Pass `--convert-javascript` to allow the codemod to change the file extension to `*.{ts,gts}` and add the component signature.
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
npx ember-codemod-add-component-signatures --convert-javascript
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
</details>
|
|
98
|
+
|
|
99
|
+
<details>
|
|
100
|
+
|
|
101
|
+
<summary>Optional: Specify the project root</summary>
|
|
102
|
+
|
|
103
|
+
Pass `--root` to run the codemod on a project somewhere else (i.e. not in the current directory).
|
|
104
|
+
|
|
105
|
+
```sh
|
|
106
|
+
npx ember-codemod-add-component-signatures --root <path/to/your/project>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
</details>
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
### Limitations
|
|
113
|
+
|
|
114
|
+
The codemod is designed to cover typical cases. It is not designed to cover one-off cases. (Classic components are not supported.)
|
|
115
|
+
|
|
116
|
+
To better meet your needs, consider cloning the repo and running the codemod locally.
|
|
117
|
+
|
|
118
|
+
```sh
|
|
119
|
+
cd <path/to/cloned/repo>
|
|
120
|
+
|
|
121
|
+
# Compile TypeScript
|
|
122
|
+
pnpm build
|
|
123
|
+
|
|
124
|
+
# Run codemod
|
|
125
|
+
./dist/bin/ember-codemod-add-component-signatures.js --root <path/to/your/project>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
## Compatibility
|
|
130
|
+
|
|
131
|
+
- Node.js v20 or above
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
## Contributing
|
|
135
|
+
|
|
136
|
+
See the [Contributing](CONTRIBUTING.md) guide for details.
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
This project is licensed under the [MIT License](LICENSE.md).
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
import yargs from 'yargs';
|
|
4
|
+
import { hideBin } from 'yargs/helpers';
|
|
5
|
+
import { runCodemod } from '../src/index.js';
|
|
6
|
+
// Provide a title to the process in `ps`
|
|
7
|
+
process.title = 'ember-codemod-add-component-signatures';
|
|
8
|
+
// Set codemod options
|
|
9
|
+
const argv = yargs(hideBin(process.argv))
|
|
10
|
+
.option('component-structure', {
|
|
11
|
+
choices: ['flat', 'nested'],
|
|
12
|
+
default: 'flat',
|
|
13
|
+
describe: 'Component structure (how your components are colocated)',
|
|
14
|
+
type: 'string',
|
|
15
|
+
})
|
|
16
|
+
.option('convert-javascript', {
|
|
17
|
+
default: false,
|
|
18
|
+
describe: 'Convert *.{js,gjs} files?',
|
|
19
|
+
type: 'boolean',
|
|
20
|
+
})
|
|
21
|
+
.option('root', {
|
|
22
|
+
describe: 'Location of your Ember project',
|
|
23
|
+
type: 'string',
|
|
24
|
+
})
|
|
25
|
+
.option('type', {
|
|
26
|
+
choices: ['app', 'v1-addon', 'v2-addon'],
|
|
27
|
+
demandOption: true,
|
|
28
|
+
describe: 'Type of your Ember project',
|
|
29
|
+
type: 'string',
|
|
30
|
+
})
|
|
31
|
+
.parseSync();
|
|
32
|
+
const codemodOptions = {
|
|
33
|
+
componentStructure: argv['component-structure'],
|
|
34
|
+
convertJavaScript: argv['convert-javascript'],
|
|
35
|
+
projectRoot: argv['root'] ?? process.cwd(),
|
|
36
|
+
projectType: argv['type'],
|
|
37
|
+
};
|
|
38
|
+
runCodemod(codemodOptions);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import templateOnlyComponent from '@ember/component/template-only';
|
|
2
|
+
|
|
3
|
+
interface <%= entity.pascalizedName %>Signature {}
|
|
4
|
+
|
|
5
|
+
const <%= entity.pascalizedName %> =
|
|
6
|
+
templateOnlyComponent<<%= entity.pascalizedName %>Signature>();
|
|
7
|
+
|
|
8
|
+
export default <%= entity.pascalizedName %>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { analyzeProject, convertToTypeScript, createOptions, createRegistries, createSignatures, createTemplateOnlyComponents, updateSignatures, } from './steps/index.js';
|
|
2
|
+
export function runCodemod(codemodOptions) {
|
|
3
|
+
const options = createOptions(codemodOptions);
|
|
4
|
+
// Prepare for migration
|
|
5
|
+
convertToTypeScript(options);
|
|
6
|
+
const context = analyzeProject(options);
|
|
7
|
+
// Update components without backing class
|
|
8
|
+
createTemplateOnlyComponents(context, options);
|
|
9
|
+
// Update components with backing class
|
|
10
|
+
createSignatures(context, options);
|
|
11
|
+
createRegistries(context, options);
|
|
12
|
+
// Fill out signatures
|
|
13
|
+
updateSignatures(context, options);
|
|
14
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { AST as ASTJavaScript } from '@codemod-utils/ast-javascript';
|
|
2
|
+
import { AST as ASTTemplate } from '@codemod-utils/ast-template';
|
|
3
|
+
function analyzeClass(file) {
|
|
4
|
+
const args = new Set();
|
|
5
|
+
if (file === undefined) {
|
|
6
|
+
return args;
|
|
7
|
+
}
|
|
8
|
+
// We know that the file is in TypeScript
|
|
9
|
+
const traverse = ASTJavaScript.traverse(true);
|
|
10
|
+
traverse(file, {
|
|
11
|
+
visitMemberExpression(node) {
|
|
12
|
+
if (node.value.object.type !== 'MemberExpression' ||
|
|
13
|
+
node.value.object.property.name !== 'args') {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
switch (node.value.property.type) {
|
|
17
|
+
// Matches the pattern `this.args.someArgument`
|
|
18
|
+
case 'Identifier': {
|
|
19
|
+
args.add(node.value.property.name);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
// Matches the pattern `this.args['someArgument']`
|
|
23
|
+
case 'StringLiteral': {
|
|
24
|
+
args.add(node.value.property.value);
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
},
|
|
30
|
+
visitVariableDeclarator(node) {
|
|
31
|
+
const { id: leftHandSide, init: rightHandSide } = node.value;
|
|
32
|
+
let isValid = false;
|
|
33
|
+
switch (rightHandSide?.type) {
|
|
34
|
+
// Matches the pattern `const { someArgument } = this.args;`
|
|
35
|
+
case 'MemberExpression': {
|
|
36
|
+
if (rightHandSide.object.type !== 'ThisExpression' ||
|
|
37
|
+
rightHandSide.property.type !== 'Identifier' ||
|
|
38
|
+
rightHandSide.property.name !== 'args') {
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
isValid = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
// Matches the pattern `const { someArgument } = this.args as SomeType;`
|
|
45
|
+
case 'TSAsExpression': {
|
|
46
|
+
if (rightHandSide.expression.type !== 'MemberExpression' ||
|
|
47
|
+
rightHandSide.expression.object.type !== 'ThisExpression' ||
|
|
48
|
+
rightHandSide.expression.property.type !== 'Identifier' ||
|
|
49
|
+
rightHandSide.expression.property.name !== 'args') {
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
isValid = true;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (!isValid) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
// @ts-expect-error: Assume that types from external packages are correct
|
|
60
|
+
leftHandSide.properties.forEach((property) => {
|
|
61
|
+
switch (property.key.type) {
|
|
62
|
+
case 'Identifier': {
|
|
63
|
+
args.add(property.key.name);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
case 'StringLiteral': {
|
|
67
|
+
args.add(property.key.value);
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return false;
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
return args;
|
|
76
|
+
}
|
|
77
|
+
function analyzeTemplate(file) {
|
|
78
|
+
const traverse = ASTTemplate.traverse();
|
|
79
|
+
const args = new Set();
|
|
80
|
+
traverse(file, {
|
|
81
|
+
PathExpression(node) {
|
|
82
|
+
if (node.head.type !== 'AtHead') {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const value = node.original.replace(/^@/, '');
|
|
86
|
+
const arg = value.split('.')[0];
|
|
87
|
+
args.add(arg);
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
return args;
|
|
91
|
+
}
|
|
92
|
+
export function findArguments(templateFile, classFile) {
|
|
93
|
+
const args = new Set([
|
|
94
|
+
...analyzeTemplate(templateFile),
|
|
95
|
+
...analyzeClass(classFile),
|
|
96
|
+
]);
|
|
97
|
+
return Array.from(args).sort();
|
|
98
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AST } from '@codemod-utils/ast-template';
|
|
2
|
+
import { getBlockParameterType, normalizeBlockName, } from '../../../utils/components.js';
|
|
3
|
+
export function findBlocks(templateFile) {
|
|
4
|
+
const traverse = AST.traverse();
|
|
5
|
+
const blocksMap = new Map();
|
|
6
|
+
traverse(templateFile, {
|
|
7
|
+
MustacheStatement(node) {
|
|
8
|
+
if (node.path.type !== 'PathExpression' ||
|
|
9
|
+
node.path.original !== 'yield') {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const toArgument = node.hash.pairs.find(({ key }) => {
|
|
13
|
+
return key === 'to';
|
|
14
|
+
});
|
|
15
|
+
// @ts-expect-error: Assume that types from external packages are correct
|
|
16
|
+
const blockName = normalizeBlockName(toArgument?.value.original);
|
|
17
|
+
const positionalArgumentTypes = node.params.map(({ type: recastType }) => {
|
|
18
|
+
return getBlockParameterType(recastType);
|
|
19
|
+
});
|
|
20
|
+
blocksMap.set(blockName, positionalArgumentTypes);
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
if (blocksMap.size === 0) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
return new Map(Array.from(blocksMap).sort());
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AST } from '@codemod-utils/ast-template';
|
|
2
|
+
import { getHtmlInterface } from '../../../utils/components.js';
|
|
3
|
+
export function findElement(templateFile) {
|
|
4
|
+
const traverse = AST.traverse();
|
|
5
|
+
const htmlInterfaces = new Set();
|
|
6
|
+
traverse(templateFile, {
|
|
7
|
+
ElementNode(node) {
|
|
8
|
+
const hasSplattributes = node.attributes.find(({ name }) => {
|
|
9
|
+
return name === '...attributes';
|
|
10
|
+
});
|
|
11
|
+
if (!hasSplattributes) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
htmlInterfaces.add(getHtmlInterface(node.tag));
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
if (htmlInterfaces.size === 0) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
return Array.from(htmlInterfaces).sort();
|
|
21
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { findTemplateTags, toEcma } from '@codemod-utils/ast-template-tag';
|
|
4
|
+
import { getClassPath, getTemplatePath } from '../../utils/components.js';
|
|
5
|
+
import { findArguments, findBlocks, findElement, } from './analyze-components/index.js';
|
|
6
|
+
function getFiles(componentName, extensions, options) {
|
|
7
|
+
const { projectRoot } = options;
|
|
8
|
+
if (extensions.has('.gts')) {
|
|
9
|
+
const gtsFilePath = getClassPath(componentName, extensions, options);
|
|
10
|
+
const gtsFile = readFileSync(join(projectRoot, gtsFilePath), 'utf8');
|
|
11
|
+
const ecmaFile = toEcma(gtsFile);
|
|
12
|
+
const templateTags = findTemplateTags(gtsFile);
|
|
13
|
+
const templateFile = templateTags.reduce((accumulator, templateTag) => {
|
|
14
|
+
accumulator += templateTag.contents;
|
|
15
|
+
return accumulator;
|
|
16
|
+
}, '');
|
|
17
|
+
return {
|
|
18
|
+
classFile: ecmaFile,
|
|
19
|
+
templateFile,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
let classFile;
|
|
23
|
+
if (extensions.has('.ts')) {
|
|
24
|
+
const classFilePath = getClassPath(componentName, extensions, options);
|
|
25
|
+
classFile = readFileSync(join(projectRoot, classFilePath), 'utf8');
|
|
26
|
+
}
|
|
27
|
+
const templateFilePath = getTemplatePath(componentName, extensions, options);
|
|
28
|
+
const templateFile = readFileSync(join(projectRoot, templateFilePath), 'utf8');
|
|
29
|
+
return {
|
|
30
|
+
classFile,
|
|
31
|
+
templateFile,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function analyzeComponents(extensionMap, options) {
|
|
35
|
+
const signatureMap = new Map();
|
|
36
|
+
for (const [componentName, extensions] of extensionMap) {
|
|
37
|
+
const hasTemplate = extensions.has('.hbs') || extensions.has('.gts');
|
|
38
|
+
if (!hasTemplate) {
|
|
39
|
+
signatureMap.set(componentName, {
|
|
40
|
+
Args: undefined,
|
|
41
|
+
Blocks: undefined,
|
|
42
|
+
Element: undefined,
|
|
43
|
+
});
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const { classFile, templateFile } = getFiles(componentName, extensions, options);
|
|
47
|
+
try {
|
|
48
|
+
const Args = findArguments(templateFile, classFile);
|
|
49
|
+
const Blocks = findBlocks(templateFile);
|
|
50
|
+
const Element = findElement(templateFile);
|
|
51
|
+
signatureMap.set(componentName, {
|
|
52
|
+
Args,
|
|
53
|
+
Blocks,
|
|
54
|
+
Element,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
let message = `WARNING: analyzeComponents could not parse \`${componentName}\`. Please update the file manually.`;
|
|
59
|
+
if (error instanceof Error) {
|
|
60
|
+
message += ` (${error.message})`;
|
|
61
|
+
}
|
|
62
|
+
console.warn(`${message}\n`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return signatureMap;
|
|
66
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { toEcma } from '@codemod-utils/ast-template-tag';
|
|
4
|
+
import { getBaseComponent, getClassPath } from '../../utils/components.js';
|
|
5
|
+
function isSupported(file) {
|
|
6
|
+
const { importPath } = getBaseComponent(file);
|
|
7
|
+
const isComponent = importPath !== undefined;
|
|
8
|
+
const isClassicComponent = importPath === '@ember/component';
|
|
9
|
+
return isComponent && !isClassicComponent;
|
|
10
|
+
}
|
|
11
|
+
export function filterComponents(extensionMap, options) {
|
|
12
|
+
const { projectRoot } = options;
|
|
13
|
+
const filteredEntries = Array.from(extensionMap.entries()).filter(([componentName, extensions]) => {
|
|
14
|
+
const hasClassJavaScript = extensions.has('.gjs') || extensions.has('.js');
|
|
15
|
+
if (hasClassJavaScript) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const hasClassTypeScript = extensions.has('.gts') || extensions.has('.ts');
|
|
19
|
+
// hbs file only
|
|
20
|
+
if (!hasClassTypeScript) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
const filePath = getClassPath(componentName, extensions, options);
|
|
24
|
+
const file = readFileSync(join(projectRoot, filePath), 'utf8');
|
|
25
|
+
const ecmaFile = toEcma(file);
|
|
26
|
+
return isSupported(ecmaFile);
|
|
27
|
+
});
|
|
28
|
+
return new Map(filteredEntries);
|
|
29
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { findFiles, renamePathByDirectory } from '@codemod-utils/files';
|
|
2
|
+
import { getExtensionMap } from '../../utils/components.js';
|
|
3
|
+
function normalizeComponentNames(extensionMap) {
|
|
4
|
+
return new Map(Array.from(extensionMap.entries()).map(([oldName, extensions]) => {
|
|
5
|
+
const newName = oldName.replace(/\/index$/, '');
|
|
6
|
+
return [newName, extensions];
|
|
7
|
+
}));
|
|
8
|
+
}
|
|
9
|
+
export function findComponents(options) {
|
|
10
|
+
const { componentStructure, projectRoot, src } = options;
|
|
11
|
+
const filePaths = findFiles(`${src}/**/*.{gjs,gts,hbs,js,ts}`, {
|
|
12
|
+
ignoreList: ['**/*.d.ts'],
|
|
13
|
+
projectRoot,
|
|
14
|
+
}).map((filePath) => {
|
|
15
|
+
return renamePathByDirectory(filePath, {
|
|
16
|
+
from: src,
|
|
17
|
+
to: '',
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
const extensionMap = getExtensionMap(filePaths);
|
|
21
|
+
if (componentStructure === 'nested') {
|
|
22
|
+
return normalizeComponentNames(extensionMap);
|
|
23
|
+
}
|
|
24
|
+
return extensionMap;
|
|
25
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { analyzeComponents, filterComponents, findComponents, } from './analyze-project/index.js';
|
|
2
|
+
export function analyzeProject(options) {
|
|
3
|
+
const unfilteredExtensionMap = findComponents(options);
|
|
4
|
+
const extensionMap = filterComponents(unfilteredExtensionMap, options);
|
|
5
|
+
const signatureMap = analyzeComponents(extensionMap, options);
|
|
6
|
+
return {
|
|
7
|
+
extensionMap,
|
|
8
|
+
signatureMap,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { findFiles, moveFiles } from '@codemod-utils/files';
|
|
2
|
+
export function convertToTypeScript(options) {
|
|
3
|
+
const { convertJavaScript, projectRoot, src } = options;
|
|
4
|
+
if (!convertJavaScript) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const filePaths = findFiles(`${src}/**/*.{gjs,js}`, {
|
|
8
|
+
projectRoot,
|
|
9
|
+
});
|
|
10
|
+
const filePathMap = filePaths.reduce((accumulator, filePath) => {
|
|
11
|
+
if (filePath.endsWith('.gjs')) {
|
|
12
|
+
accumulator.set(filePath, filePath.replace(/\.gjs$/, '.gts'));
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
accumulator.set(filePath, filePath.replace(/\.js$/, '.ts'));
|
|
16
|
+
}
|
|
17
|
+
return accumulator;
|
|
18
|
+
}, new Map());
|
|
19
|
+
moveFiles(filePathMap, {
|
|
20
|
+
projectRoot,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
function getSrc(projectType) {
|
|
2
|
+
switch (projectType) {
|
|
3
|
+
case 'app': {
|
|
4
|
+
return 'app/components';
|
|
5
|
+
}
|
|
6
|
+
case 'v1-addon': {
|
|
7
|
+
return 'addon/components';
|
|
8
|
+
}
|
|
9
|
+
case 'v2-addon': {
|
|
10
|
+
return 'src/components';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function createOptions(codemodOptions) {
|
|
15
|
+
const { componentStructure, convertJavaScript, projectRoot, projectType } = codemodOptions;
|
|
16
|
+
return {
|
|
17
|
+
componentStructure,
|
|
18
|
+
convertJavaScript,
|
|
19
|
+
projectRoot,
|
|
20
|
+
src: getSrc(projectType),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AST } from '@codemod-utils/ast-javascript';
|
|
2
|
+
export function createRegistry(file, data) {
|
|
3
|
+
const traverse = AST.traverse(true);
|
|
4
|
+
const ast = traverse(file);
|
|
5
|
+
// @ts-expect-error: Assume that types from external packages are correct
|
|
6
|
+
const nodes = ast.program.body;
|
|
7
|
+
const registryEntries = AST.builders.tsInterfaceBody([
|
|
8
|
+
AST.builders.tsPropertySignature(AST.builders.stringLiteral(data.entity.doubleColonizedName), AST.builders.tsTypeAnnotation(AST.builders.tsTypeQuery(AST.builders.identifier(data.entity.pascalizedName)))),
|
|
9
|
+
AST.builders.tsPropertySignature(AST.builders.stringLiteral(data.entity.name), AST.builders.tsTypeAnnotation(AST.builders.tsTypeQuery(AST.builders.identifier(data.entity.pascalizedName)))),
|
|
10
|
+
]);
|
|
11
|
+
const registryNode = AST.builders.tsModuleDeclaration(AST.builders.stringLiteral('@glint/environment-ember-loose/registry'), AST.builders.tsModuleBlock([
|
|
12
|
+
AST.builders.exportDefaultDeclaration(AST.builders.tsInterfaceDeclaration(AST.builders.identifier('Registry'), registryEntries)),
|
|
13
|
+
]));
|
|
14
|
+
registryNode.declare = true;
|
|
15
|
+
nodes.push(registryNode);
|
|
16
|
+
return AST.print(ast);
|
|
17
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AST } from '@codemod-utils/ast-javascript';
|
|
2
|
+
export function hasRegistry(file) {
|
|
3
|
+
const traverse = AST.traverse(true);
|
|
4
|
+
let hasRegistry = false;
|
|
5
|
+
traverse(file, {
|
|
6
|
+
visitTSModuleDeclaration(path) {
|
|
7
|
+
if (path.node.id.type !== 'StringLiteral') {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
const moduleName = path.node.id.value;
|
|
11
|
+
if (moduleName === '@glint/environment-ember-loose/registry') {
|
|
12
|
+
hasRegistry = true;
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
return hasRegistry;
|
|
18
|
+
}
|
package/dist/src/steps/create-registries/rename-component/pass-component-name-to-base-component.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AST } from '@codemod-utils/ast-javascript';
|
|
2
|
+
export function passComponentNameToBaseComponent(file, options) {
|
|
3
|
+
const traverse = AST.traverse(true);
|
|
4
|
+
const { baseComponentName, data } = options;
|
|
5
|
+
let componentName;
|
|
6
|
+
const ast = traverse(file, {
|
|
7
|
+
visitClassDeclaration(path) {
|
|
8
|
+
if (!path.node.superClass ||
|
|
9
|
+
path.node.superClass.type !== 'Identifier' ||
|
|
10
|
+
path.node.superClass.name !== baseComponentName) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (!path.node.id) {
|
|
14
|
+
path.node.id = AST.builders.identifier(data.entity.pascalizedName);
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
componentName = path.node.id.name;
|
|
18
|
+
path.node.id.name = data.entity.pascalizedName;
|
|
19
|
+
return false;
|
|
20
|
+
},
|
|
21
|
+
visitVariableDeclaration(path) {
|
|
22
|
+
const declaration = path.node.declarations[0];
|
|
23
|
+
if (declaration.type !== 'VariableDeclarator') {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
switch (declaration.init?.type) {
|
|
27
|
+
case 'CallExpression': {
|
|
28
|
+
if (declaration.init.callee.type !== 'Identifier' ||
|
|
29
|
+
declaration.init.callee.name !== baseComponentName ||
|
|
30
|
+
declaration.id.type !== 'Identifier') {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
componentName = declaration.id.name;
|
|
34
|
+
declaration.id.name = data.entity.pascalizedName;
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
case 'ClassExpression': {
|
|
38
|
+
if (!declaration.init.superClass ||
|
|
39
|
+
declaration.init.superClass.type !== 'Identifier' ||
|
|
40
|
+
declaration.init.superClass.name !== baseComponentName ||
|
|
41
|
+
declaration.id.type !== 'Identifier') {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
componentName = declaration.id.name;
|
|
45
|
+
declaration.id.name = data.entity.pascalizedName;
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
return {
|
|
53
|
+
componentName,
|
|
54
|
+
newFile: AST.print(ast),
|
|
55
|
+
};
|
|
56
|
+
}
|