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

auto-fix lint rule for missing template imports #2309

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions packages/host/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ module.exports = {
'deprecation/deprecation': 'off',
'window-mock/mock-window-only': 'error',
'window-mock/wrapped-setup-helpers-only': 'error',
'ember/template-missing-invokable': [
'error',
{
invokables: {
fn: ['fn', '@ember/helper'],
on: ['on', '@ember/modifier'],
and: ['and', '@cardstack/boxel-ui/helpers'],
bool: ['bool', '@cardstack/boxel-ui/helpers'],
eq: ['eq', '@cardstack/boxel-ui/helpers'],
gt: ['gt', '@cardstack/boxel-ui/helpers'],
lt: ['lt', '@cardstack/boxel-ui/helpers'],
not: ['not', '@cardstack/boxel-ui/helpers'],
or: ['or', '@cardstack/boxel-ui/helpers'],
add: ['add', '@cardstack/boxel-ui/helpers'],
subtract: ['subtract', '@cardstack/boxel-ui/helpers'],
},
},
],
},
},
// node files
Expand Down
2 changes: 1 addition & 1 deletion packages/host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"ember-window-mock": "^1.0.1",
"eslint": "^8.52.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-ember": "^12.1.1",
"eslint-plugin-ember": "ember-cli/eslint-plugin-ember#870ee2fa24ae826d431fca8e6109dbc389f368f6",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-n": "^16.2.0",
"eslint-plugin-prettier": "^5.0.1",
Expand Down
31 changes: 31 additions & 0 deletions packages/realm-server/tests/realm-endpoints-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4294,6 +4294,37 @@ module(basename(__filename), function () {
});
});
});

module('linting endpoint', function () {
setupPermissionedRealm(hooks, {
john: ['read', 'write'],
});

test('401 with invalid JWT', async function (assert) {
let response = await request
.post('/_lint')
.set('Authorization', `Bearer invalid-token`)
.set('X-HTTP-Method-Override', 'QUERY')
.send(`console.log('hi')`);

assert.strictEqual(response.status, 401, 'HTTP 401 status');
});

test('user can do a lint with fix', async function (assert) {
let response = await request
.post('/_lint')
.set(
'Authorization',
`Bearer ${createJWT(testRealm, 'john', ['read', 'write'])}`,
)
.set('X-HTTP-Method-Override', 'QUERY')
.set('Accept', 'application/json')
.send(`console.log('hi')`);

assert.strictEqual(response.status, 200, 'HTTP 200 status');
assert.strictEqual(response.body, 'xxx');
});
});
});

module('Realm server with realm mounted at the origin', function (hooks) {
Expand Down
11 changes: 11 additions & 0 deletions packages/runtime-common/lint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface LintArgs {
source: string;
}

export interface LintResult {
output: string;
}

export async function lintFix({ source }: LintArgs): Promise<LintResult> {
return { output: source };
}
27 changes: 27 additions & 0 deletions packages/runtime-common/realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
type QueuePublisher,
type FileMeta,
type DirectoryMeta,
userInitiatedPriority,
} from './index';
import merge from 'lodash/merge';
import mergeWith from 'lodash/mergeWith';
Expand Down Expand Up @@ -85,6 +86,7 @@ import type {
RealmEventContent,
UpdateRealmEventContent,
} from 'https://cardstack.com/base/matrix-event';
import type { LintArgs, LintResult } from './lint';

export const REALM_ROOM_RETENTION_POLICY_MAX_LIFETIME = 60 * 60 * 1000;

Expand Down Expand Up @@ -227,6 +229,7 @@ export class Realm {
],
]);
#dbAdapter: DBAdapter;
#queue: QueuePublisher;

#disableMatrixRealmEvents = false;

Expand Down Expand Up @@ -299,6 +302,7 @@ export class Realm {
this.loaderTemplate = loader;

this.#adapter = adapter;
this.#queue = queue;
this.#realmIndexUpdater = new RealmIndexUpdater({
realm: this,
dbAdapter,
Expand Down Expand Up @@ -333,6 +337,7 @@ export class Realm {
SupportedMimeType.CardJson,
this.searchPrerendered.bind(this),
)
.query('/_lint', SupportedMimeType.JSON, this.lint.bind(this))
.get(
'/_types',
SupportedMimeType.CardTypeSummary,
Expand Down Expand Up @@ -1632,6 +1637,28 @@ export class Realm {
});
}

private async lint(
request: Request,
requestContext: RequestContext,
): Promise<Response> {
let source = await request.text();
let job = await this.#queue.publish<LintResult>({
jobType: `lint-source`,
concurrencyGroup: `linting:${this.url}`,
timeout: 10,
priority: userInitiatedPriority,
args: { source } satisfies LintArgs,
});
let result = await job.done;
return createResponse({
body: JSON.stringify(result),
init: {
headers: { 'content-type': SupportedMimeType.JSON },
},
requestContext,
});
}

private async searchPrerendered(
request: Request,
requestContext: RequestContext,
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-common/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum SupportedMimeType {
EventStream = 'text/event-stream',
HTML = 'text/html',
JSONAPI = 'application/vnd.api+json',
JSON = 'application/json',
CardTypeSummary = 'application/json',
All = '*/*',
}
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-common/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type RealmInfo,
} from '.';
import { MatrixClient } from './matrix-client';
import { lintFix } from './lint';

export interface Stats extends JSONTypes.Object {
instancesIndexed: number;
Expand Down Expand Up @@ -182,6 +183,7 @@ export class Worker {
this.#queue.register(`from-scratch-index`, this.fromScratch),
this.#queue.register(`incremental-index`, this.incremental),
this.#queue.register(`copy-index`, this.copy),
this.#queue.register(`lint-source`, lintFix),
]);
await this.#queue.start();
}
Expand Down
Loading