Skip to content

Commit

Permalink
fix: linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
d-jeffery committed Oct 5, 2023
1 parent 65e8b9e commit 5c971a0
Show file tree
Hide file tree
Showing 16 changed files with 1,782 additions and 364 deletions.
70 changes: 50 additions & 20 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
/**@type {import('eslint').Linter.Config} */
// eslint-disable-next-line no-undef
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {
'semi': [2, "always"],
'@typescript-eslint/no-unused-vars': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/no-non-null-assertion': 0,
'no-trailing-spaces': ["error", { "skipBlankLines": false }]
}
};
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"prettier",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:import/typescript"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2021,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"ignorePatterns": ["src/parser/grammar.ts"],
"rules": {
"no-case-declarations": "off",
"linebreak-style": [
"error",
"unix"
],
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "warn",
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
],
"max-len": [
"warn",
{
"code": 120
}
],
"object-curly-spacing": [
"error",
"always"
]
}
};
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tests/__snapshots__/*
src/parser/grammar.ts
*.ne
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": false,
"trailingComma": "all",
"printWidth": 120
}
17 changes: 9 additions & 8 deletions client/src/extension.browser.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { transformer } from '@openfga/syntax-transformer';
import { ExtensionContext, Uri, commands, window, workspace } from 'vscode';
import { LanguageClientOptions } from 'vscode-languageclient';
import { transformer } from "@openfga/syntax-transformer";
// eslint-disable-next-line import/no-unresolved
import { ExtensionContext, Uri, commands, window, workspace } from "vscode";
import { LanguageClientOptions } from "vscode-languageclient";

import { LanguageClient } from 'vscode-languageclient/browser';
import { LanguageClient } from "vscode-languageclient/browser";

let client: LanguageClient;

// this method is called when vs code is activated
export function activate(context: ExtensionContext) {

// Register the server for all document types
const documentSelector = [{ language: 'openfga' }];
const documentSelector = [{ language: "openfga" }];

// Options to control the language client
const clientOptions: LanguageClientOptions = {
Expand All @@ -24,7 +25,7 @@ export function activate(context: ExtensionContext) {
client.start();


const transformCommand = commands.registerCommand('openfga.commands.transformToJson', async () => {
const transformCommand = commands.registerCommand("openfga.commands.transformToJson", async () => {
const activeEditor = window.activeTextEditor;
if (!activeEditor) {
return;
Expand Down Expand Up @@ -53,9 +54,9 @@ export function deactivate(): Thenable<void> | undefined {

function createWorkerLanguageClient(context: ExtensionContext, clientOptions: LanguageClientOptions) {
// Create a worker. The worker main file implements the language server.
const serverMain = Uri.joinPath(context.extensionUri, 'server/out/server.browser.js');
const serverMain = Uri.joinPath(context.extensionUri, "server/out/server.browser.js");
const worker = new Worker(serverMain.toString(true));

// create the language server client to communicate with the server running in the worker
return new LanguageClient('openfgaLanguageServer', 'OpenFGA Language Server', clientOptions, worker);
return new LanguageClient("openfgaLanguageServer", "OpenFGA Language Server", clientOptions, worker);
}
137 changes: 69 additions & 68 deletions client/src/extension.node.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,70 @@
import * as path from 'path';
import { window, workspace, ExtensionContext, commands, Range } from 'vscode';
import { transformer } from '@openfga/syntax-transformer';

import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind
} from 'vscode-languageclient/node';

let client: LanguageClient;

export function activate(context: ExtensionContext) {

const module = path.join(__dirname, '..', '..', 'server', 'out', 'server.node.js');
const debugOptions = { execArgv: ['--nolazy', '--inspect=6012'] };
const serverOptions: ServerOptions = {
run: { module, transport: TransportKind.ipc },
debug: { module, transport: TransportKind.ipc, options: debugOptions}
};

// Options to control the language client
const clientOptions: LanguageClientOptions = {
// Register the server for all document types
documentSelector: [{ language: 'openfga' }],
synchronize: {
// Notify the server about file changes to '.clientrc files contained in the workspace
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
}
};

// Create the language client and start the client.
client = new LanguageClient(
'openfgaLanguageServer',
'OpenFGA Language Server',
serverOptions,
clientOptions
);

// Start the client. This will also launch the server
client.start();

const transformCommand = commands.registerCommand('openfga.commands.transformToJson', async () => {
const activeEditor = window.activeTextEditor;
if (!activeEditor) {
return;
}
const text = activeEditor.document.getText();

const modelInApiFormat = transformer.transformDSLToJSON(text);

const doc = await workspace.openTextDocument({
content: JSON.stringify(modelInApiFormat, null, " "),
language: "json"
});

return (await window.showTextDocument(doc)).document;
});

context.subscriptions.push(transformCommand);
}

export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
import * as path from "path";
// eslint-disable-next-line import/no-unresolved
import { window, workspace, ExtensionContext, commands } from "vscode";
import { transformer } from "@openfga/syntax-transformer";

import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind
} from "vscode-languageclient/node";

let client: LanguageClient;

export function activate(context: ExtensionContext) {

const module = path.join(__dirname, "..", "..", "server", "out", "server.node.js");
const debugOptions = { execArgv: ["--nolazy", "--inspect=6012"] };
const serverOptions: ServerOptions = {
run: { module, transport: TransportKind.ipc },
debug: { module, transport: TransportKind.ipc, options: debugOptions }
};

// Options to control the language client
const clientOptions: LanguageClientOptions = {
// Register the server for all document types
documentSelector: [{ language: "openfga" }],
synchronize: {
// Notify the server about file changes to '.clientrc files contained in the workspace
fileEvents: workspace.createFileSystemWatcher("**/.clientrc")
}
};

// Create the language client and start the client.
client = new LanguageClient(
"openfgaLanguageServer",
"OpenFGA Language Server",
serverOptions,
clientOptions
);

// Start the client. This will also launch the server
client.start();

const transformCommand = commands.registerCommand("openfga.commands.transformToJson", async () => {
const activeEditor = window.activeTextEditor;
if (!activeEditor) {
return;
}
const text = activeEditor.document.getText();

const modelInApiFormat = transformer.transformDSLToJSON(text);

const doc = await workspace.openTextDocument({
content: JSON.stringify(modelInApiFormat, null, " "),
language: "json"
});

return (await window.showTextDocument(doc)).document;
});

context.subscriptions.push(transformCommand);
}

export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
}
31 changes: 16 additions & 15 deletions client/src/test/diagnostics.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import * as vscode from 'vscode';
import * as assert from 'assert';
import { getDocUri, activate } from './helper';
// eslint-disable-next-line import/no-unresolved
import * as vscode from "vscode";
import * as assert from "assert";
import { getDocUri, activate } from "./helper";

suite('Should get diagnostics', () => {
const docUri = getDocUri('diagnostics.openfga');
suite("Should get diagnostics", () => {
const docUri = getDocUri("diagnostics.openfga");

test('Diagnoses uppercase texts', async () => {
test("Diagnoses uppercase texts", async () => {
await testDiagnostics(docUri, [
{ message: '`user` is not a valid type.', range: toRange(5, 20, 5, 24), severity: vscode.DiagnosticSeverity.Error, source: 'ModelValidationError' },
{ message: '`user` is not a valid type.', range: toRange(8, 19, 8, 23), severity: vscode.DiagnosticSeverity.Error, source: 'ModelValidationError' },
{ message: '`user` is not a valid type.', range: toRange(9, 19, 9, 23), severity: vscode.DiagnosticSeverity.Error, source: 'ModelValidationError' },
{ message: '`user` is not a valid type.', range: toRange(10, 24, 10, 28), severity: vscode.DiagnosticSeverity.Error, source: 'ModelValidationError' },
{ message: '`member` is not a valid relation for `organization`.', range: toRange(10, 29, 10, 48), severity: vscode.DiagnosticSeverity.Error, source: 'ModelValidationError' },
{ message: '`user` is not a valid type.', range: toRange(11, 25, 11, 29), severity: vscode.DiagnosticSeverity.Error, source: 'ModelValidationError' },
{ message: '`member` is not a valid relation for `organization`.', range: toRange(11, 30, 11, 49), severity: vscode.DiagnosticSeverity.Error, source: 'ModelValidationError' },
{ message: '`user` is not a valid type.', range: toRange(12, 25, 12, 29), severity: vscode.DiagnosticSeverity.Error, source: 'ModelValidationError' },
{ message: '`member` is not a valid relation for `organization`.', range: toRange(12, 30, 12, 49), severity: vscode.DiagnosticSeverity.Error, source: 'ModelValidationError' },
{ message: "`user` is not a valid type.", range: toRange(5, 20, 5, 24), severity: vscode.DiagnosticSeverity.Error, source: "ModelValidationError" },

Check warning on line 11 in client/src/test/diagnostics.test.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 160. Maximum allowed is 120
{ message: "`user` is not a valid type.", range: toRange(8, 19, 8, 23), severity: vscode.DiagnosticSeverity.Error, source: "ModelValidationError" },

Check warning on line 12 in client/src/test/diagnostics.test.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 160. Maximum allowed is 120
{ message: "`user` is not a valid type.", range: toRange(9, 19, 9, 23), severity: vscode.DiagnosticSeverity.Error, source: "ModelValidationError" },

Check warning on line 13 in client/src/test/diagnostics.test.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 160. Maximum allowed is 120
{ message: "`user` is not a valid type.", range: toRange(10, 24, 10, 28), severity: vscode.DiagnosticSeverity.Error, source: "ModelValidationError" },

Check warning on line 14 in client/src/test/diagnostics.test.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 162. Maximum allowed is 120
{ message: "`member` is not a valid relation for `organization`.", range: toRange(10, 29, 10, 48), severity: vscode.DiagnosticSeverity.Error, source: "ModelValidationError" },

Check warning on line 15 in client/src/test/diagnostics.test.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 187. Maximum allowed is 120
{ message: "`user` is not a valid type.", range: toRange(11, 25, 11, 29), severity: vscode.DiagnosticSeverity.Error, source: "ModelValidationError" },

Check warning on line 16 in client/src/test/diagnostics.test.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 162. Maximum allowed is 120
{ message: "`member` is not a valid relation for `organization`.", range: toRange(11, 30, 11, 49), severity: vscode.DiagnosticSeverity.Error, source: "ModelValidationError" },

Check warning on line 17 in client/src/test/diagnostics.test.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 187. Maximum allowed is 120
{ message: "`user` is not a valid type.", range: toRange(12, 25, 12, 29), severity: vscode.DiagnosticSeverity.Error, source: "ModelValidationError" },

Check warning on line 18 in client/src/test/diagnostics.test.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 162. Maximum allowed is 120
{ message: "`member` is not a valid relation for `organization`.", range: toRange(12, 30, 12, 49), severity: vscode.DiagnosticSeverity.Error, source: "ModelValidationError" },

Check warning on line 19 in client/src/test/diagnostics.test.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 187. Maximum allowed is 120
]);
});
});
Expand Down
21 changes: 10 additions & 11 deletions client/src/test/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import * as assert from "assert";
// eslint-disable-next-line import/no-unresolved
import { TextDocument, Uri, commands, window, workspace } from "vscode";

Check warning on line 3 in client/src/test/extension.test.ts

View workflow job for this annotation

GitHub Actions / lint

'Uri' is defined but never used
import { getDocUri, activate } from "./helper";
import { transformer } from "@openfga/syntax-transformer";


import * as assert from 'assert';
import { TextDocument, Uri, commands, window, workspace } from 'vscode';
import { getDocUri, activate } from './helper';
import { transformer } from '@openfga/syntax-transformer';
suite("Should execute command", () => {

const docUri = getDocUri("test.fga");

suite('Should execute command', () => {

const docUri = getDocUri('test.fga');

test('Generates expected JSON', async () => {
test("Generates expected JSON", async () => {

await activate(docUri);

// Get editor window
const editor = await window.showTextDocument(docUri);

// Get result from running command against what is in editor window
const resultFromCommand = await commands.executeCommand<TextDocument>('openfga.commands.transformToJson');
const resultFromCommand = await commands.executeCommand<TextDocument>("openfga.commands.transformToJson");

// Get original document
const original = String.fromCharCode.apply(null, await workspace.fs.readFile(getDocUri('test.fga')));
const original = String.fromCharCode.apply(null, await workspace.fs.readFile(getDocUri("test.fga")));

// Call transform directly for comparison, using original doc
const resultFromMethodCall = JSON.stringify(transformer.transformDSLToJSON(original), null, " ");
Expand Down
16 changes: 6 additions & 10 deletions client/src/test/helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as vscode from 'vscode';
import * as path from 'path';
// eslint-disable-next-line import/no-unresolved
import * as vscode from "vscode";
import * as path from "path";

export let doc: vscode.TextDocument;
export let editor: vscode.TextEditor;
Expand All @@ -16,7 +12,7 @@ export let platformEol: string;
*/
export async function activate(docUri: vscode.Uri) {
// The extensionId is `publisher.name` from package.json
const ext = vscode.extensions.getExtension('openfga.openfga-vscode')!;
const ext = vscode.extensions.getExtension("openfga.openfga-vscode")!;
await ext.activate();
try {
doc = await vscode.workspace.openTextDocument(docUri);
Expand All @@ -32,13 +28,13 @@ async function sleep(ms: number) {
}

export const getDocPath = (p: string) => {
return path.resolve(__dirname, '../../testFixture', p);
return path.resolve(__dirname, "../../testFixture", p);
};
export const getDocUri = (p: string) => {
if (process.env.VSCODE_TEST_NODE) {
return vscode.Uri.file(getDocPath(p));
} else {
return vscode.Uri.file(p).with({ scheme: 'vscode-test-web', authority: 'mount', path: `/${p}` });
return vscode.Uri.file(p).with({ scheme: "vscode-test-web", authority: "mount", path: `/${p}` });
}
};

Expand Down
Loading

0 comments on commit 5c971a0

Please sign in to comment.