diff --git a/src/ruleset/asyncapi-rules.ts b/src/ruleset/asyncapi-rules.ts index 95df22e..2a338bc 100644 --- a/src/ruleset/asyncapi-rules.ts +++ b/src/ruleset/asyncapi-rules.ts @@ -3,9 +3,7 @@ import addField from './functions/addField'; import deleteEndingSlash from './functions/deleteEndingSlash'; import latestVersionUpdate from './functions/latestVersionUpdate'; import deleteEmptyParam from './functions/deleteEmptyParam'; -import createNewParam from './functions/createNewParam'; import deleteID from './functions/deleteID'; -import createNewID from './functions/createNewID'; import deleteRepeatedTags from './functions/deleteRepeatedTags'; import renameRepeatedTag from './functions/renameRepeatedTag'; import addDescription from './functions/addDescription'; @@ -123,13 +121,6 @@ export default { given: '$.channels.', field: '', function: deleteEmptyParam - }, - { - name: 'Quick fix - create new param', - given: '$.chennels', - field: 'channels', - function: createNewParam - } ] }, @@ -143,27 +134,9 @@ export default { given: '$.channels.', field: 'messsageId', function: deleteID - }, - { - name: 'Quick fix - create new messageId', - given: '$..messageId', - field: 'messageId', - function: createNewID } ] }, - "asyncapi-operation-operationId": { - description: 'Operation must have "operationId".', - recommended: true, - given: '$', - fix: { - name: 'Quick fix - create an operationId', - given: '$.channels[*][publish,subscribe]', - field: 'operationId', - function: createNewID - - } - }, "asyncapi-operation-operationId-uniqueness": { description: '"operationId" must be unique across all the operations.', recommended: true, @@ -174,12 +147,6 @@ export default { given: '$.channels[*][publish,subscribe]', field: 'operationId', function: deleteID - }, - { - name: 'Quick fix - create new operationId', - given: '$.channels[*][publish,subscribe]', - field: 'operationId', - function: createNewID } ] }, diff --git a/src/ruleset/functions/createNewID.ts b/src/ruleset/functions/createNewID.ts deleted file mode 100644 index b535a80..0000000 --- a/src/ruleset/functions/createNewID.ts +++ /dev/null @@ -1,79 +0,0 @@ -import * as vscode from 'vscode'; -import * as yaml from 'js-yaml'; -import { JSONPath } from 'jsonpath-plus'; - -function getMessageIDMap(documentContent: string, given: string, field: string) { - const IDMap = new Set(); - try { - let jsonObject = yaml.load(documentContent); - const queryResult = JSONPath({ - path: given, json: jsonObject as any, resultType: 'all' - }); - for (const result of queryResult) { - IDMap.add(result.value); - } - } catch (error) { - console.error("Failed to parse document content as YAML", error); - } - return IDMap; -} - -function getLeadingSpaces(target: string): string { - console.log("target: ", target); - let res = ''; - for (const c of target) { - if (c === ' ') { - res += ' '; - } else { - break; - } - } - return res; -} - -export default async function createNewID(document: vscode.TextDocument, range: vscode.Range, given: string, field: string) { - const documentContent = document.getText(); - - const start = new vscode.Position(range.start.line, 0); - const end = new vscode.Position(range.end.line, document.lineAt(range.end.line).text.length); - const selection = new vscode.Selection(start, end); - const editor = vscode.window.activeTextEditor; - if (editor) { - editor.selections = [selection]; - } - const selectedText = document.getText(new vscode.Range(start, end)); - console.log("Selected text: \n", selectedText, range.start.line, range.end.line); - const lines = documentContent.split('\n'); - const idMap = getMessageIDMap(documentContent, given, field); - try { - const paramName = await vscode.window.showInputBox({ - prompt: `Enter a new unique ${field}`, - validateInput: (value: string) => { - if (!value) { - return `${field} cannot be empty`; - } - if (idMap.has(value)) { - return `${value} is already existed!`; - } - return null; - } - }); - const newText = field === 'messageId' ? selectedText.replace(/messageId: \w+/g, `messageId: ${paramName ? paramName : ''}`) : selectedText.replace(/operationId: \w+/g, `operationId: ${paramName ? paramName : ''}`); - // ID is already existed - if (range.start.line === range.end.line) { - lines[range.start.line] = paramName ? newText : selectedText; - } - // Add a new operationId - else { - let tabSize = 0; - if (editor) { - tabSize = editor.options.tabSize as number; - } - console.log(`tabSize is ${tabSize}`); - lines.splice(range.start.line + 1, 0, getLeadingSpaces(lines[range.start.line]) + ' '.repeat(tabSize) + `operationId: ${paramName}`); - } - return lines.join('\n'); - } catch (error) { - console.error("Failed to show input box.", error); - } -} \ No newline at end of file diff --git a/src/ruleset/functions/createNewParam.ts b/src/ruleset/functions/createNewParam.ts deleted file mode 100644 index fc779b1..0000000 --- a/src/ruleset/functions/createNewParam.ts +++ /dev/null @@ -1,36 +0,0 @@ -import * as vscode from 'vscode'; -import * as yaml from 'js-yaml'; -import { JSONPath } from 'jsonpath-plus'; -import { nextTick } from 'process'; - -export default async function createNewParam(document: vscode.TextDocument, range: vscode.Range, given: string, field: string) { - const documentContent = document.getText(); - - const start = new vscode.Position(range.start.line, 0); - const end = new vscode.Position(range.end.line, document.lineAt(range.end.line).text.length); - const selection = new vscode.Selection(start, end); - const editor = vscode.window.activeTextEditor; - if (editor) { - editor.selections = [selection]; - } - const selectedText = document.getText(new vscode.Range(start, start)); - const lines = documentContent.split('\n'); - try { - const paramName = await vscode.window.showInputBox({ - prompt: 'Enter the name for the new parameter', - validateInput: (value: string) => { - if (!value) { - return 'Parameter name cannot be empty'; - } - return null; - } - }); - const newText = lines[range.start.line].replace(/{}/g, `{${paramName ? paramName : ''}}`); - console.log("selected text: ", selectedText); - console.log("newText: ", newText); - lines[range.start.line] = paramName ? newText : lines[range.start.line]; - return lines.join('\n'); - } catch (error) { - console.error("Failed to show input box.", error); - } -} \ No newline at end of file