Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- In JavaScript and TypeScript, a file that re-exports a module, like an `index.ts` with `export * from './button'` or `export { Button } from './button'`, now counts as importing that module, the way an `import` statement does. `codegraph affected` and a module's dependents now include the barrels that re-export it; before, a barrel made only of `export *` lines showed no dependency at all. Re-index JavaScript and TypeScript projects after upgrading.
- `codegraph sync` and the file watcher now also update the files that import through an `index.ts` re-exporting another barrel, like `export * from './dropdown-menu'`, when that inner barrel is deleted or added. Before, their imports stayed linked through the deleted barrel, or stayed linked the way they were while it was missing, until each importing file changed or the project was re-indexed.
- An import in a Vue, Svelte or Astro component now links the component to the file it imports, as an import in a script does, and so does an import of a scoped package that a `tsconfig` path or a workspace package maps to a project file, like `import { debounce } from '@excalidraw/common'`. Before, only the names such an import binds were linked, so a barrel or a module imported for its side effects showed no dependency from those files in `codegraph affected`, and `codegraph sync` could not update them when a barrel they import through was deleted or added. `codegraph sync` and the file watcher also link such an import again, and one written as `.` or `..`, when the file it names comes back. Re-index JavaScript, TypeScript, Vue, Svelte and Astro projects after upgrading.
- In CFML, `<cfinclude template="formlistshared.cfm">` now links the component, function or template it is written in to the file it includes, as `include "formlistshared.cfm";` in a script already did, and so do `cfinclude(template="…")`, `include template="…";` and an `include` inside a function. A relative path like `../shared/helpers.cfm` now finds that file even when another file has the same name. Before, these includes left no trace in the graph, and `cfinclude(…)` showed up as a call to a function that doesn't exist. Thanks @HarryMuc for the report. (#2651)
- In a pnpm, npm, Yarn or Bun workspace whose globs reach packages more than one folder down, like pnpm's `packages/**/*` or npm's `libs/**/*`, those packages now count as part of the project, so an import of one is no longer taken for an outside package's, and `codegraph sync` and the file watcher relink the calls through it when its files change. A workspace glob can now also name packages by part of a folder name (`packages/plugin-*`) or by a folder below the wildcard (`apps/*/web`), and a negated glob like `!**/example` leaves packages out, as the package managers do. Before, only the folders directly under a glob's first wildcard were read, and a negated glob was ignored. Re-index after upgrading.
- In a project that uses Vue, an import written through an `@/` or `~/` alias, like `import { Button } from '@/components'`, now links the file it imports, as it does in any other project, so `codegraph sync` and the file watcher update the files that import a module when that module, or a barrel they import through, is added or deleted. Before, Vue support linked such an import to the first declaration or the component in that file, which sync does not follow, and an import through a `tsconfig` alias that starts like one of Nuxt's built-in modules, like `#app/where`, linked the importing file to itself. Re-index Vue projects after upgrading.
- `codegraph sync` and the file watcher now move an import written without an extension, like `import { sizeOf } from './button'`, to a file added beside the one it found before when the new file comes first, such as `button.ts` next to `button.vue`. Before, the import and the names it binds stayed linked to `button.vue` until the importing file changed or the project was re-indexed. JavaScript, TypeScript, Vue, Svelte and Astro.
Expand Down
162 changes: 162 additions & 0 deletions __tests__/cfml-include.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**
* CFML includes (#2651). Only cfscript's `include "x.cfm";`, written outside
* any function, linked the including code to the template: the tag form
* `<cfinclude template="x.cfm">` — the common one in tag-based code — made
* nothing anywhere, the function form `cfinclude(template="x.cfm")` became a
* call to a function named `cfinclude`, and an `include` inside a function
* was dropped. A template path is relative to the including file's directory
* (or to the web root with a leading `/`), so `../shared/helpers.cfm` is that
* file even when another `helpers.cfm` exists.
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { CodeGraph } from '../src';

let dir = '';
let edges: string[] = [];
let imports: string[] = [];
let unresolved: string[] = [];
let importTargetKinds: string[] = [];

const files: Record<string, string> = {
// Tag form at component scope: same directory, relative, upper case, unquoted.
'app/core/baseform.cfc': `<cfcomponent output="false">
\t<cffunction name="init"><cfreturn this></cffunction>
\t<cfinclude template="formlistshared.cfm">
\t<cfinclude template="../shared/helpers.cfm">
\t<CFINCLUDE TEMPLATE="Upper.cfm">
\t<cfinclude template=bare.cfm>
</cfcomponent>
`,
// Tag form in a function body, nested in control flow; runtime-built paths and <cfmodule> are no includes.
'app/core/report.cfc': `<cfcomponent>
\t<cffunction name="render">
\t\t<cfif arguments.full>
\t\t\t<cfinclude template="formlistshared.cfm" />
\t\t</cfif>
\t\t<cfinclude template="#variables.view#">
\t\t<cfinclude template="#variables.dir#/helpers.cfm">
\t\t<cfmodule template="formlistshared.cfm">
\t</cffunction>
</cfcomponent>
`,
// Tag form in a template, and a path from the web root.
'app/page.cfm': `<cfinclude template="core/formlistshared.cfm">
<cfif x><cfinclude template="/app/shared/helpers.cfm"></cfif>
`,
// A template and a function including a path no file has (a server
// mapping's): the function may not link to the template's import of it.
'app/one.cfm': `<cfinclude template="/mapped/runner.cfm">\n`,
'app/core/runtests.cfc': `component {\n\tfunction run() {\n\t\tinclude "/mapped/runner.cfm";\n\t}\n}\n`,
// The only file of the name an include names is the includer itself.
'app/views/wheels/wheels.cfm': `<cfinclude template="../../wheels/public/wheels.cfm">\n`,
// Script forms: in a function body, the attribute form, the function form.
'app/core/scriptform.cfc': `component {
\tinclude "formlistshared.cfm";
\tfunction render() {
\t\tinclude "../shared/helpers.cfm";
\t\tcfinclude(template="upper.cfm");
\t\tcfinclude("bare.cfm");
\t\tCFINCLUDE( TEMPLATE = 'formlistshared.cfm' );
\t}
\tfunction other() {
\t\tinclude template="formlistshared.cfm";
\t\tcfinclude(template=variables.view);
\t\tinclude "#variables.view#";
\t\tinclude "#variables.dir#/helpers.cfm";
\t}
}
`,
'app/core/formlistshared.cfm': `<cffunction name="checkRightsPartPrepare"></cffunction>\n`,
'app/core/Upper.cfm': `<cfset x = 1>\n`,
'app/core/bare.cfm': `<cfset x = 1>\n`,
'app/shared/helpers.cfm': `<cfset x = 1>\n`,
// A namesake the relative path does not name.
'other/shared/helpers.cfm': `<cfset x = 1>\n`,
};

beforeAll(async () => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cfml-include-'));
for (const [rel, body] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(dir, rel)), { recursive: true });
fs.writeFileSync(path.join(dir, rel), body);
}
const cg = await CodeGraph.init(dir, { silent: true });
await cg.indexAll();
const db = (cg as any).db.db;
edges = db
.prepare(
`SELECT s.qualified_name src, t.file_path tgt FROM edges e
JOIN nodes s ON s.id = e.source JOIN nodes t ON t.id = e.target
WHERE e.kind = 'imports'`
)
.all()
.map((r: any) => `${r.src} -> ${r.tgt}`)
.sort();
imports = db.prepare(`SELECT qualified_name q FROM nodes WHERE kind = 'import'`).all().map((r: any) => r.q).sort();
importTargetKinds = db
.prepare(`SELECT DISTINCT t.kind k FROM edges e JOIN nodes t ON t.id = e.target WHERE e.kind = 'imports'`)
.all()
.map((r: any) => r.k);
unresolved = db
.prepare(`SELECT reference_kind k, reference_name n FROM unresolved_refs`)
.all()
.map((r: any) => `${r.k} ${r.n}`);
cg.close?.();
});

afterAll(() => {
if (dir) fs.rmSync(dir, { recursive: true, force: true });
});

describe('CFML includes (#2651)', () => {
it('links <cfinclude template="…"> at component scope to the template, relative to the including file', () => {
expect(edges).toEqual(expect.arrayContaining([
'app/core/baseform.cfc::baseform -> app/core/formlistshared.cfm',
'app/core/baseform.cfc::baseform -> app/shared/helpers.cfm',
'app/core/baseform.cfc::baseform -> app/core/Upper.cfm',
'app/core/baseform.cfc::baseform -> app/core/bare.cfm',
]));
expect(imports).toContain('baseform::formlistshared.cfm');
});

it('links a <cfinclude> in a function body from that function', () => {
expect(edges).toContain('report::render -> app/core/formlistshared.cfm');
expect(imports).toContain('report::render::formlistshared.cfm');
});

it('links a <cfinclude> in a template, including one from the web root', () => {
expect(edges).toContain('app/page.cfm -> app/core/formlistshared.cfm');
expect(edges).toContain('app/page.cfm -> app/shared/helpers.cfm');
});

it('makes nothing of a template built at runtime or of <cfmodule>', () => {
expect(edges.filter((e) => e.startsWith('report::render ->'))).toEqual(['report::render -> app/core/formlistshared.cfm']);
expect(imports.filter((q) => q.startsWith('report::'))).toEqual(['report::render::formlistshared.cfm']);
expect(edges.filter((e) => e.startsWith('scriptform::other ->'))).toEqual(['scriptform::other -> app/core/formlistshared.cfm']);
expect(imports.filter((q) => q.includes('#'))).toEqual([]);
});

it('links the script forms: in a function body, `include template=…` and cfinclude(…)', () => {
expect(edges).toEqual(expect.arrayContaining([
'scriptform::render -> app/shared/helpers.cfm',
'scriptform::render -> app/core/Upper.cfm',
'scriptform::render -> app/core/bare.cfm',
'scriptform::render -> app/core/formlistshared.cfm',
'scriptform::other -> app/core/formlistshared.cfm',
]));
expect(unresolved.filter((r) => /cfinclude/i.test(r))).toEqual([]);
});

it('never links a relative include to a namesake the path does not name', () => {
expect(edges.filter((e) => e.endsWith('other/shared/helpers.cfm'))).toEqual([]);
});

it('links an include to a file or to nothing: never to an import statement, nor to the including file', () => {
expect(importTargetKinds).toEqual(['file']);
expect(edges.filter((e) => e.startsWith('app/one.cfm') || e.startsWith('runtests::'))).toEqual([]);
expect(edges.filter((e) => e.startsWith('app/views/wheels/wheels.cfm'))).toEqual([]);
});
});
65 changes: 63 additions & 2 deletions src/extraction/cfml-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ export class CfmlExtractor {
case 'cf_function_tag':
if (declScope) this.extractFunctionTag(node, parentClassName ? containerId : undefined, containerId, parentClassName);
return;
case 'cf_selfclose_tag':
// `<cfinclude template="…">`, wherever it is written (#2651). Its
// attributes are still walked below for `#hash#` expressions.
if (this.selfCloseTagName(node) === 'include') this.extractIncludeTag(node, containerId);
break;
case 'cf_tag': {
const name = this.cfTagName(node);
if (name === 'interface' && declScope && !parentClassName) {
Expand Down Expand Up @@ -559,6 +564,60 @@ export class CfmlExtractor {
return nameNode ? this.source.substring(nameNode.startIndex, nameNode.endIndex).toLowerCase() : undefined;
}

/**
* A void tag's name, lowercased, without the `cf` prefix (`<cfInclude …>` →
* `include`). The grammar keeps no node for it: it is the text after `<cf`.
*/
private selfCloseTagName(tag: SyntaxNode): string | undefined {
const match = /^<cf([A-Za-z_]\w*)/i.exec(this.source.slice(tag.startIndex, tag.startIndex + 64));
return match?.[1]?.toLowerCase();
}

/**
* `<cfinclude template="formlistshared.cfm">` (#2651) includes the template
* as cfscript's `include "formlistshared.cfm";` does, so it is extracted the
* same way: an import node in the scope it sits in (the function, the
* component, or the file of a template) and an `imports` reference from
* that scope, which the resolver links to the file. A template built at
* runtime (`template="#dir#/x.cfm"`) names no file the index can link.
*/
private extractIncludeTag(tag: SyntaxNode, scopeId: string): void {
const valueNode = this.tagAttrValueNode(tag, 'template', true);
const template = valueNode ? this.source.substring(valueNode.startIndex, valueNode.endIndex).trim() : '';
if (!template) return;
// Qualified like the generic extractor's import nodes: under the scope's
// own name, without the file path (`baseform::formlistshared.cfm`).
const scope = this.nodes.find((n) => n.id === scopeId);
const prefix = !scope || scope.kind === 'file' ? '' : scope.kind === 'method' ? scope.qualifiedName : scope.name;
const line = tag.startPosition.row + 1;
const column = tag.startPosition.column;
const id = this.nodeIds.generate(this.filePath, 'import', template, line, column);
this.nodes.push({
id,
kind: 'import',
name: template,
qualifiedName: prefix ? `${prefix}::${template}` : template,
filePath: this.filePath,
language: this.language,
startLine: line,
endLine: tag.endPosition.row + 1,
startColumn: column,
endColumn: tag.endPosition.column,
signature: this.source.substring(tag.startIndex, tag.endIndex).trim(),
updatedAt: Date.now(),
});
this.edges.push({ source: scopeId, target: id, kind: 'contains' });
this.unresolvedReferences.push({
fromNodeId: scopeId,
referenceName: template,
referenceKind: 'imports',
filePath: this.filePath,
line,
column,
language: this.language,
});
}

/**
* Delegate a `<cfscript>...</cfscript>` tag body to the cfscript grammar.
* With `parentClassName` set (the block sits at component scope), functions
Expand Down Expand Up @@ -691,9 +750,10 @@ export class CfmlExtractor {
/**
* The `attribute_value` node of a tag attribute — `undefined` when the tag
* has no such attribute, `null` when its value isn't plain text (empty, or
* a `#hash#` expression).
* a `#hash#` expression). With `wholeValue`, also `null` when only part of
* the value is plain text (`template="#dir#/x.cfm"`).
*/
private tagAttrValueNode(tag: SyntaxNode, attrName: string): SyntaxNode | null | undefined {
private tagAttrValueNode(tag: SyntaxNode, attrName: string, wholeValue = false): SyntaxNode | null | undefined {
const attrs: SyntaxNode[] = [];
for (let i = 0; i < tag.namedChildCount; i++) {
const child = tag.namedChild(i);
Expand All @@ -716,6 +776,7 @@ export class CfmlExtractor {
const valueWrapper = attr.namedChildren.find(
(c: SyntaxNode) => c.type === 'quoted_cf_attribute_value' || c.type === 'cf_attribute_value'
);
if (wholeValue && valueWrapper?.namedChildCount !== 1) return null;
return valueWrapper?.namedChildren.find((c: SyntaxNode) => c.type === 'attribute_value') ?? null;
}
return undefined;
Expand Down
52 changes: 49 additions & 3 deletions src/extraction/languages/cfscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,44 @@ function cfmlVisibility(node: SyntaxNode): 'public' | 'private' | 'protected' |
return undefined;
}

/**
* A string literal's text when it is written out in full, or null when a
* `#…#` expression builds it at runtime (`"#dir#/x.cfm"`): such a path names
* no file the index can link.
*/
function literalString(node: SyntaxNode | null | undefined, source: string): string | null {
if (node?.type !== 'string' || node.namedChildren.some((c: SyntaxNode) => c.type !== 'string_fragment')) return null;
return getNodeText(node, source).replace(/^["']|["']$/g, '').trim() || null;
}

/** The value of a `template=…` argument or attribute, when it is a literal string. */
function templateArgument(node: SyntaxNode, source: string): string | null {
if (node.type !== 'assignment_expression') return null;
const left = getChildByField(node, 'left');
if (left?.type !== 'identifier' || getNodeText(left, source).toLowerCase() !== 'template') return null;
return literalString(getChildByField(node, 'right'), source);
}

/**
* Whether a call is `cfinclude(…)` — the function form of `include` and of
* `<cfinclude>` (ACF 11+, Lucee): an include of a template, not a call (#2651).
*/
export function isCfIncludeCall(node: SyntaxNode, source: string): boolean {
const fn = getChildByField(node, 'function');
return fn?.type === 'identifier' && getNodeText(fn, source).toLowerCase() === 'cfinclude';
}

/** The literal template `cfinclude(template="x.cfm")` or `cfinclude("x.cfm")` names. */
function cfIncludeCallTemplate(node: SyntaxNode, source: string): string | null {
const args = getChildByField(node, 'arguments');
if (!args) return null;
for (const arg of args.namedChildren) {
const named = templateArgument(arg, source);
if (named) return named;
}
return literalString(args.namedChildren[0], source);
}

export const cfscriptExtractor: LanguageExtractor = {
functionTypes: ['function_declaration', 'function_expression', 'arrow_function'],
classTypes: ['component'],
Expand Down Expand Up @@ -45,10 +83,18 @@ export const cfscriptExtractor: LanguageExtractor = {
const importText = source.substring(node.startIndex, node.endIndex).trim();

if (node.type === 'include_statement') {
// `include "path/to/file.cfm";` — the included template path.
// `include "path/to/file.cfm";` — the included template path — or the
// attribute form, `include template="path/to/file.cfm";` (#2651). A
// path built at runtime (`include "#view#";`) is no import.
const expr = node.namedChildren.find((c: SyntaxNode) => c.type === 'string');
if (!expr) return null;
const moduleName = getNodeText(expr, source).replace(/^["']|["']$/g, '');
const attr = expr ? undefined : node.namedChildren.find((c: SyntaxNode) => c.type === 'assignment_expression');
const moduleName = expr ? literalString(expr, source) : attr ? templateArgument(attr, source) : null;
return moduleName ? { moduleName, signature: importText } : null;
}

if (node.type === 'call_expression') {
// `cfinclude(template="path/to/file.cfm");` (#2651).
const moduleName = isCfIncludeCall(node, source) ? cfIncludeCallTemplate(node, source) : null;
return moduleName ? { moduleName, signature: importText } : null;
}

Expand Down
Loading