-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from sj-distributor/4-add-new-rule
Add new rule
- Loading branch information
Showing
6 changed files
with
98 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @fileoverview Require interface names to begin with a specified prefix | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
category: "Naming", | ||
recommended: false, | ||
description: "Require interface names to begin with a specified prefix", | ||
}, | ||
fixable: "code", | ||
schema: [ | ||
{ | ||
type: "string", | ||
}, | ||
], | ||
messages: { | ||
missingPrefix: | ||
"Interface name '{{ name }}' should start with '{{ prefix }}'", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const [prefix = "I"] = context.options; | ||
|
||
function checkPrefix(node) { | ||
const name = node.id.name; | ||
|
||
if ( | ||
typeof name !== "string" || | ||
name.startsWith(prefix) || | ||
node.parent.type === "TSModuleDeclaration" | ||
) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: "missingPrefix", | ||
data: { | ||
name, | ||
prefix, | ||
}, | ||
fix: (fixer) => { | ||
const newName = `${prefix}${name}`; | ||
|
||
return fixer.replaceText(node.id, newName); | ||
}, | ||
}); | ||
} | ||
|
||
return { | ||
TSInterfaceDeclaration: checkPrefix, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters