Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NO_TELEMETRY mode / Implement observer -> observes behavior #550

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
*.log
.eslintcache
transforms/**/*.js
!transforms/ember-object/__testfixtures__/**/*.js
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ If you have any _lazily loaded_ modules, such as modules from Ember Engines,
you'll need to make sure that the URL you provide loads these modules as well.
Otherwise, the codemod will not be able to detect them or analyze them.

To disable this feature, run with `NO_TELEMETRY=true` and omit the path to your local server:

```shell
NO_TELEMETRY=true npx ember-native-class-codemod [OPTIONS] path/of/files/ or/some**/*glob.js
```

DANGER: Disabling the telemetry may result in incorrect behavior. Carefully vet the results.

### Types

The `type` option can be used to further narrow down transforms to a particular type of
Expand Down
12 changes: 6 additions & 6 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
const { gatherTelemetryForUrl, analyzeEmberObject } = require('ember-codemods-telemetry-helpers');

(async () => {
await gatherTelemetryForUrl(process.argv[2], analyzeEmberObject);
let args = process.argv.slice(1);
if (process.env['NO_TELEMETRY'] !== 'true') {
await gatherTelemetryForUrl(process.argv[2], analyzeEmberObject);
args = process.argv.slice(2);
}

require('codemod-cli').runTransform(
__dirname,
'ember-object',
process.argv.slice(2) /* paths or globs */
);
require('codemod-cli').runTransform(__dirname, 'ember-object', args);
})();
4 changes: 2 additions & 2 deletions transforms/ember-object/__testfixtures__/-mock-telemetry.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"runtime": {
"computedProperties": ["computedMacro", "anotherMacro", "numPlusOne", "numPlusPlus", "error", "errorService"],
"observedProperties": [],
"observerProperties": {},
"observedProperties": ["prop"],
"observerProperties": { "observerProp": ["prop"] },
"offProperties": { "offProp": ["prop1", "prop2"] },
"overriddenActions": ["overriddenActionMethod"],
"overriddenProperties": ["overriddenMethod"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"noTelemetry": "false"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"noTelemetry": "false"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"decorators": false,
"inObjectLiterals": ["macro"]
"inObjectLiterals": ["macro"],
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"decorators": false
"decorators": false,
"noTelemetry": false
}
7 changes: 5 additions & 2 deletions transforms/ember-object/__testfixtures__/runtime.input.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Runtime from 'common/runtime';
import { computed, observer } from '@ember/object';
import { alias } from '@ember/object/computed';
import { computed } from '@ember/object';
import { service } from '@ember/service';
import Runtime from 'common/runtime';
import { customMacro, customMacroWithInput } from 'my-app/lib';

/**
* Program comments
Expand All @@ -19,6 +20,8 @@ export default Runtime.extend(MyMixin, {
error: service(),
errorService: service('error'),

observerProp: observer('prop', function() { return this.prop; }),

unobservedProp: null,
offProp: null,

Expand Down
3 changes: 3 additions & 0 deletions transforms/ember-object/__testfixtures__/runtime.options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"noTelemetry": "false"
}
6 changes: 5 additions & 1 deletion transforms/ember-object/__testfixtures__/runtime.output.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import classic from 'ember-classic-decorator';
import { off, unobserves } from '@ember-decorators/object';
import { off, unobserves, observes } from '@ember-decorators/object';
import { action, computed } from '@ember/object';
import { service } from '@ember/service';
import { alias } from '@ember/object/computed';
import Runtime from 'common/runtime';
import { customMacro, customMacroWithInput } from 'my-app/lib';

/**
* Program comments
Expand All @@ -26,6 +27,9 @@ export default class _Runtime extends Runtime.extend(MyMixin) {
@service('error')
errorService;

@observes('prop')
observerProp() { return this.prop; }

@unobserves('prop3', 'prop4')
unobservedProp;

Expand Down
85 changes: 76 additions & 9 deletions transforms/ember-object/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import mockTelemetryData from './__testfixtures__/-mock-telemetry.json';
const fixtureDir = 'transforms/ember-object/__testfixtures__/';
const testFiles = new GlobSync(`${fixtureDir}**/*.input.js`).found;

const OUTPUT_PARSER =
/^(?:\/\*\nExpect error:\n(?<expectedError>[\S\s]*)\n\*\/)?(?<content>[\S\s]*)$/;

const mockTelemetry: Record<string, unknown> = {};
for (const testFile of testFiles) {
const moduleName = testFile
Expand All @@ -31,7 +34,9 @@ interface TestCase {
testPath: string;
input: string;
output: string;
outputWithNoTelemetry: string;
expectedError: string | undefined;
expectedErrorWithNoTelemetry: string | undefined;
skipped: boolean;
options: string;
}
Expand All @@ -44,18 +49,27 @@ const testCases = testFiles.map((inputPath): TestCase => {
const testPath = path.join(fixtureDir, `${testName}${extension}`);
const input = readFileSync(inputPath, 'utf8');
const outputPath = path.join(fixtureDir, `${testName}.output${extension}`);
const outputWithNoTelemetryPath = path.join(
fixtureDir,
`${testName}.output-no-telemetry${extension}`
);

const fullOutput = readFileSync(outputPath, 'utf8');
const parsedOutput =
/^(?:\/\*\nExpect error:\n(?<expectedError>[\S\s]*)\n\*\/)?(?<content>[\S\s]*)$/.exec(
fullOutput
);
const output = parsedOutput?.groups?.['content'];
assert(output, `Expected to find file content in ${outputPath}`);
const expectedError = parsedOutput.groups?.['expectedError'];
const { fullOutput, output, expectedError } = parseOutput(outputPath);

const skipped = fullOutput.startsWith('/* Expect skipped */');

let outputWithNoTelemetry;
let expectedErrorWithNoTelemetry;
if (existsSync(outputWithNoTelemetryPath)) {
({
output: outputWithNoTelemetry,
expectedError: expectedErrorWithNoTelemetry,
} = parseOutput(outputWithNoTelemetryPath));
} else {
outputWithNoTelemetry = output;
expectedErrorWithNoTelemetry = expectedError;
}

const optionsPath = path.join(fixtureDir, `${testName}.options.json`);
const options = existsSync(optionsPath)
? readFileSync(optionsPath, 'utf8')
Expand All @@ -65,7 +79,9 @@ const testCases = testFiles.map((inputPath): TestCase => {
testPath,
input,
output,
outputWithNoTelemetry,
expectedError,
expectedErrorWithNoTelemetry,
skipped,
options,
};
Expand All @@ -74,7 +90,20 @@ const testCases = testFiles.map((inputPath): TestCase => {
describe('ember-object', () => {
describe.each(testCases)(
'$testName',
({ testPath, input, output, expectedError, skipped, options }) => {
({
testPath,
input,
output,
outputWithNoTelemetry,
expectedError,
expectedErrorWithNoTelemetry,
skipped,
options,
}) => {
const parsedOptions = options
? (JSON.parse(options) as Record<string, unknown>)
: {};

beforeEach(function () {
process.env['CODEMOD_CLI_ARGS'] = options;
});
Expand All @@ -90,10 +119,48 @@ describe('ember-object', () => {
test('is idempotent', function () {
runTest(testPath, output, output, expectedError, skipped);
});

// NOTE: To skip testing an input file in no telemetry file, make an
// options file with `{ "noTelemetry": "false" }`
if (parsedOptions['noTelemetry'] === undefined) {
describe('NO_TELEMETRY', function () {
beforeEach(function () {
process.env['CODEMOD_CLI_ARGS'] = JSON.stringify({
noTelemetry: true,
...parsedOptions,
});
});

test('transforms correctly', function () {
runTest(
testPath,
input,
outputWithNoTelemetry,
expectedErrorWithNoTelemetry,
skipped
);
});

// FIXME: is idempotent
});
}
}
);
});

function parseOutput(outputPath: string): {
fullOutput: string;
output: string;
expectedError: string | undefined;
} {
const fullOutput = readFileSync(outputPath, 'utf8');
const parsedOutput = OUTPUT_PARSER.exec(fullOutput);
const output = parsedOutput?.groups?.['content'];
assert(output, `Expected to find file content in ${outputPath}`);
const expectedError = parsedOutput.groups?.['expectedError'];
return { fullOutput, output, expectedError };
}

function runTest(
testPath: string,
input: string,
Expand Down
3 changes: 3 additions & 0 deletions transforms/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default function getConfig(dir = process.cwd()): UserOptions {
getFileConfig(dir),
getCliConfig()
);
if (process.env['NO_TELEMETRY'] === 'true') {
config.noTelemetry = true;
}
return UserOptionsSchema.parse(config);
}

Expand Down
4 changes: 2 additions & 2 deletions transforms/helpers/decorator-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from './util/types';
import { COMPUTED_DECORATOR_NAME, METHOD_DECORATORS } from './util/index';
import type * as AST from '../helpers/ast';
import { COMPUTED_DECORATOR_NAME, METHOD_DECORATORS } from './util/index';
import { assert } from './util/types';

export interface DecoratorImportInfo {
name: string;
Expand Down
9 changes: 8 additions & 1 deletion transforms/helpers/eo-extend-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ export default class EOExtendExpression {
) {
const raw = path.value;

this.className = getClassName(path, filePath, options.runtimeData.type);
this.superClassName = raw.callee.object.name;
this.className = getClassName(
path,
filePath,
this.superClassName,
options.runtimeData?.type
);

const mixins: AST.EOMixin[] = [];
for (const arg of raw.arguments) {
Expand Down Expand Up @@ -92,6 +97,7 @@ export default class EOExtendExpression {
templateLayout: false,
off: false,
tagName: false,
observes: false,
unobserves: false,
};
const { properties } = this;
Expand All @@ -110,6 +116,7 @@ export default class EOExtendExpression {
specs.templateLayout || prop.decoratorImportSpecs.templateLayout,
off: specs.off || prop.decoratorImportSpecs.off,
tagName: specs.tagName || prop.decoratorImportSpecs.tagName,
observes: specs.observes || prop.decoratorImportSpecs.observes,
unobserves: specs.unobserves || prop.decoratorImportSpecs.unobserves,
};
}
Expand Down
Loading
Loading