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

Analyzed .gjs and .gts files #28

Merged
merged 5 commits into from
Aug 27, 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
5 changes: 5 additions & 0 deletions .changeset/quick-eyes-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"analyze-ember-project-dependencies": minor
---

Analyzed .gjs and .gts files
1 change: 1 addition & 0 deletions packages/analyze-ember-project-dependencies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@codemod-utils/ember-cli-string": "^1.1.4",
"@codemod-utils/files": "^2.0.4",
"@codemod-utils/json": "^1.1.9",
"content-tag": "^2.0.1",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function getPackageRoots(options: Options): string[] {
const { projectRoot } = options;

const packageRoots = findFiles('**/package.json', {
ignoreList: ['**/dist/**/*', '**/node_modules/**/*'],
ignoreList: ['**/{dist,node_modules}/**/*'],
projectRoot,
}).map((filePath) => {
return join(projectRoot, filePath.replace(/package\.json$/, ''));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,7 @@ import type {
ProjectData,
ProjectDependencies,
} from '../types/index.js';
import { findDependenciesInClass } from '../utils/find-dependencies/in-class.js';
import { findDependenciesInTemplate } from '../utils/find-dependencies/in-template.js';

const patterns = {
app: ['app/**/*.{hbs,js,ts}', 'tests/**/*.{js,ts}', '*.{js,ts}'],
node: ['**/*.{js,ts}', '*.{js,ts}'],
'v1-addon': [
'addon/**/*.{hbs,js,ts}',
'addon-test-support/**/*.{js,ts}',
'tests/**/*.{js,ts}',
'*.{js,ts}',
],
'v2-addon': ['src/**/*.{hbs,js,ts}', '*.{cjs,js,mjs,ts}'],
};
import { analyzeFile, patterns } from '../utils/find-dependencies/index.js';

export function findDependencies(
projectData: ProjectData,
Expand All @@ -36,11 +23,7 @@ export function findDependencies(
const _unknowns = new Set<string>();

const filePaths = findFiles(patterns[packageType], {
ignoreList: [
'**/declarations/**/*',
'**/dist/**/*',
'**/node_modules/**/*',
],
ignoreList: ['**/{declarations,dist,node_modules}/**/*'],
projectRoot: packageRoot,
});

Expand All @@ -49,24 +32,9 @@ export function findDependencies(
const path = join(packageRoot, filePath);
const file = readFileSync(path, 'utf8');

if (filePath.endsWith('.hbs')) {
const { dependencies, unknowns } = findDependenciesInTemplate(file, {
entities,
filePath,
});

dependencies.forEach((dependency) => _dependencies.add(dependency));
unknowns.forEach((unknown) => _unknowns.add(unknown));

return;
}

const isTypeScript = filePath.endsWith('.ts');

const { dependencies, unknowns } = findDependenciesInClass(file, {
const { dependencies, unknowns } = analyzeFile(file, {
entities,
filePath,
isTypeScript,
packageName,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Preprocessor } from 'content-tag';

export function extractClass(file: string): string {
const preprocessor = new Preprocessor();

return preprocessor.process(file);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import type { Entities, PackageAnalysis } from '../../types/index.js';
import { findModules, findServices } from './in-class/index.js';
import type { PackageAnalysis } from '../../types/index.js';
import { findModules, findServices } from './analyze-class/index.js';
import type { Data } from './index.js';

export type Data = {
entities: Entities;
filePath: string;
isTypeScript: boolean;
packageName: string;
};

export function findDependenciesInClass(
file: string,
data: Data,
): PackageAnalysis {
export function analyzeClass(file: string, data: Data): PackageAnalysis {
const resultsForModules = findModules(file, data);
const resultsForServices = findServices(file, data);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AST } from '@codemod-utils/ast-javascript';

import type { PackageAnalysis } from '../../../types/index.js';
import type { Data } from '../in-class.js';
import type { Data } from '../index.js';

const MODULE_PREFIXES_TO_IGNORE: string[] = [
'.',
Expand Down Expand Up @@ -39,7 +39,8 @@ export function findModules(file: string, data: Data): PackageAnalysis {
const dependencies = new Set<string>();
const unknowns = new Set<string>();

const traverse = AST.traverse(data.isTypeScript);
const isTypeScript = data.filePath.endsWith('.ts');
const traverse = AST.traverse(isTypeScript);

traverse(file, {
visitCallExpression(node) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AST } from '@codemod-utils/ast-javascript';

import type { PackageAnalysis } from '../../../types/index.js';
import type { Data } from '../in-class.js';
import type { Data } from '../index.js';

function dasherize(value: string): string {
return value.replace(/([a-z\d])([A-Z])/g, '$1-$2').toLowerCase();
Expand All @@ -13,7 +13,8 @@ export function findServices(file: string, data: Data): PackageAnalysis {
const dependencies = new Set<string>();
const unknowns = new Set<string>();

const traverse = AST.traverse(data.isTypeScript);
const isTypeScript = data.filePath.endsWith('.ts');
const traverse = AST.traverse(isTypeScript);

traverse(file, {
visitClassProperty(node) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { PackageAnalysis } from '../../types/index.js';
import { extractClass } from '../ast/template-tag.js';
import { analyzeClass } from './analyze-class.js';
import type { Data } from './index.js';

export function analyzeTemplateTag(file: string, data: Data): PackageAnalysis {
const newFile = extractClass(file);

const newData = {
...data,
filePath: data.filePath.replace(/\.gjs$/, '.js').replace(/\.gts$/, '.ts'),
};

return analyzeClass(newFile, newData);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { PackageAnalysis } from '../../types/index.js';
import { findComponentsHelpersModifiers } from './analyze-template/index.js';
import type { Data } from './index.js';

export function analyzeTemplate(file: string, data: Data): PackageAnalysis {
const results = findComponentsHelpersModifiers(file, data);

return results;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AST } from '@codemod-utils/ast-template';

import type { PackageAnalysis } from '../../../types/index.js';
import type { Data } from '../in-template.js';
import type { Data } from '../index.js';

function findBlockParams(file: string): Set<string> {
const blockParams = new Set<string>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './find-components-helpers-modifiers.js';

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Entities, PackageAnalysis } from '../../types/index.js';
import { analyzeClass } from './analyze-class.js';
import { analyzeTemplate } from './analyze-template.js';
import { analyzeTemplateTag } from './analyze-template-tag.js';

export const patterns = {
app: [
'app/**/*.{gjs,gts,hbs,js,ts}',
'tests/**/*.{gjs,gts,js,ts}',
'*.{js,ts}',
],
node: ['**/*.{js,ts}', '*.{js,ts}'],
'v1-addon': [
'addon/**/*.{gjs,gts,hbs,js,ts}',
'addon-test-support/**/*.{js,ts}',
'tests/**/*.{gjs,gts,js,ts}',
'*.{js,ts}',
],
'v2-addon': ['src/**/*.{gjs,gts,hbs,js,ts}', '*.{cjs,js,mjs,ts}'],
};

export type Data = {
entities: Entities;
filePath: string;
packageName: string;
};

export function analyzeFile(file: string, data: Data): PackageAnalysis {
const { filePath } = data;

if (filePath.endsWith('.gjs') || filePath.endsWith('.gts')) {
return analyzeTemplateTag(file, data);
}

if (filePath.endsWith('.hbs')) {
return analyzeTemplate(file, data);
}

return analyzeClass(file, data);
}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.