Skip to content

Commit d66c3c2

Browse files
committed
Stop passing around jscodeshift
1 parent b0fcb3b commit d66c3c2

10 files changed

Lines changed: 33 additions & 67 deletions

File tree

transforms/ember-object/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const transformer: Transform = function (
1616

1717
const root = j(source) as AST.Collection;
1818
const userOptions = getConfig();
19-
const replaced = maybeTransformEmberObjects(j, root, filePath, userOptions);
19+
const replaced = maybeTransformEmberObjects(root, filePath, userOptions);
2020

2121
if (replaced) {
2222
source = root.toSource({

transforms/helpers/eo-extend-expression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import type { EOClassDecorator, EOProp } from './eo-prop/index';
66
import makeEOProp from './eo-prop/index';
77
import logger from './log-helper';
88
import type { Options } from './options';
9-
import type { DecoratorImportSpecs } from './parse-helper';
109
import { getClassName, getExpressionToReplace } from './parse-helper';
1110
import { withComments } from './transform-helper';
11+
import type { DecoratorImportSpecs } from './util/index';
1212

1313
export default class EOExtendExpression {
1414
private className: string;
@@ -71,7 +71,7 @@ export default class EOExtendExpression {
7171
transform(): boolean {
7272
const es6ClassDeclaration = this.build();
7373
if (es6ClassDeclaration) {
74-
const expressionToReplace = getExpressionToReplace(j, this.path);
74+
const expressionToReplace = getExpressionToReplace(this.path);
7575
j(expressionToReplace).replaceWith(
7676
withComments(es6ClassDeclaration, expressionToReplace.value)
7777
);

transforms/helpers/eo-prop/private/abstract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as AST from '../../ast';
22
import type { DecoratorImportInfo } from '../../decorator-info';
33
import type { Options } from '../../options';
4-
import type { DecoratorImportSpecs } from '../../parse-helper';
54
import type { RuntimeData } from '../../runtime-data';
5+
import type { DecoratorImportSpecs } from '../../util/index';
66
import {
77
DECORATORS_REQUIRED_PROP_NAMES,
88
OFF_DECORATOR_NAME,

transforms/helpers/eo-prop/private/actions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as AST from '../../../ast';
2-
import type { DecoratorImportSpecs } from '../../../parse-helper';
2+
import type { DecoratorImportSpecs } from '../../../util/index';
33
import { LIFECYCLE_HOOKS } from '../../../util/index';
44
import AbstractEOProp from '../abstract';
55
import EOActionMethod from './method';

transforms/helpers/eo-prop/private/class-decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as AST from '../../ast';
22
import { createClassDecorator } from '../../decorator-helper';
3-
import type { DecoratorImportSpecs } from '../../parse-helper';
3+
import type { DecoratorImportSpecs } from '../../util/index';
44
import {
55
ATTRIBUTE_BINDINGS_DECORATOR_NAME,
66
CLASS_NAMES_DECORATOR_NAME,

transforms/helpers/import-helper.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { JSCodeshift } from 'jscodeshift';
1+
import { default as j } from 'jscodeshift';
22
import * as AST from '../helpers/ast';
33
import type { DecoratorImportInfoMap } from './decorator-info';
44
import { getDecoratorImportInfo } from './decorator-info';
@@ -62,13 +62,12 @@ function setSpecifierNames(
6262

6363
/** Get decorated props from `import` statements */
6464
function getExistingDecoratorImports(
65-
j: JSCodeshift,
6665
root: AST.Collection
6766
): Array<AST.Path<AST.DecoratorImportDeclaration>> {
6867
const imports: Array<AST.Path<AST.DecoratorImportDeclaration>> = [];
6968

7069
for (const path in Object.fromEntries(DECORATOR_PATHS)) {
71-
const decoratorImport = getExistingImportForPath(j, root, path);
70+
const decoratorImport = getExistingImportForPath(root, path);
7271
if (decoratorImport) {
7372
imports.push(decoratorImport);
7473
}
@@ -84,7 +83,6 @@ function getExistingDecoratorImports(
8483
* `@ember-decorators/component`
8584
*/
8685
function createNewImportDeclarations(
87-
j: JSCodeshift,
8886
root: AST.Collection,
8987
decoratorsToImport: string[],
9088
/** Already imported paths */
@@ -93,11 +91,9 @@ function createNewImportDeclarations(
9391
): void {
9492
const firstDeclaration = AST.getFirstDeclaration(root);
9593

96-
// FIXME: Why is this necessary?
9794
if (options.classicDecorator) {
9895
firstDeclaration.insertBefore(
9996
createImportDeclaration(
100-
j,
10197
[j.importDefaultSpecifier(j.identifier('classic'))],
10298
'ember-classic-decorator'
10399
)
@@ -113,15 +109,12 @@ function createNewImportDeclarations(
113109

114110
for (const path of paths) {
115111
const specifiers = createEmberDecoratorSpecifiers(
116-
j,
117112
edPathNameMap.get(path),
118113
decoratorsToImport
119114
).sort((a, b) => a.imported.name.localeCompare(b.imported.name));
120115

121116
if (specifiers.length > 0) {
122-
firstDeclaration.insertBefore(
123-
createImportDeclaration(j, specifiers, path)
124-
);
117+
firstDeclaration.insertBefore(createImportDeclaration(specifiers, path));
125118
}
126119
}
127120
}
@@ -137,15 +130,14 @@ function createNewImportDeclarations(
137130
* ```
138131
*/
139132
function getDecoratorPathSpecifiers(
140-
j: JSCodeshift,
141133
root: AST.Collection,
142134
decoratorsToImport: string[] = []
143135
): Record<string, AST.ImportSpecifier[]> {
144136
const edPathNameMap = new Map(EMBER_DECORATOR_SPECIFIERS);
145137

146138
const decoratorPathSpecifierMap: Record<string, AST.ImportSpecifier[]> = {};
147139

148-
const existingDecoratorImports = getExistingDecoratorImports(j, root);
140+
const existingDecoratorImports = getExistingDecoratorImports(root);
149141

150142
// Iterate over the existing imports
151143
// Extract and process the specifiers
@@ -166,7 +158,6 @@ function getDecoratorPathSpecifiers(
166158
// Create decorator specifiers for which no existing specifiers present in the current path
167159
// e.g. `actions` need not to be imported but `@action` need to be imported from `@ember-decorators/object`
168160
const decoratedSpecifiers = createEmberDecoratorSpecifiers(
169-
j,
170161
decoratorsForPath,
171162
decoratorsToImport
172163
);
@@ -226,7 +217,6 @@ function getDecoratorPathSpecifiers(
226217

227218
/** Get existing import statement matching the import path */
228219
function getExistingImportForPath(
229-
j: JSCodeshift,
230220
root: AST.Collection,
231221
importPath: string
232222
): AST.Path<AST.DecoratorImportDeclaration> | undefined {
@@ -244,14 +234,12 @@ function getExistingImportForPath(
244234
* 3. Insert the new imports for which no existing imports found
245235
*/
246236
export function createDecoratorImportDeclarations(
247-
j: JSCodeshift,
248237
root: AST.Collection,
249238
decoratorsToImport: string[],
250239
options: Options
251240
): void {
252241
// Iterate through existing imports, extract the already imported specifiers
253242
const decoratorPathSpecifierMap = getDecoratorPathSpecifiers(
254-
j,
255243
root,
256244
decoratorsToImport
257245
);
@@ -261,7 +249,7 @@ export function createDecoratorImportDeclarations(
261249
// Create import statement replacing the existing ones with specifiers importing from ember-decorators namespace
262250
for (const decoratorPath of decoratorPathsImported) {
263251
const specifiers = defined(decoratorPathSpecifierMap[decoratorPath]);
264-
const existingImport = getExistingImportForPath(j, root, decoratorPath);
252+
const existingImport = getExistingImportForPath(root, decoratorPath);
265253
if (existingImport) {
266254
const existingSpecifiers = existingImport.value.specifiers;
267255
if (existingSpecifiers) {
@@ -272,14 +260,13 @@ export function createDecoratorImportDeclarations(
272260
}
273261
} else {
274262
firstDeclaration.insertBefore(
275-
createImportDeclaration(j, specifiers, decoratorPath)
263+
createImportDeclaration(specifiers, decoratorPath)
276264
);
277265
}
278266
}
279267

280268
// Create new import declarations
281269
createNewImportDeclarations(
282-
j,
283270
root,
284271
decoratorsToImport,
285272
decoratorPathsImported,
@@ -316,10 +303,9 @@ export function createDecoratorImportDeclarations(
316303
* ```
317304
*/
318305
export function getDecoratorImportInfos(
319-
j: JSCodeshift,
320306
root: AST.Collection
321307
): DecoratorImportInfoMap {
322-
const existingDecoratorImports = getExistingDecoratorImports(j, root);
308+
const existingDecoratorImports = getExistingDecoratorImports(root);
323309
const decoratorImportInfo: DecoratorImportInfoMap = new Map();
324310

325311
for (const decoratorImport of existingDecoratorImports) {

transforms/helpers/parse-helper.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
import camelCase from 'camelcase';
2-
import type { JSCodeshift } from 'jscodeshift';
32
import { default as j } from 'jscodeshift';
43
import path from 'path';
54
import * as AST from './ast';
5+
import type { DecoratorImportSpecs } from './util/index';
66
import { capitalizeFirstLetter } from './util/index';
77
import { assert, defined, isRecord } from './util/types';
88

9-
export interface DecoratorImportSpecs {
10-
action: boolean;
11-
classNames: boolean;
12-
classNameBindings: boolean;
13-
attributeBindings: boolean;
14-
layout: boolean;
15-
templateLayout: boolean;
16-
off: boolean;
17-
tagName: boolean;
18-
unobserves: boolean;
19-
}
20-
219
/**
2210
* Get the map of decorators to import other than the computed props, services etc
2311
* which already have imports in the code
@@ -41,7 +29,6 @@ export function mergeDecoratorImportSpecs(
4129

4230
/** Find the `EmberObject.extend` statements */
4331
export function getEOExtendExpressionCollection(
44-
j: JSCodeshift,
4532
root: AST.Collection
4633
): AST.Collection<AST.EOExtendExpression> {
4734
return AST.findPaths(root, j.CallExpression, AST.isEOExtendExpression).filter(
@@ -51,7 +38,6 @@ export function getEOExtendExpressionCollection(
5138

5239
/** Return closest parent var declaration statement */
5340
function getClosestVariableDeclaration(
54-
j: JSCodeshift,
5541
eoExtendExpressionPath: AST.Path<AST.EOExtendExpression>
5642
): AST.Path<AST.VariableDeclaration> | null {
5743
const varDeclarations = j(eoExtendExpressionPath).closest(
@@ -66,13 +52,9 @@ function getClosestVariableDeclaration(
6652
* It returns either VariableDeclaration or the CallExpression depending on how the object is created
6753
*/
6854
export function getExpressionToReplace(
69-
j: JSCodeshift,
7055
eoExtendExpressionPath: AST.Path<AST.EOExtendExpression>
7156
): AST.Path<AST.EOExtendExpression> | AST.Path<AST.VariableDeclaration> {
72-
const varDeclaration = getClosestVariableDeclaration(
73-
j,
74-
eoExtendExpressionPath
75-
);
57+
const varDeclaration = getClosestVariableDeclaration(eoExtendExpressionPath);
7658
const parentValue = eoExtendExpressionPath.parentPath?.value;
7759
const isFollowedByCreate =
7860
isRecord(parentValue) &&
@@ -94,10 +76,7 @@ export function getClassName(
9476
filePath: string,
9577
type = ''
9678
): string {
97-
const varDeclaration = getClosestVariableDeclaration(
98-
j,
99-
eoExtendExpressionPath
100-
);
79+
const varDeclaration = getClosestVariableDeclaration(eoExtendExpressionPath);
10180
if (varDeclaration) {
10281
const firstDeclarator = defined(varDeclaration.value.declarations[0]);
10382
assert(

transforms/helpers/transform-helper.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { JSCodeshift } from 'jscodeshift';
21
import { default as j } from 'jscodeshift';
32
import * as AST from '../helpers/ast';
43
import {
@@ -22,10 +21,7 @@ export function withComments<T extends { comments?: unknown }>(
2221
}
2322

2423
/** Creates line comments from passed lines */
25-
function createLineComments(
26-
j: JSCodeshift,
27-
lines: readonly string[] = []
28-
): AST.CommentLine[] {
24+
function createLineComments(lines: readonly string[] = []): AST.CommentLine[] {
2925
return lines.map((line) => j.commentLine(line));
3026
}
3127

@@ -82,7 +78,6 @@ export function replaceActionSuperExpressions(
8278
[j.thisExpression(), ...superMethodArgs]
8379
);
8480
superMethodCall.comments = createLineComments(
85-
j,
8681
ACTION_SUPER_EXPRESSION_COMMENT
8782
);
8883
return superMethodCall;
@@ -120,7 +115,6 @@ export function replaceMethodSuperExpression(
120115

121116
/** Create import statement */
122117
export function createImportDeclaration(
123-
j: JSCodeshift,
124118
specifiers: Array<AST.ImportSpecifier | AST.ImportDefaultSpecifier>,
125119
path: string
126120
): AST.ImportDeclaration {
@@ -132,7 +126,6 @@ export function createImportDeclaration(
132126
* and creates import specifiers for the matching decorators
133127
*/
134128
export function createEmberDecoratorSpecifiers(
135-
j: JSCodeshift,
136129
pathSpecifiers: string[] = [],
137130
decoratorsToImport: string[] = []
138131
): AST.ImportSpecifier[] {

transforms/helpers/transform.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getTelemetryFor } from 'ember-codemods-telemetry-helpers';
2-
import type { JSCodeshift } from 'jscodeshift';
32
import path from 'path';
43
import type * as AST from './ast';
54
import EOExtendExpression from './eo-extend-expression';
@@ -9,17 +8,16 @@ import {
98
} from './import-helper';
109
import logger from './log-helper';
1110
import type { Options, UserOptions } from './options';
12-
import type { DecoratorImportSpecs } from './parse-helper';
1311
import {
1412
getEOExtendExpressionCollection,
1513
mergeDecoratorImportSpecs,
1614
} from './parse-helper';
1715
import { RuntimeDataSchema } from './runtime-data';
16+
import type { DecoratorImportSpecs } from './util/index';
1817
import { isFileOfType, isTestFile } from './validation-helper';
1918

2019
/** Main entry point for parsing and replacing ember objects */
2120
export default function maybeTransformEmberObjects(
22-
j: JSCodeshift,
2321
root: AST.Collection,
2422
filePath: string,
2523
userOptions: UserOptions
@@ -67,7 +65,6 @@ export default function maybeTransformEmberObjects(
6765
};
6866

6967
const { transformed, decoratorImportSpecs } = _maybeTransformEmberObjects(
70-
j,
7168
root,
7269
filePath,
7370
options
@@ -79,14 +76,13 @@ export default function maybeTransformEmberObjects(
7976
const decoratorsToImport = Object.keys(decoratorImportSpecs).filter(
8077
(key) => decoratorImportSpecs[key as keyof DecoratorImportSpecs]
8178
);
82-
createDecoratorImportDeclarations(j, root, decoratorsToImport, options);
79+
createDecoratorImportDeclarations(root, decoratorsToImport, options);
8380
logger.info(`[${filePath}]: SUCCESS`);
8481
}
8582
return transformed;
8683
}
8784

8885
function _maybeTransformEmberObjects(
89-
j: JSCodeshift,
9086
root: AST.Collection,
9187
filePath: string,
9288
options: Options
@@ -95,7 +91,7 @@ function _maybeTransformEmberObjects(
9591
decoratorImportSpecs: DecoratorImportSpecs;
9692
} {
9793
// Parse the import statements
98-
const existingDecoratorImportInfos = getExistingDecoratorImportInfos(j, root);
94+
const existingDecoratorImportInfos = getExistingDecoratorImportInfos(root);
9995
let transformed = false;
10096
let decoratorImportSpecs: DecoratorImportSpecs = {
10197
action: false,
@@ -109,7 +105,7 @@ function _maybeTransformEmberObjects(
109105
unobserves: false,
110106
};
111107

112-
const eoExtendExpressionPaths = getEOExtendExpressionCollection(j, root);
108+
const eoExtendExpressionPaths = getEOExtendExpressionCollection(root);
113109

114110
if (eoExtendExpressionPaths.length === 0) {
115111
logger.warn(

transforms/helpers/util/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ interface DecoratorPathInfo {
2727
readonly decoratorPath: string;
2828
}
2929

30+
export interface DecoratorImportSpecs {
31+
[ACTION_DECORATOR_NAME]: boolean;
32+
[CLASS_NAMES_DECORATOR_NAME]: boolean;
33+
[CLASS_NAME_BINDINGS_DECORATOR_NAME]: boolean;
34+
[ATTRIBUTE_BINDINGS_DECORATOR_NAME]: boolean;
35+
[LAYOUT_DECORATOR_NAME]: boolean;
36+
[LAYOUT_DECORATOR_LOCAL_NAME]: boolean;
37+
[OFF_DECORATOR_NAME]: boolean;
38+
[TAG_NAME_DECORATOR_NAME]: boolean;
39+
[UNOBSERVES_DECORATOR_NAME]: boolean;
40+
}
41+
3042
export const DECORATOR_PATHS: ReadonlyMap<string, DecoratorPathInfo> = new Map([
3143
[
3244
'@ember/object',

0 commit comments

Comments
 (0)