Skip to content

Commit 725f1a1

Browse files
committed
bring in filenames to github rules
1 parent 7520260 commit 725f1a1

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

lib/rules/filenames-match-regex.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const path = require('path')
2+
const parseFilename = require('../utils/parse-filename')
3+
const getExportedName = require('../utils/get-exported-name')
4+
const isIgnoredFilename = require('../utils/is-ignored-filename')
5+
6+
module.exports = {
7+
meta: {
8+
type: 'problem',
9+
docs: {
10+
description: 'ensure filenames match a regex naming convention',
11+
url: require('../url')(module),
12+
},
13+
schema: {
14+
type: 'array',
15+
minItems: 1,
16+
maxItems: 2,
17+
items: [
18+
{
19+
type: 'string',
20+
},
21+
],
22+
},
23+
},
24+
25+
create(context) {
26+
const defaultRegexp = /^([a-z0-9]+)([A-Z][a-z0-9]+)*$/g
27+
const conventionRegexp = context.options[0] ? new RegExp(context.options[0]) : defaultRegexp
28+
const ignoreExporting = context.options[1] ? context.options[1] : false
29+
30+
return {
31+
Program(node) {
32+
const filename = context.getFilename()
33+
const absoluteFilename = path.resolve(filename)
34+
const parsed = parseFilename(absoluteFilename)
35+
const shouldIgnore = isIgnoredFilename(filename)
36+
const isExporting = Boolean(getExportedName(node))
37+
const matchesRegex = conventionRegexp.test(parsed.name)
38+
39+
if (shouldIgnore) return
40+
if (ignoreExporting && isExporting) return
41+
if (!matchesRegex) {
42+
context.report(node, "Filename '{{name}}' does not match the regex naming convention.", {
43+
name: parsed.base,
44+
})
45+
}
46+
},
47+
}
48+
},
49+
}

lib/utils/get-exported-name.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function getNodeName(node, options) {
2+
const op = options || []
3+
4+
if (node.type === 'Identifier') {
5+
return node.name
6+
}
7+
8+
if (node.id && node.id.type === 'Identifier') {
9+
return node.id.name
10+
}
11+
12+
if (op[2] && node.type === 'CallExpression' && node.callee.type === 'Identifier') {
13+
return node.callee.name
14+
}
15+
}
16+
17+
module.exports = function getExportedName(programNode, options) {
18+
for (let i = 0; i < programNode.body.length; i += 1) {
19+
const node = programNode.body[i]
20+
21+
if (node.type === 'ExportDefaultDeclaration') {
22+
return getNodeName(node.declaration, options)
23+
}
24+
25+
if (
26+
node.type === 'ExpressionStatement' &&
27+
node.expression.type === 'AssignmentExpression' &&
28+
node.expression.left.type === 'MemberExpression' &&
29+
node.expression.left.object.type === 'Identifier' &&
30+
node.expression.left.object.name === 'module' &&
31+
node.expression.left.property.type === 'Identifier' &&
32+
node.expression.left.property.name === 'exports'
33+
) {
34+
return getNodeName(node.expression.right, options)
35+
}
36+
}
37+
}

lib/utils/is-ignored-filename.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const ignoredFilenames = ['<text>', '<input>']
2+
3+
module.exports = function isIgnoredFilename(filename) {
4+
return ignoredFilenames.indexOf(filename) !== -1
5+
}

lib/utils/parse-filename.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const path = require('path')
2+
3+
module.exports = function parseFilename(filename) {
4+
const ext = path.extname(filename)
5+
6+
return {
7+
dir: path.dirname(filename),
8+
base: path.basename(filename),
9+
ext,
10+
name: path.basename(filename, ext),
11+
}
12+
}

0 commit comments

Comments
 (0)