open-vault/ui/scripts/codemods/inject-service.js

150 lines
4.3 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
Ember Upgrade to 4.4 (#17086) * runs ember-cli-update to 4.4.0 * updates yarn.lock * updates dependencies causing runtime errors (#17135) * Inject Store Service When Accessed Implicitly (#17345) * adds codemod for injecting store service * adds custom babylon parser with decorators-legacy plugin for jscodeshift transforms * updates inject-store-service codemod to only look for .extend object expressions and adds recast options * runs inject-store-service codemod on js files * replace query-params helper with hash (#17404) * Updates/removes dependencies throwing errors in Ember 4.4 (#17396) * updates ember-responsive to latest * updates ember-composable-helpers to latest and uses includes helper since contains was removed * updates ember-concurrency to latest * updates ember-cli-clipboard to latest * temporary workaround for toolbar-link component throwing errors for using params arg with LinkTo * adds missing store injection to auth configure route * fixes issue with string-list component throwing error for accessing prop in same computation * fixes non-iterable query params issue in mfa methods controller * refactors field-to-attrs to handle belongsTo rather than fragments * converts mount-config fragment to belongsTo on auth-method model * removes ember-api-actions and adds tune method to auth-method adapter * converts cluster replication attributes from fragment to relationship * updates ember-data, removes ember-data-fragments and updates yarn to latest * removes fragments from secret-engine model * removes fragment from test-form-model * removes commented out code * minor change to inject-store-service codemod and runs again on js files * Remove LinkTo positional params (#17421) * updates ember-cli-page-object to latest version * update toolbar-link to support link-to args and not positional params * adds replace arg to toolbar-link component * Clean up js lint errors (#17426) * replaces assert.equal to assert.strictEqual * update eslint no-console to error and disables invididual intended uses of console * cleans up hbs lint warnings (#17432) * Upgrade bug and test fixes (#17500) * updates inject-service codemod to take arg for service name and runs for flashMessages service * fixes hbs lint error after merging main * fixes flash messages * updates more deps * bug fixes * test fixes * updates ember-cli-content-security-policy and prevents default form submission throwing errors * more bug and test fixes * removes commented out code * fixes issue with code-mirror modifier sending change event on setup causing same computation error * Upgrade Clean Up (#17543) * updates deprecation workflow and filter * cleans up build errors, removes unused ivy-codemirror and sass and updates ember-cli-sass and node-sass to latest * fixes control groups test that was skipped after upgrade * updates control group service tests * addresses review feedback * updates control group service handleError method to use router.currentURL rather that transition.intent.url * adds changelog entry
2022-10-18 15:46:02 +00:00
import babylonParser from './jscodeshift-babylon-parser';
// use babylon parser with decorators-legacy plugin
export const parser = babylonParser;
// checks for access of specified service on this
// injects service if not present and imports inject as service if needed
// example usage - npx jscodeshift -t ./scripts/codemods/inject-service.js ./app/**/*.js --service=store
// --service arg is required with name of service
// pass -d for dry run (no files transformed)
export default function transformer({ source }, api, { service }) {
if (!service) {
throw new Error('Missing service arg. Pass --service=store for example to script');
}
const j = api.jscodeshift;
const filterForService = (path) => {
return j(path.value).find(j.MemberExpression, {
object: {
type: 'ThisExpression',
},
property: {
name: service,
},
}).length;
};
const recastOptions = {
reuseWhitespace: false,
wrapColumn: 110,
quote: 'single',
trailingComma: true,
};
let didInjectService = false;
// find class bodies and filter down to ones that access service
const classesAccessingService = j(source).find(j.ClassBody).filter(filterForService);
if (classesAccessingService.length) {
// filter down to class bodies where service is not injected
const missingService = classesAccessingService.filter((path) => {
return !j(path.value)
.find(j.ClassProperty, {
key: {
name: service,
},
})
.filter((path) => {
// ensure service property belongs to service decorator
return path.value.decorators.find((path) => path.expression.name === 'service');
}).length;
});
if (missingService.length) {
// inject service
const serviceInjection = j.classProperty(j.identifier(`@service ${service}`), null);
// adding a decorator this way will force injection down to a new line and then add a new line
// leaving in just in case it's needed
// serviceInjection.decorators = [j.decorator(j.identifier('service'))];
source = missingService
.forEach((path) => {
path.value.body.unshift(serviceInjection);
})
.toSource();
didInjectService = true;
}
}
// find .extend object expressions and filter down to ones that access this[service]
const objectsAccessingService = j(source)
.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
property: {
name: 'extend',
},
},
})
.filter(filterForService)
.find(j.ObjectExpression)
.filter((path) => {
// filter object expressions that belong to .extend
// otherwise service will also be injected in actions: { } block of component for example
const callee = path.parent.value.callee;
return callee && callee.property?.name === 'extend';
});
if (objectsAccessingService.length) {
// filter down to objects where service is not injected
const missingService = objectsAccessingService.filter((path) => {
return !j(path.value).find(j.ObjectProperty, {
key: {
name: service,
},
value: {
callee: {
name: 'service',
},
},
}).length;
});
if (missingService.length) {
// inject service
const serviceInjection = j.objectProperty(
j.identifier(service),
j.callExpression(j.identifier('service'), [])
);
source = missingService
.forEach((path) => {
path.value.properties.unshift(serviceInjection);
})
.toSource(recastOptions);
didInjectService = true;
}
}
// if service was injected here check if inject has been imported
if (didInjectService) {
const needsImport = !j(source).find(j.ImportSpecifier, {
imported: {
name: 'inject',
},
}).length;
if (needsImport) {
const injectionImport = j.importDeclaration(
[j.importSpecifier(j.identifier('inject'), j.identifier('service'))],
j.literal('@ember/service')
);
const imports = j(source).find(j.ImportDeclaration);
source = imports
.at(imports.length - 1)
.insertAfter(injectionImport)
.toSource(recastOptions);
}
}
return source;
}