Skip to content

Commit 067b0e8

Browse files
committed
More cleanup from dogfooding
1 parent 64d7cd3 commit 067b0e8

5 files changed

Lines changed: 77 additions & 91 deletions

File tree

bin/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { gatherTelemetryForUrl, analyzeEmberObject } = require('ember-codemods-te
55

66
(async () => {
77
// FIXME: Remove
8-
if (!process.env.DOGFOOD) {
8+
if (!process.env['DOGFOOD']) {
99
await gatherTelemetryForUrl(process.argv[2], analyzeEmberObject);
1010
}
1111

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"postpublish": "yarn clean",
3636
"build": "tsc",
3737
"clean": "tsc --build --clean",
38-
"fixme": "yarn build; codemod-cli test && git checkout test/fixtures; yarn clean",
38+
"fixme": "yarn build; codemod-cli test && node ./test/run-test.js && git checkout test/fixtures; yarn clean",
3939
"lint": "concurrently \"npm:lint:*(!fix)\" --names \"lint:\"",
4040
"lint:fix": "concurrently \"npm:lint:*:fix\" --names \"fix:\"",
4141
"lint:js": "eslint . --cache",

transforms/helpers/parse-helper.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import makeEOProp, {
2323
EOClassDecoratorProp,
2424
} from './eo-prop/index';
2525
import type { RuntimeData } from './runtime-data';
26-
import { capitalizeFirstLetter, dig } from './util/index';
26+
import { capitalizeFirstLetter } from './util/index';
2727
import { assert, defined, isRecord, verified } from './util/types';
2828

2929
/**
@@ -134,10 +134,11 @@ export function getExpressionToReplace(
134134
j,
135135
eoExtendExpressionPath
136136
);
137-
const parentValue = dig(eoExtendExpressionPath, 'parentPath.value', isRecord);
137+
const parentValue = eoExtendExpressionPath.parentPath?.value;
138138
const isFollowedByCreate =
139-
isRecord(parentValue['property']) &&
140-
parentValue['property']['name'] === 'create';
139+
isRecord(parentValue) &&
140+
isRecord(parentValue.property) &&
141+
parentValue.property['name'] === 'create';
141142

142143
let expressionToReplace:
143144
| ASTPath<EOExtendExpression>

transforms/helpers/transform.ts

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getTelemetryFor } from 'ember-codemods-telemetry-helpers';
22
import type { JSCodeshift } from 'jscodeshift';
33
import path from 'path';
4+
import type { ASTPath, Collection, EOExtendExpression } from './ast';
5+
import { isNode } from './ast';
46
import {
57
createDecoratorImportDeclarations,
68
getDecoratorImportInfos as getExistingDecoratorImportInfos,
@@ -18,10 +20,7 @@ import {
1820
} from './parse-helper';
1921
import { isRuntimeData } from './runtime-data';
2022
import { createClass, withComments } from './transform-helper';
21-
import { dig } from './util/index';
22-
import { isString } from './util/types';
2323
import { hasValidProps, isFileOfType, isTestFile } from './validation-helper';
24-
import type { Collection } from './ast';
2524

2625
/** Main entry point for parsing and replacing ember objects */
2726
export default function maybeTransformEmberObjects(
@@ -42,13 +41,14 @@ export default function maybeTransformEmberObjects(
4241
return;
4342
}
4443

45-
// FIXME: Revert
46-
const runtimeData = getTelemetryFor(path.resolve(filePath)) ?? {};
47-
if (!isRuntimeData(runtimeData)) {
48-
// logger.warn(
49-
// `[${filePath}]: SKIPPED Could not find runtime data NO_RUNTIME_DATA`
50-
// );
51-
// return;
44+
const runtimeData = process.env['DOGFOOD']
45+
? {}
46+
: getTelemetryFor(path.resolve(filePath));
47+
if (!runtimeData || !isRuntimeData(runtimeData)) {
48+
logger.warn(
49+
`[${filePath}]: SKIPPED Could not find runtime data NO_RUNTIME_DATA`
50+
);
51+
return;
5252
}
5353

5454
const options: Options = {
@@ -100,73 +100,76 @@ function _maybeTransformEmberObjects(
100100
};
101101

102102
// eslint-disable-next-line unicorn/no-array-for-each
103-
getEOExtendExpressionCollection(j, root).forEach((eoExtendExpressionPath) => {
104-
const { eoExpression, mixins } = parseEOExtendExpression(
105-
eoExtendExpressionPath.value
106-
);
107-
108-
const eoProps = getEOProps(
109-
eoExpression,
110-
existingDecoratorImportInfos,
111-
options.runtimeData
112-
);
113-
114-
const errors = hasValidProps(j, eoProps, options);
103+
getEOExtendExpressionCollection(j, root).forEach(
104+
(eoExtendExpressionPath: ASTPath<EOExtendExpression>) => {
105+
const { eoExpression, mixins } = parseEOExtendExpression(
106+
eoExtendExpressionPath.value
107+
);
115108

116-
if (
117-
dig(eoExtendExpressionPath, 'parentPath.value.type', isString) ===
118-
'MemberExpression'
119-
) {
120-
errors.push(
121-
'class has chained definition (e.g. EmberObject.extend().reopenClass();'
109+
const eoProps = getEOProps(
110+
eoExpression,
111+
existingDecoratorImportInfos,
112+
options.runtimeData
122113
);
123-
}
124114

125-
if (errors.length > 0) {
126-
logger.warn(
127-
`[${filePath}]: FAILURE \nValidation errors: \n\t${errors.join('\n\t')}`
115+
const errors = hasValidProps(j, eoProps, options);
116+
117+
if (
118+
isNode(eoExtendExpressionPath.parentPath?.value, 'MemberExpression')
119+
) {
120+
errors.push(
121+
'class has chained definition (e.g. EmberObject.extend().reopenClass();'
122+
);
123+
}
124+
125+
if (errors.length > 0) {
126+
logger.warn(
127+
`[${filePath}]: FAILURE \nValidation errors: \n\t${errors.join(
128+
'\n\t'
129+
)}`
130+
);
131+
return;
132+
}
133+
134+
let className = getClassName(
135+
j,
136+
eoExtendExpressionPath,
137+
filePath,
138+
options.runtimeData.type
128139
);
129-
return;
130-
}
131140

132-
let className = getClassName(
133-
j,
134-
eoExtendExpressionPath,
135-
filePath,
136-
options.runtimeData.type
137-
);
141+
const callee = eoExtendExpressionPath.value.callee;
142+
const superClassName = callee.object.name;
138143

139-
const callee = eoExtendExpressionPath.value.callee;
140-
const superClassName = callee.object.name;
144+
if (className === superClassName) {
145+
className = `_${className}`;
146+
}
141147

142-
if (className === superClassName) {
143-
className = `_${className}`;
144-
}
145-
146-
const es6ClassDeclaration = createClass(
147-
j,
148-
className,
149-
eoProps,
150-
superClassName,
151-
mixins,
152-
options
153-
);
148+
const es6ClassDeclaration = createClass(
149+
j,
150+
className,
151+
eoProps,
152+
superClassName,
153+
mixins,
154+
options
155+
);
154156

155-
const expressionToReplace = getExpressionToReplace(
156-
j,
157-
eoExtendExpressionPath
158-
);
159-
j(expressionToReplace).replaceWith(
160-
withComments(es6ClassDeclaration, expressionToReplace.value)
161-
);
157+
const expressionToReplace = getExpressionToReplace(
158+
j,
159+
eoExtendExpressionPath
160+
);
161+
j(expressionToReplace).replaceWith(
162+
withComments(es6ClassDeclaration, expressionToReplace.value)
163+
);
162164

163-
transformed = true;
165+
transformed = true;
164166

165-
decoratorImportSpecs = getDecoratorsToImportSpecs(
166-
eoProps.instanceProps,
167-
decoratorImportSpecs
168-
);
169-
});
167+
decoratorImportSpecs = getDecoratorsToImportSpecs(
168+
eoProps.instanceProps,
169+
decoratorImportSpecs
170+
);
171+
}
172+
);
170173

171174
return { transformed, decoratorImportSpecs };
172175
}

transforms/helpers/util/index.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { JSCodeshift } from 'jscodeshift';
22
import type { Collection, Declaration } from '../ast';
3-
import { isRecord, verified } from './types';
43

54
export const LAYOUT_DECORATOR_NAME = 'layout' as const;
65
export const LAYOUT_DECORATOR_LOCAL_NAME = 'templateLayout' as const;
@@ -173,23 +172,6 @@ export const LIFECYCLE_HOOKS = new Set([
173172
'drop',
174173
]);
175174

176-
/**
177-
* Get a property from an object. Useful to get nested props on `any` types.
178-
*/
179-
export function dig<T>(
180-
obj: unknown,
181-
path: string,
182-
condition: (value: unknown) => value is T,
183-
message?: string
184-
): T {
185-
const segments = path.split('.');
186-
let current: unknown = obj;
187-
for (const segment of segments) {
188-
current = verified(current, isRecord)[segment];
189-
}
190-
return verified(current, condition, message);
191-
}
192-
193175
/** Get the first declaration in the program */
194176
export function getFirstDeclaration(
195177
j: JSCodeshift,

0 commit comments

Comments
 (0)