Skip to content

fix/issue-26 #27

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
79 changes: 7 additions & 72 deletions lib/rules/commands.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,8 @@
import {manifest} from "../readManifest";
// This file has been split into separate rules in the commands/ directory:
// - commandInId.ts
// - commandInName.ts
// - hotkeys.ts
// - pluginId.ts
// - pluginName.ts

export = {
name: 'commands',
meta: {
docs: {
description: 'Command guidelines',
url: 'https://docs.obsidian.md/Plugins/Releasing/Plugin+guidelines#Commands'
},
type: 'problem',
messages: {
hotkeys: 'We recommend against providing a default hotkey when possible.',
commandInId: 'Adding `command` to the command ID is not necessary.',
commandInName: 'Adding `command` to the command name is not necessary.',
pluginName: 'The command name should not include the plugin name.',
pluginId: 'The command ID should not include the plugin ID.'
},
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (node.callee.type === 'MemberExpression' &&
node.callee.property.name === 'addCommand' &&
node.arguments.length > 0 &&
node.arguments[0].type === 'ObjectExpression') {

const argument = node.arguments[0];

argument.properties.forEach(property => {
if (property.key.type === 'Identifier') {
if (property.key.name === 'id' && property.value.type === 'Literal') {
if (property.value.value.toLowerCase().includes('command')) {
context.report({
node: property,
messageId: 'commandInId'
});
}
if (property.value.value.includes(manifest.id)) {
context.report({
node: property,
messageId: 'pluginId'
})
}
}
if (property.key.name === 'name' && property.value.type === 'Literal') {
if (property.value.value.toLowerCase().includes('command')) {
context.report({
node: property,
messageId: 'commandInName'
});
}
if (property.value.value.toLowerCase().includes(manifest.name.toLowerCase())) {
context.report({
node: property,
messageId: 'pluginName'
})
}
}
if (property.key.name === 'hotkeys') {
context.report({
node: property,
messageId: 'hotkeys'
});
}
}
});
}
},
};
}
};
export = {};
50 changes: 50 additions & 0 deletions lib/rules/commands/commandInId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { manifest } from "../../readManifest";

export = {
name: "commandInId",
meta: {
docs: {
description: "Discourage adding `command` to the command ID.",
url: "https://docs.obsidian.md/Plugins/Releasing/Plugin+guidelines#Commands",
},
type: "problem",
messages: {
commandInId: "Adding `command` to the command ID is not necessary.",
},
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.property.name === "addCommand" &&
node.arguments.length > 0 &&
node.arguments[0].type === "ObjectExpression"
) {
const argument = node.arguments[0];

argument.properties.forEach((property) => {
if (
property.key.type === "Identifier" &&
property.key.name === "id" &&
property.value.type === "Literal"
) {
if (
property.value.value
.toLowerCase()
.includes("command")
) {
context.report({
node: property,
messageId: "commandInId",
});
}
}
});
}
},
};
},
};
51 changes: 51 additions & 0 deletions lib/rules/commands/commandInName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { manifest } from "../../readManifest";

export = {
name: "commandInName",
meta: {
docs: {
description: "Discourage adding `command` to the command name.",
url: "https://docs.obsidian.md/Plugins/Releasing/Plugin+guidelines#Commands",
},
type: "problem",
messages: {
commandInName:
"Adding `command` to the command name is not necessary.",
},
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.property.name === "addCommand" &&
node.arguments.length > 0 &&
node.arguments[0].type === "ObjectExpression"
) {
const argument = node.arguments[0];

argument.properties.forEach((property) => {
if (
property.key.type === "Identifier" &&
property.key.name === "name" &&
property.value.type === "Literal"
) {
if (
property.value.value
.toLowerCase()
.includes("command")
) {
context.report({
node: property,
messageId: "commandInName",
});
}
}
});
}
},
};
},
};
44 changes: 44 additions & 0 deletions lib/rules/commands/hotkeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { manifest } from "../../readManifest";

export = {
name: "hotkeys",
meta: {
docs: {
description: "Discourage providing default hotkeys.",
url: "https://docs.obsidian.md/Plugins/Releasing/Plugin+guidelines#Commands",
},
type: "problem",
messages: {
hotkeys:
"We recommend against providing a default hotkey when possible.",
},
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.property.name === "addCommand" &&
node.arguments.length > 0 &&
node.arguments[0].type === "ObjectExpression"
) {
const argument = node.arguments[0];

argument.properties.forEach((property) => {
if (
property.key.type === "Identifier" &&
property.key.name === "hotkeys"
) {
context.report({
node: property,
messageId: "hotkeys",
});
}
});
}
},
};
},
};
47 changes: 47 additions & 0 deletions lib/rules/commands/pluginId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { manifest } from "../../readManifest";

export = {
name: "pluginId",
meta: {
docs: {
description:
"Discourage including the plugin ID in the command ID.",
url: "https://docs.obsidian.md/Plugins/Releasing/Plugin+guidelines#Commands",
},
type: "problem",
messages: {
pluginId: "The command ID should not include the plugin ID.",
},
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.property.name === "addCommand" &&
node.arguments.length > 0 &&
node.arguments[0].type === "ObjectExpression"
) {
const argument = node.arguments[0];

argument.properties.forEach((property) => {
if (
property.key.type === "Identifier" &&
property.key.name === "id" &&
property.value.type === "Literal"
) {
if (property.value.value.includes(manifest.id)) {
context.report({
node: property,
messageId: "pluginId",
});
}
}
});
}
},
};
},
};
51 changes: 51 additions & 0 deletions lib/rules/commands/pluginName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { manifest } from "../../readManifest";

export = {
name: "pluginName",
meta: {
docs: {
description:
"Discourage including the plugin name in the command name.",
url: "https://docs.obsidian.md/Plugins/Releasing/Plugin+guidelines#Commands",
},
type: "problem",
messages: {
pluginName: "The command name should not include the plugin name.",
},
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.property.name === "addCommand" &&
node.arguments.length > 0 &&
node.arguments[0].type === "ObjectExpression"
) {
const argument = node.arguments[0];

argument.properties.forEach((property) => {
if (
property.key.type === "Identifier" &&
property.key.name === "name" &&
property.value.type === "Literal"
) {
if (
property.value.value
.toLowerCase()
.includes(manifest.name.toLowerCase())
) {
context.report({
node: property,
messageId: "pluginName",
});
}
}
});
}
},
};
},
};
Loading