Skip to content

Commit 1cfc8ce

Browse files
fix: validation for event handlers (#1545)
* Revert "fix: builds for templates (#1540)" This reverts commit 8a79d05. * Revert "feat: validation for handlers from manifest file (#1535)" This reverts commit 7d5c818. * fix: call handler validation * chore(dependencies): updated changesets for modified dependencies * trigger ci --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 45f3197 commit 1cfc8ce

File tree

6 files changed

+42
-254
lines changed

6 files changed

+42
-254
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@graphprotocol/graph-cli": patch
3+
---
4+
dependencies updates:
5+
- Removed dependency [`@babel/core@^7.20.5` ↗︎](https://www.npmjs.com/package/@babel/core/v/7.20.5) (from `dependencies`)
6+
- Removed dependency [`@babel/preset-typescript@^7.18.6` ↗︎](https://www.npmjs.com/package/@babel/preset-typescript/v/7.18.6) (from `dependencies`)
7+
- Removed dependency [`memoizee@^0.4.15` ↗︎](https://www.npmjs.com/package/memoizee/v/0.4.15) (from `dependencies`)

.changeset/rich-turtles-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
fix call handler validation

packages/cli/package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
"type-check": "tsc --noEmit"
2929
},
3030
"dependencies": {
31-
"@babel/core": "^7.20.5",
32-
"@babel/preset-typescript": "^7.18.6",
3331
"@float-capital/float-subgraph-uncrashable": "^0.0.0-alpha.4",
3432
"@oclif/core": "2.8.6",
3533
"@oclif/plugin-autocomplete": "^2.3.6",
@@ -50,7 +48,6 @@
5048
"ipfs-http-client": "55.0.0",
5149
"jayson": "4.0.0",
5250
"js-yaml": "3.14.1",
53-
"memoizee": "^0.4.15",
5451
"prettier": "1.19.1",
5552
"request": "2.88.2",
5653
"semver": "7.4.0",
@@ -61,12 +58,10 @@
6158
"yaml": "1.10.2"
6259
},
6360
"devDependencies": {
64-
"@types/babel__core": "^7.20.5",
6561
"@types/debug": "^4.1.7",
6662
"@types/fs-extra": "^9.0.13",
6763
"@types/jest": "^29.0.0",
6864
"@types/js-yaml": "^3.12.7",
69-
"@types/memoizee": "^0.4.11",
7065
"@types/semver": "^7.3.13",
7166
"@types/which": "^2.0.1",
7267
"copyfiles": "^2.4.1",

packages/cli/src/commands/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default class BuildCommand extends Command {
106106
if (watch) {
107107
await compiler.watchAndCompile();
108108
} else {
109-
const result = await compiler.compile({ validate: false });
109+
const result = await compiler.compile({ validate: true });
110110
if (result === false) {
111111
this.exit(1);
112112
}

packages/cli/src/compiler/index.ts

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import * as toolbox from 'gluegun';
66
import immutable from 'immutable';
77
import type { IPFSHTTPClient } from 'ipfs-http-client';
88
import yaml from 'js-yaml';
9-
import memo from 'memoizee';
10-
import { parseSync } from '@babel/core';
119
import { Spinner, step, withSpinner } from '../command-helpers/spinner';
1210
import debug from '../debug';
1311
import { applyMigrations } from '../migrations';
@@ -18,18 +16,6 @@ import * as asc from './asc';
1816

1917
const compilerDebug = debug('graph-cli:compiler');
2018

21-
/** memoize the reading of the file so we don't have to read it every time */
22-
const readFile = memo((filename: string) => fs.readFileSync(filename, 'utf-8'));
23-
24-
/** Memoized parser for Babel, so we can simply just read from cache */
25-
const babelAst = memo((filename: string) => {
26-
const data = readFile(filename);
27-
return parseSync(data, {
28-
presets: ['@babel/preset-typescript'],
29-
filename,
30-
});
31-
});
32-
3319
interface CompilerOptions {
3420
ipfs: any;
3521
subgraphManifest: string;
@@ -364,19 +350,12 @@ export default class Compiler {
364350

365351
try {
366352
const dataSourceName = dataSource.getIn(['name']);
353+
367354
const baseDir = this.sourceDir;
368355
const absoluteMappingPath = path.resolve(baseDir, mappingPath);
369356
const inputFile = path.relative(baseDir, absoluteMappingPath);
370-
371357
this._validateMappingContent(absoluteMappingPath);
372358

373-
const eventHandlers = dataSource.getIn(['mapping', 'eventHandlers']);
374-
// TODO: improve the types
375-
for (const eventHandler of (eventHandlers as any).toJS()) {
376-
compilerDebug('Validating Event Handler %s', eventHandler.handler);
377-
this._validateHandler(absoluteMappingPath, eventHandler.handler);
378-
}
379-
380359
// If the file has already been compiled elsewhere, just use that output
381360
// file and return early
382361
const inputCacheKey = this.cacheKeyForFile(absoluteMappingPath);
@@ -452,13 +431,6 @@ export default class Compiler {
452431
const inputFile = path.relative(baseDir, absoluteMappingPath);
453432
this._validateMappingContent(absoluteMappingPath);
454433

455-
const eventHandlers = template.getIn(['mapping', 'eventHandlers']);
456-
// TODO: improve the types
457-
for (const eventHandler of (eventHandlers as any).toJS()) {
458-
compilerDebug('Validating Template handler %s', eventHandler.handler);
459-
this._validateHandler(absoluteMappingPath, eventHandler.handler);
460-
}
461-
462434
// If the file has already been compiled elsewhere, just use that output
463435
// file and return early
464436
const inputCacheKey = this.cacheKeyForFile(absoluteMappingPath);
@@ -517,7 +489,7 @@ export default class Compiler {
517489
}
518490

519491
_validateMappingContent(filePath: string) {
520-
const data = readFile(filePath);
492+
const data = fs.readFileSync(filePath);
521493
if (this.blockIpfsMethods && (data.includes('ipfs.cat') || data.includes('ipfs.map'))) {
522494
throw Error(`
523495
Subgraph Studio does not support mappings with ipfs methods.
@@ -527,31 +499,6 @@ export default class Compiler {
527499
}
528500
}
529501

530-
_validateHandler(filePath: string, handlerName: string) {
531-
const baselAst = babelAst(filePath);
532-
533-
const body = baselAst?.program.body;
534-
535-
if (!body) {
536-
throw Error(`Could not parse ${filePath}`);
537-
}
538-
539-
const exportedFunctionNames = body
540-
.map(statement => {
541-
if (
542-
statement.type === 'ExportNamedDeclaration' &&
543-
statement?.declaration?.type === 'FunctionDeclaration'
544-
) {
545-
return statement.declaration.id?.name;
546-
}
547-
})
548-
.filter(Boolean);
549-
550-
if (!exportedFunctionNames.includes(handlerName)) {
551-
throw Error(`Could not find handler '${handlerName}' in ${filePath}`);
552-
}
553-
}
554-
555502
async writeSubgraphToOutputDirectory(protocol: Protocol, subgraph: immutable.Map<any, any>) {
556503
const displayDir = `${this.displayPath(this.options.outputDir)}${toolbox.filesystem.separator}`;
557504

0 commit comments

Comments
 (0)