Skip to content

Commit

Permalink
add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
yp05327 committed Jun 25, 2024
1 parent ea9fcde commit 59b10bb
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
// Extension: ESLint
"eslint.options": {
"rulePaths": [
"eslint-internal-rules"
]
},
}
14 changes: 14 additions & 0 deletions eslint-internal-rules/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
rules: {
'valid-appcardcode-code-prop': 'error',
'valid-appcardcode-demo-sfc': 'error',
},
overrides: [
{
files: ['*.json'],
rules: {
'no-invalid-meta': 'off',
},
},
],
}
3 changes: 3 additions & 0 deletions eslint-internal-rules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
53 changes: 53 additions & 0 deletions eslint-internal-rules/valid-appcardcode-code-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

// eslint-disable-next-line @typescript-eslint/no-var-requires
const utils = require('eslint-plugin-vue/lib/utils')

function toCamelCase(str) {
return str.toLowerCase().replace(/[^a-z\d]+(.)/gi, (m, chr) => chr.toUpperCase())
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require valid prop for "AppCardCode" component',
categories: ['base'],
url: 'https://eslint.vuejs.org/rules/require-component-is.html',
},
fixable: null,
schema: [],
},

/** @param {RuleContext} context */
create(context) {
return utils.defineTemplateBodyVisitor(context, {
/** @param {VElement} node */
'VElement[name=\'appcardcode\']': function (node) {
const propTitle = utils.getAttribute(node, 'title')
const propCode = utils.getDirective(node, 'bind', 'code')

const titleValue = propTitle.value.value
const demoCodeProperty = propCode.value.expression.property.name

const camelCasedTitle = toCamelCase(titleValue)

if (camelCasedTitle !== demoCodeProperty) {
context.report({
node,
loc: propCode.value.expression.property.loc,
message: `Expected 'code' prop value to match the camelcase value of title prop value. Expected: '${camelCasedTitle}', Actual: '${demoCodeProperty}'`,
})
}
},
})
},
}
82 changes: 82 additions & 0 deletions eslint-internal-rules/valid-appcardcode-demo-sfc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

// eslint-disable-next-line @typescript-eslint/no-var-requires
const utils = require('eslint-plugin-vue/lib/utils')

function toPascalCase(str) {
const words = str.match(/[a-z]+/gi)
if (!words)
return ''

return words
.map(word => {
return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()
})
.join('')
}

function findDemoElement(node) {
let el = null
node.children.forEach(child => {
if (child.children && child.children.length) {
const r = findDemoElement(child)
if (r)
el = r
}

else {
const r = child.type === 'VElement' && child.name.startsWith('demo')
if (r)
el = child
}
})

return el
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require valid demo SFC for "AppCardCode" component',
categories: ['base'],
url: 'https://eslint.vuejs.org/rules/require-component-is.html',
},
fixable: null,
schema: [],
},

/** @param {RuleContext} context */
create(context) {
return utils.defineTemplateBodyVisitor(context, {
/** @param {VElement} node */
'VElement[name=\'appcardcode\']': function (node) {
const propTitle = utils.getAttribute(node, 'title')
const titleValue = propTitle.value.value

const pascalCaseTitle = toPascalCase(titleValue)

const demoNode = findDemoElement(node)
const demoNodeSfcName = demoNode.rawName
const pattern = new RegExp(`Demo[a-zA-z]+${pascalCaseTitle}`)
const demoSfcMatch = demoNodeSfcName.search(pattern)

if (demoSfcMatch !== 0) {
context.report({
node,
loc: demoNode.loc,
message: `Expected Demo SFC to match the pascal case value of title prop value. Expected: 'Demo[a-zA-Z]+${pascalCaseTitle}', Actual: '${demoNodeSfcName}'`,
})
}
},
})
},
}

0 comments on commit 59b10bb

Please sign in to comment.