Permalink
Cannot retrieve contributors at this time
57 lines (51 sloc)
1.61 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
codeql-action/node_modules/eslint-plugin-github/lib/rules/js-class-name.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
module.exports = { | |
meta: { | |
type: 'suggestion', | |
docs: { | |
description: 'enforce a naming convention for js- prefixed classes', | |
url: require('../url')(module), | |
}, | |
schema: [], | |
}, | |
create(context) { | |
const allJsClassNameRegexp = /\bjs-[_a-zA-Z0-9-]*/g | |
const validJsClassNameRegexp = /^js(-[a-z0-9]+)+$/g | |
const endWithJsClassNameRegexp = /\bjs-[_a-zA-Z0-9-]*$/g | |
function checkStringFormat(node, str) { | |
const matches = str.match(allJsClassNameRegexp) || [] | |
for (const match of matches) { | |
if (!match.match(validJsClassNameRegexp)) { | |
context.report({node, message: 'js- class names should be lowercase and only contain dashes.'}) | |
} | |
} | |
} | |
function checkStringEndsWithJSClassName(node, str) { | |
if (str.match(endWithJsClassNameRegexp)) { | |
context.report({node, message: 'js- class names should be statically defined.'}) | |
} | |
} | |
return { | |
Literal(node) { | |
if (typeof node.value === 'string') { | |
checkStringFormat(node, node.value) | |
if ( | |
node.parent && | |
node.parent.type === 'BinaryExpression' && | |
node.parent.operator === '+' && | |
node.parent.left.value | |
) { | |
checkStringEndsWithJSClassName(node.parent.left, node.parent.left.value) | |
} | |
} | |
}, | |
TemplateLiteral(node) { | |
for (const quasi of node.quasis) { | |
checkStringFormat(quasi, quasi.value.raw) | |
if (quasi.tail === false) { | |
checkStringEndsWithJSClassName(quasi, quasi.value.raw) | |
} | |
} | |
}, | |
} | |
}, | |
} |