eslint-plugin-budapestian
Advanced tools
Comparing version 1.0.0 to 1.1.0
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 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
15213
8
234
68
0
3
1
+ Addeddecamelize@3.2.0
+ Added@babel/runtime-corejs3@7.26.0(transitive)
+ Addedcore-js-pure@3.39.0(transitive)
+ Addeddecamelize@3.2.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
+ Addedxregexp@4.4.1(transitive)