Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-budapestian

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-budapestian - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

lib/rules/global-constant-pattern.js

97

lib/rules/parameter-pattern.js
const _get = require("lodash.get");
const {
getValidParameterPattern,
getIdentifierReplacementPattern
} = require("./pattern-utl");
/**

@@ -7,2 +11,17 @@ * @fileoverview Enforce function parameters to adhere to a pattern

function getParameterName(pParam) {
return _get(pParam, "name", _get(pParam, "left.name", "pOK"));
}
function normalizeParameterName(pString) {
return `p${pString
.slice(0, 1)
.toLocaleUpperCase()
.concat(pString.slice(1))}`;
}
function parameterNameIsValid(pString) {
return pString.match(getValidParameterPattern());
}
//------------------------------------------------------------------------------

@@ -12,2 +31,19 @@ // Rule Definition

function getFixes(pContext, pNode, pProblematicParameterNames, pFire) {
if (pFire) {
return pFixer => {
let lBetterized = pProblematicParameterNames.reduce(
(pSource, pProblematicParameterName) =>
pSource.replace(
getIdentifierReplacementPattern(pProblematicParameterName),
`$1${normalizeParameterName(pProblematicParameterName)}$2`
),
pContext.getSourceCode().getText(pNode)
);
return pFixer.replaceText(pNode, lBetterized);
};
}
}
module.exports = {

@@ -23,54 +59,25 @@ meta: {

},
fixable: "code", // or "code" or "whitespace"
schema: [
// fill in your schema
]
fixable: "code"
},
create: pContext => {
const PARAMETER_PATTERN = /^(p[A-Z]|_)\S*/;
function checkParameters(pNode, pContext) {
const lProblematicParameterNames = _get(pNode, "params", [])
.map(getParameterName)
.filter(pParameterName => !parameterNameIsValid(pParameterName));
//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------
function getParameterName(pParam) {
return _get(pParam, "name", _get(pParam, "left.name", "pOK"));
}
lProblematicParameterNames.forEach((pProblematicParameterName, pIndex, pAllProblematicParameterNames) => {
pContext.report({
node: pNode,
message: `parameter '{{ identifier }}' should be pascal case and start with a p: '{{ betterIdentifier }}'`,
data: {
identifier: pProblematicParameterName,
betterIdentifier: normalizeParameterName(pProblematicParameterName)
},
fix: getFixes(pContext, pNode, pAllProblematicParameterNames, pIndex === 0)
});
})
function normalizeParameterName(pString) {
return `p${pString
.slice(0, 1)
.toLocaleUpperCase()
.concat(pString.slice(1))}`;
}
function checkParameters(pNode, pContext) {
_get(pNode, "params", []).forEach(pParam => {
const lParameterName = getParameterName(pParam);
if (!lParameterName.match(PARAMETER_PATTERN)) {
pContext.report({
node: pNode,
message: `parameter '{{ identifier }}' should be pascal case and start with a p: '{{ betterIdentifier }}'`,
data: {
identifier: lParameterName,
betterIdentifier: normalizeParameterName(lParameterName)
},
fix: pFixer => {
const lBetterized = pContext
.getSourceCode()
.getText(pNode)
.replace(
// TODO: doesn't work really well with non-ascii
// maybe use the power of the AST(tm) here as well
// instead of re-hacking
new RegExp(`(\\W|^)${lParameterName}(\\W|$)`, "g"),
`$1${normalizeParameterName(lParameterName)}$2`
);
return pFixer.replaceText(pNode, lBetterized);
}
});
}
});
}
//----------------------------------------------------------------------

@@ -77,0 +84,0 @@ // Public

{
"name": "eslint-plugin-budapestian",
"version": "1.0.0",
"version": "1.1.0",
"description": "enforce budapestian style rules",

@@ -16,3 +16,8 @@ "keywords": [

"homepage": "https://github.com/sverweij/eslint-plugin-budapestian",
"repository": {
"type": "git",
"url": "git+https://github.com/sverweij/dependency-cruiser"
},
"scripts": {
"check": "npm-run-all lint test:cover",
"depcruise:graph": "depcruise --config .dependency-cruiser.js --output-type dot lib test | dot -T svg | tee docs/dependency-graph.svg | depcruise-wrap-stream-in-html > docs/dependency-graph.html",

@@ -26,2 +31,3 @@ "lint": "npm-run-all lint:eslint lint:prettier lint:dependency-cruiser",

"lint:prettier:fix": "prettier --loglevel warn --write {src,test}/\\*\\*/\\*.{js,json} *.js *.json .github/\\*\\*/\\*",
"scm:stage": "git add .",
"test": "mocha test --recursive",

@@ -31,5 +37,7 @@ "test:cover": "nyc npm test",

"upem:install": "npm install",
"upem:update": "npm outdated --json | upem"
"upem:update": "npm outdated --json | upem",
"version": "npm-run-all check depcruise:graph scm:stage"
},
"dependencies": {
"decamelize": "3.2.0",
"lodash.get": "4.4.2"

@@ -56,3 +64,7 @@ },

"package": "eslint-plugin-unicorn",
"because": "eslint-plugin-unicorn 16 doesn't support node 8 anymore, while state-machine-cat still does."
"because": "eslint-plugin-unicorn 16 doesn't support node 8 anymore, while eslint-plugin-budapestian still does."
},
{
"package": "decamelize",
"because": "decamelize 4 doesn't support node 8 anymore, while eslint-plugin-budapestian still does."
}

@@ -70,3 +82,4 @@ ]

"tmp*",
"docs/**/*"
"docs/**/*",
"test/**/*"
],

@@ -73,0 +86,0 @@ "reporter": [

@@ -9,6 +9,7 @@ # eslint-plugin-budapestian

- `p` for parameters (supported by this plugin),
- For global constants we use the C convention of ALL_CAPS_SNAKE_CASE (supported by this plugin).
- `l` for local variables (not yet supported)
- `g` for global variables (not yet supported).
- For global constants we use the C convention of ALL_CAPS_SNAKE_CASE (not yet supported).
This convention makes weird re-assignment bugs immediately visible, and makes naming things

@@ -50,3 +51,4 @@ that would normally clash with regular javascript syntax a easier. E.g. you can't use

"rules": {
"budapestian/parameter-pattern": "error"
"budapestian/parameter-pattern": "error",
"budapestian/global-constant-pattern": "error"
}

@@ -58,5 +60,6 @@ }

| fixable? | rule | description |
| -------- | ---------------------------------------------------------------- | -------------------------------------------------------------- |
| yes | [budapestian/parameter-pattern](docs/rules/parameter-pattern.md) | pascal case function parameters and make them start with a `p` |
| auto fixable? | rule | description |
| ------------- | ----------------------------------------------------------------------- | -------------------------------------------------------------- |
| yes | [budapestian/parameter-pattern](docs/rules/parameter-pattern.md) | pascal case function parameters and make them start with a `p` |
| yes | [budapestian/global-constant-pattern](rules/global-constant-pattern.md) | makes sure global constants are in snaked upper case. |

@@ -63,0 +66,0 @@ ## Flare'n status section

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc