Skip to content

Commit

Permalink
More cleanup from dogfooding
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan committed Jan 23, 2023
1 parent 64d7cd3 commit 067b0e8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 91 deletions.
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { gatherTelemetryForUrl, analyzeEmberObject } = require('ember-codemods-te

(async () => {
// FIXME: Remove
if (!process.env.DOGFOOD) {
if (!process.env['DOGFOOD']) {
await gatherTelemetryForUrl(process.argv[2], analyzeEmberObject);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"postpublish": "yarn clean",
"build": "tsc",
"clean": "tsc --build --clean",
"fixme": "yarn build; codemod-cli test && git checkout test/fixtures; yarn clean",
"fixme": "yarn build; codemod-cli test && node ./test/run-test.js && git checkout test/fixtures; yarn clean",
"lint": "concurrently \"npm:lint:*(!fix)\" --names \"lint:\"",
"lint:fix": "concurrently \"npm:lint:*:fix\" --names \"fix:\"",
"lint:js": "eslint . --cache",
Expand Down
9 changes: 5 additions & 4 deletions transforms/helpers/parse-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import makeEOProp, {
EOClassDecoratorProp,
} from './eo-prop/index';
import type { RuntimeData } from './runtime-data';
import { capitalizeFirstLetter, dig } from './util/index';
import { capitalizeFirstLetter } from './util/index';
import { assert, defined, isRecord, verified } from './util/types';

/**
Expand Down Expand Up @@ -134,10 +134,11 @@ export function getExpressionToReplace(
j,
eoExtendExpressionPath
);
const parentValue = dig(eoExtendExpressionPath, 'parentPath.value', isRecord);
const parentValue = eoExtendExpressionPath.parentPath?.value;
const isFollowedByCreate =
isRecord(parentValue['property']) &&
parentValue['property']['name'] === 'create';
isRecord(parentValue) &&
isRecord(parentValue.property) &&
parentValue.property['name'] === 'create';

let expressionToReplace:
| ASTPath<EOExtendExpression>
Expand Down
137 changes: 70 additions & 67 deletions transforms/helpers/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getTelemetryFor } from 'ember-codemods-telemetry-helpers';
import type { JSCodeshift } from 'jscodeshift';
import path from 'path';
import type { ASTPath, Collection, EOExtendExpression } from './ast';
import { isNode } from './ast';
import {
createDecoratorImportDeclarations,
getDecoratorImportInfos as getExistingDecoratorImportInfos,
Expand All @@ -18,10 +20,7 @@ import {
} from './parse-helper';
import { isRuntimeData } from './runtime-data';
import { createClass, withComments } from './transform-helper';
import { dig } from './util/index';
import { isString } from './util/types';
import { hasValidProps, isFileOfType, isTestFile } from './validation-helper';
import type { Collection } from './ast';

/** Main entry point for parsing and replacing ember objects */
export default function maybeTransformEmberObjects(
Expand All @@ -42,13 +41,14 @@ export default function maybeTransformEmberObjects(
return;
}

// FIXME: Revert
const runtimeData = getTelemetryFor(path.resolve(filePath)) ?? {};
if (!isRuntimeData(runtimeData)) {
// logger.warn(
// `[${filePath}]: SKIPPED Could not find runtime data NO_RUNTIME_DATA`
// );
// return;
const runtimeData = process.env['DOGFOOD']
? {}
: getTelemetryFor(path.resolve(filePath));
if (!runtimeData || !isRuntimeData(runtimeData)) {
logger.warn(
`[${filePath}]: SKIPPED Could not find runtime data NO_RUNTIME_DATA`
);
return;
}

const options: Options = {
Expand Down Expand Up @@ -100,73 +100,76 @@ function _maybeTransformEmberObjects(
};

// eslint-disable-next-line unicorn/no-array-for-each
getEOExtendExpressionCollection(j, root).forEach((eoExtendExpressionPath) => {
const { eoExpression, mixins } = parseEOExtendExpression(
eoExtendExpressionPath.value
);

const eoProps = getEOProps(
eoExpression,
existingDecoratorImportInfos,
options.runtimeData
);

const errors = hasValidProps(j, eoProps, options);
getEOExtendExpressionCollection(j, root).forEach(
(eoExtendExpressionPath: ASTPath<EOExtendExpression>) => {
const { eoExpression, mixins } = parseEOExtendExpression(
eoExtendExpressionPath.value
);

if (
dig(eoExtendExpressionPath, 'parentPath.value.type', isString) ===
'MemberExpression'
) {
errors.push(
'class has chained definition (e.g. EmberObject.extend().reopenClass();'
const eoProps = getEOProps(
eoExpression,
existingDecoratorImportInfos,
options.runtimeData
);
}

if (errors.length > 0) {
logger.warn(
`[${filePath}]: FAILURE \nValidation errors: \n\t${errors.join('\n\t')}`
const errors = hasValidProps(j, eoProps, options);

if (
isNode(eoExtendExpressionPath.parentPath?.value, 'MemberExpression')
) {
errors.push(
'class has chained definition (e.g. EmberObject.extend().reopenClass();'
);
}

if (errors.length > 0) {
logger.warn(
`[${filePath}]: FAILURE \nValidation errors: \n\t${errors.join(
'\n\t'
)}`
);
return;
}

let className = getClassName(
j,
eoExtendExpressionPath,
filePath,
options.runtimeData.type
);
return;
}

let className = getClassName(
j,
eoExtendExpressionPath,
filePath,
options.runtimeData.type
);
const callee = eoExtendExpressionPath.value.callee;
const superClassName = callee.object.name;

const callee = eoExtendExpressionPath.value.callee;
const superClassName = callee.object.name;
if (className === superClassName) {
className = `_${className}`;
}

if (className === superClassName) {
className = `_${className}`;
}

const es6ClassDeclaration = createClass(
j,
className,
eoProps,
superClassName,
mixins,
options
);
const es6ClassDeclaration = createClass(
j,
className,
eoProps,
superClassName,
mixins,
options
);

const expressionToReplace = getExpressionToReplace(
j,
eoExtendExpressionPath
);
j(expressionToReplace).replaceWith(
withComments(es6ClassDeclaration, expressionToReplace.value)
);
const expressionToReplace = getExpressionToReplace(
j,
eoExtendExpressionPath
);
j(expressionToReplace).replaceWith(
withComments(es6ClassDeclaration, expressionToReplace.value)
);

transformed = true;
transformed = true;

decoratorImportSpecs = getDecoratorsToImportSpecs(
eoProps.instanceProps,
decoratorImportSpecs
);
});
decoratorImportSpecs = getDecoratorsToImportSpecs(
eoProps.instanceProps,
decoratorImportSpecs
);
}
);

return { transformed, decoratorImportSpecs };
}
18 changes: 0 additions & 18 deletions transforms/helpers/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { JSCodeshift } from 'jscodeshift';
import type { Collection, Declaration } from '../ast';
import { isRecord, verified } from './types';

export const LAYOUT_DECORATOR_NAME = 'layout' as const;
export const LAYOUT_DECORATOR_LOCAL_NAME = 'templateLayout' as const;
Expand Down Expand Up @@ -173,23 +172,6 @@ export const LIFECYCLE_HOOKS = new Set([
'drop',
]);

/**
* Get a property from an object. Useful to get nested props on `any` types.
*/
export function dig<T>(
obj: unknown,
path: string,
condition: (value: unknown) => value is T,
message?: string
): T {
const segments = path.split('.');
let current: unknown = obj;
for (const segment of segments) {
current = verified(current, isRecord)[segment];
}
return verified(current, condition, message);
}

/** Get the first declaration in the program */
export function getFirstDeclaration(
j: JSCodeshift,
Expand Down

0 comments on commit 067b0e8

Please sign in to comment.