ember-codemod-remove-inject-as-service 1.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 ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 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,110 @@
1
+ [![This project uses GitHub Actions for continuous integration.](https://github.com/ijlee2/ember-codemod-remove-inject-as-service/actions/workflows/ci.yml/badge.svg)](https://github.com/ijlee2/ember-codemod-remove-inject-as-service/actions/workflows/ci.yml)
2
+
3
+ # ember-codemod-remove-inject-as-service
4
+
5
+ _Codemod to remove `inject as service`_<sup>1</sup>
6
+
7
+ 1. [Features](#features)
8
+ 1. [Usage](#usage)
9
+ - [Arguments](#arguments)
10
+ - [Limitations](#limitations)
11
+ 1. [Compatibility](#compatibility)
12
+ 1. [Contributing](#contributing)
13
+ 1. [License](#license)
14
+
15
+ <sup>1. `@ember/service` has provided `service` since [Ember 4.1](https://blog.emberjs.com/ember-4-1-released/). `inject`, which became an alias to `service`, is [planned to be deprecated in v6](https://rfcs.emberjs.com/id/1001-deprecate-named-inject).</sup>
16
+
17
+
18
+ ## Features
19
+
20
+ The codemod helps you standardize how you inject services:
21
+
22
+ - Replace `inject` with `service`<sup>1</sup>
23
+ - Replace `!` with `declare` in TS files<sup>2</sup>
24
+ - Remove `private` and `readonly` keywords in TS files<sup>2</sup>
25
+
26
+ It preserves your code whenever possible.
27
+
28
+ <sup>1. Replaces `inject as ...` and `service as ...`, too.</sup>
29
+
30
+ <sup>2. Matches [the style shown in the Ember Guides](https://guides.emberjs.com/v5.8.0/typescript/core-concepts/services/#toc_using-services).</sup>
31
+
32
+
33
+ ## Usage
34
+
35
+ Step 1. Quickly migrate.
36
+
37
+ ```sh
38
+ cd <path/to/your/project>
39
+ npx ember-codemod-remove-inject-as-service <arguments>
40
+ ```
41
+
42
+ Step 2. Fix formatting issues.
43
+
44
+ - Optional: Do a find-and-replace-all, if you want to place `@service()` and `declare` on the same line when possible.
45
+
46
+ - Find: `@service\((.+)\)(\n|\s)+declare`
47
+ - Replace: `@service($1) declare`
48
+
49
+ - Optional: Do a find-and-replace-all, if you want to place `@service` and `declare` on the same line when possible.
50
+
51
+ - Find: `@service(\n|\s)+declare`
52
+ - Replace: `@service declare`
53
+
54
+ - Run `lint:js:fix` (i.e. autofix from `eslint` and `prettier`).
55
+
56
+
57
+
58
+ ### Arguments
59
+
60
+ You must pass `--type` to indicate what type of project you have.
61
+
62
+ ```sh
63
+ npx ember-codemod-remove-inject-as-service --type app
64
+ npx ember-codemod-remove-inject-as-service --type v1-addon
65
+ npx ember-codemod-remove-inject-as-service --type v2-addon
66
+ ```
67
+
68
+ <details>
69
+
70
+ <summary>Optional: Specify the project root</summary>
71
+
72
+ Pass `--root` to run the codemod somewhere else (i.e. not in the current directory).
73
+
74
+ ```sh
75
+ npx ember-codemod-remove-inject-as-service --root <path/to/your/project>
76
+ ```
77
+
78
+ </details>
79
+
80
+
81
+ ### Limitations
82
+
83
+ The codemod is designed to cover typical cases. It is not designed to cover one-off cases.
84
+
85
+ To better meet your needs, consider cloning the repo and running the codemod locally.
86
+
87
+ ```sh
88
+ cd <path/to/cloned/repo>
89
+
90
+ # Compile TypeScript
91
+ pnpm build
92
+
93
+ # Run codemod
94
+ ./dist/bin/ember-codemod-remove-inject-as-service.js --root <path/to/your/project>
95
+ ```
96
+
97
+
98
+ ## Compatibility
99
+
100
+ - Node.js v18 or above
101
+
102
+
103
+ ## Contributing
104
+
105
+ See the [Contributing](CONTRIBUTING.md) guide for details.
106
+
107
+
108
+ ## License
109
+
110
+ This project is licensed under the [MIT License](LICENSE.md).
@@ -0,0 +1,25 @@
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-remove-inject-as-service';
8
+ // Set codemod options
9
+ const argv = yargs(hideBin(process.argv))
10
+ .option('root', {
11
+ describe: 'Where to run the codemod',
12
+ type: 'string',
13
+ })
14
+ .option('type', {
15
+ choices: ['app', 'v1-addon', 'v2-addon'],
16
+ demandOption: true,
17
+ describe: 'Type of your Ember project',
18
+ type: 'string',
19
+ })
20
+ .parseSync();
21
+ const codemodOptions = {
22
+ projectRoot: argv['root'] ?? process.cwd(),
23
+ projectType: argv['type'],
24
+ };
25
+ runCodemod(codemodOptions);
@@ -0,0 +1,17 @@
1
+ import { migrateEmberApp, migrateEmberV1Addon, migrateEmberV2Addon, } from './migration/index.js';
2
+ export function runCodemod(codemodOptions) {
3
+ switch (codemodOptions.projectType) {
4
+ case 'app': {
5
+ migrateEmberApp(codemodOptions);
6
+ break;
7
+ }
8
+ case 'v1-addon': {
9
+ migrateEmberV1Addon(codemodOptions);
10
+ break;
11
+ }
12
+ case 'v2-addon': {
13
+ migrateEmberV2Addon(codemodOptions);
14
+ break;
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,6 @@
1
+ import { createOptions } from '../../utils/steps/create-options.js';
2
+ import { updateProject } from '../../utils/steps/update-project.js';
3
+ export function migrateEmberApp(codemodOptions) {
4
+ const options = createOptions(codemodOptions);
5
+ updateProject(['app/**/*.{js,ts}'], options);
6
+ }
@@ -0,0 +1,6 @@
1
+ import { createOptions } from '../../utils/steps/create-options.js';
2
+ import { updateProject } from '../../utils/steps/update-project.js';
3
+ export function migrateEmberV1Addon(codemodOptions) {
4
+ const options = createOptions(codemodOptions);
5
+ updateProject(['addon/**/*.{js,ts}', 'tests/dummy/app/**/*.{js,ts}'], options);
6
+ }
@@ -0,0 +1,6 @@
1
+ import { createOptions } from '../../utils/steps/create-options.js';
2
+ import { updateProject } from '../../utils/steps/update-project.js';
3
+ export function migrateEmberV2Addon(codemodOptions) {
4
+ const options = createOptions(codemodOptions);
5
+ updateProject(['src/**/*.{js,ts}'], options);
6
+ }
@@ -0,0 +1,3 @@
1
+ export * from './ember-app/index.js';
2
+ export * from './ember-v1-addon/index.js';
3
+ export * from './ember-v2-addon/index.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ export function createOptions(codemodOptions) {
2
+ const { projectRoot, projectType } = codemodOptions;
3
+ return {
4
+ projectRoot,
5
+ projectType,
6
+ };
7
+ }
@@ -0,0 +1,109 @@
1
+ import { AST } from '@codemod-utils/ast-javascript';
2
+ function isValueImport(importKind) {
3
+ return importKind === undefined || importKind === 'value';
4
+ }
5
+ function updateImportStatement(file, data) {
6
+ let localName;
7
+ const traverse = AST.traverse(data.isTypeScript);
8
+ const ast = traverse(file, {
9
+ visitImportDeclaration(path) {
10
+ const { importKind, source, specifiers } = path.node;
11
+ if (!isValueImport(importKind)) {
12
+ return false;
13
+ }
14
+ if (source.type !== 'Literal' && source.type !== 'StringLiteral') {
15
+ return false;
16
+ }
17
+ if (source.value !== '@ember/service' || !Array.isArray(specifiers)) {
18
+ return false;
19
+ }
20
+ path.node.specifiers = specifiers.map((specifier) => {
21
+ // @ts-expect-error: 'specifier.importKind' exists
22
+ if (!isValueImport(specifier.importKind)) {
23
+ return specifier;
24
+ }
25
+ if (specifier.type !== 'ImportSpecifier') {
26
+ return specifier;
27
+ }
28
+ if (specifier.imported.name !== 'inject' &&
29
+ specifier.imported.name !== 'service') {
30
+ return specifier;
31
+ }
32
+ localName = specifier.local.name;
33
+ return AST.builders.importSpecifier(AST.builders.identifier('service'));
34
+ });
35
+ return false;
36
+ },
37
+ });
38
+ return {
39
+ localName,
40
+ newFile: AST.print(ast),
41
+ };
42
+ }
43
+ function updateServiceDecorators(file, data) {
44
+ const traverse = AST.traverse(data.isTypeScript);
45
+ const ast = traverse(file, {
46
+ visitCallExpression(node) {
47
+ this.traverse(node);
48
+ switch (node.value.callee.type) {
49
+ case 'Identifier': {
50
+ if (node.value.callee.name === data.localName) {
51
+ node.value.callee.name = 'service';
52
+ }
53
+ break;
54
+ }
55
+ }
56
+ return false;
57
+ },
58
+ visitClassProperty(node) {
59
+ if (!Array.isArray(node.value.decorators) ||
60
+ node.value.decorators.length !== 1) {
61
+ return false;
62
+ }
63
+ const decorator = node.value.decorators[0];
64
+ let isMatch = false;
65
+ switch (decorator.expression.type) {
66
+ case 'CallExpression': {
67
+ if (decorator.expression.callee.name === data.localName) {
68
+ decorator.expression.callee.name = 'service';
69
+ isMatch = true;
70
+ }
71
+ break;
72
+ }
73
+ case 'Identifier': {
74
+ if (decorator.expression.name === data.localName) {
75
+ decorator.expression.name = 'service';
76
+ isMatch = true;
77
+ }
78
+ break;
79
+ }
80
+ }
81
+ if (!isMatch || !data.isTypeScript) {
82
+ return false;
83
+ }
84
+ // Stylistic choices
85
+ if (!decorator.trailingComments) {
86
+ node.value.accessibility = null;
87
+ node.value.declare = true;
88
+ node.value.definite = null;
89
+ node.value.readonly = null;
90
+ }
91
+ return false;
92
+ },
93
+ });
94
+ return AST.print(ast);
95
+ }
96
+ export function updateClass(file, isTypeScript) {
97
+ // eslint-disable-next-line prefer-const
98
+ let { localName, newFile } = updateImportStatement(file, {
99
+ isTypeScript,
100
+ });
101
+ if (!localName) {
102
+ return file;
103
+ }
104
+ newFile = updateServiceDecorators(newFile, {
105
+ isTypeScript,
106
+ localName,
107
+ });
108
+ return newFile;
109
+ }
@@ -0,0 +1,17 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { findFiles } from '@codemod-utils/files';
4
+ import { updateClass } from './update-project/update-class.js';
5
+ export function updateProject(src, options) {
6
+ const { projectRoot } = options;
7
+ const filePaths = findFiles(src, {
8
+ projectRoot,
9
+ });
10
+ filePaths.forEach((filePath) => {
11
+ const oldPath = join(projectRoot, filePath);
12
+ const oldFile = readFileSync(oldPath, 'utf8');
13
+ const isTypeScript = filePath.endsWith('.ts');
14
+ const newFile = updateClass(oldFile, isTypeScript);
15
+ writeFileSync(oldPath, newFile, 'utf8');
16
+ });
17
+ }
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "ember-codemod-remove-inject-as-service",
3
+ "version": "1.0.0",
4
+ "description": "Codemod to remove `inject as service`",
5
+ "keywords": [
6
+ "codemod",
7
+ "ember-codemod",
8
+ "emberjs"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/ijlee2/ember-codemod-remove-inject-as-service.git"
13
+ },
14
+ "license": "MIT",
15
+ "author": "Isaac J. Lee",
16
+ "type": "module",
17
+ "main": "dist/src/index.js",
18
+ "bin": "dist/bin/ember-codemod-remove-inject-as-service.js",
19
+ "directories": {
20
+ "test": "tests"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "dependencies": {
26
+ "@codemod-utils/ast-javascript": "^1.2.5",
27
+ "@codemod-utils/files": "^2.0.1",
28
+ "yargs": "^17.7.2"
29
+ },
30
+ "devDependencies": {
31
+ "@babel/core": "^7.24.4",
32
+ "@changesets/cli": "^2.27.1",
33
+ "@changesets/get-github-info": "^0.6.0",
34
+ "@codemod-utils/tests": "^1.1.4",
35
+ "@sondr3/minitest": "^0.1.2",
36
+ "@tsconfig/node18": "^18.2.4",
37
+ "@tsconfig/strictest": "^2.0.5",
38
+ "@types/node": "^18.19.31",
39
+ "@types/yargs": "^17.0.32",
40
+ "@typescript-eslint/eslint-plugin": "^7.7.0",
41
+ "@typescript-eslint/parser": "^7.7.0",
42
+ "concurrently": "^8.2.2",
43
+ "eslint": "^8.57.0",
44
+ "eslint-config-prettier": "^9.1.0",
45
+ "eslint-import-resolver-typescript": "^3.6.1",
46
+ "eslint-plugin-import": "^2.29.1",
47
+ "eslint-plugin-n": "^17.2.1",
48
+ "eslint-plugin-prettier": "^5.1.3",
49
+ "eslint-plugin-simple-import-sort": "^12.1.0",
50
+ "eslint-plugin-typescript-sort-keys": "^3.2.0",
51
+ "prettier": "^3.2.5",
52
+ "typescript": "^5.4.5"
53
+ },
54
+ "engines": {
55
+ "node": "18.* || >= 20"
56
+ },
57
+ "scripts": {
58
+ "build": "./build.sh --production",
59
+ "lint": "concurrently \"npm:lint:*(!fix)\" --names \"lint:\"",
60
+ "lint:fix": "concurrently \"npm:lint:*:fix\" --names \"fix:\"",
61
+ "lint:js": "eslint . --cache",
62
+ "lint:js:fix": "eslint . --fix",
63
+ "lint:types": "tsc --noEmit",
64
+ "release:changelog": "changeset version",
65
+ "release:publish": "pnpm build && changeset publish",
66
+ "test": "./build.sh --test && mt dist-for-testing --quiet"
67
+ }
68
+ }