mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-06 21:42:49 +00:00
feat: update tests
feat: update tests feat: update tests
This commit is contained in:
115
configuration/eslint-config/copyright-header-rule.js
Normal file
115
configuration/eslint-config/copyright-header-rule.js
Normal file
@@ -0,0 +1,115 @@
|
||||
// @ts-check
|
||||
const fs = require("fs");
|
||||
const path = require("node:path");
|
||||
const child_process = require("child_process");
|
||||
|
||||
/** @type {import('eslint').Rule.RuleModule} */
|
||||
const copyrightHeaderRule = {
|
||||
meta: {
|
||||
schema: [{
|
||||
type: 'object',
|
||||
properties: {
|
||||
author: {type: 'string'},
|
||||
license: {type: 'string'},
|
||||
fixedDate: {type: 'string'},
|
||||
},
|
||||
required: ['author', 'license']
|
||||
}],
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description: 'Files should start with a copyright header'
|
||||
},
|
||||
messages: {
|
||||
missing: 'Missing a copyright header at the start of the file',
|
||||
invalid: 'Invalid copyright header',
|
||||
tryThisHeader: 'Use this copyright header:\n\n/*{{expected}}*/'
|
||||
},
|
||||
fixable: "code",
|
||||
hasSuggestions: true,
|
||||
},
|
||||
create: function(context) {
|
||||
const code = context.getSourceCode()
|
||||
const {author, license, fixedDate} = context.options[0]
|
||||
|
||||
const year = fixedDate ? new Date(fixedDate) : getAuthorDate(context.getPhysicalFilename())
|
||||
if (!year) return {}
|
||||
|
||||
const expected = license
|
||||
.replace('{{year}}', year.getFullYear().toString())
|
||||
.replace('{{author}}', author)
|
||||
|
||||
const comment = code.getAllComments().find(it => it.type === 'Block')
|
||||
if (!comment) {
|
||||
context.report({
|
||||
loc: {
|
||||
line: 0,
|
||||
column: 0,
|
||||
},
|
||||
messageId: 'missing',
|
||||
suggest: [
|
||||
{
|
||||
messageId: 'tryThisHeader',
|
||||
data: {expected},
|
||||
fix(fixer) {
|
||||
return fixer.insertTextBeforeRange([0, 0], `/*${expected}*/\n`)
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
return {}
|
||||
}
|
||||
|
||||
if (comment.value !== expected && comment.loc && comment.range) {
|
||||
const range = comment.range
|
||||
context.report({
|
||||
loc: comment.loc,
|
||||
messageId: 'invalid',
|
||||
suggest: [
|
||||
{
|
||||
messageId: 'tryThisHeader',
|
||||
data: {expected},
|
||||
fix(fixer) {
|
||||
return fixer.replaceTextRange(range, `/*${expected}*/`)
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the last edited date of a file
|
||||
*
|
||||
* Uses git history if available, last modified date otherwise.
|
||||
*
|
||||
* @param filePath {string}
|
||||
* @return {Date | undefined}
|
||||
*/
|
||||
function getAuthorDate(filePath) {
|
||||
// just to be on the safe side
|
||||
const sanitizedPath = path.resolve(filePath)
|
||||
|
||||
try {
|
||||
const result = child_process.execSync(`git log -1 --pretty="format:%ad" "${sanitizedPath}"`, {
|
||||
cwd: path.dirname(sanitizedPath),
|
||||
stdio: 'pipe',
|
||||
})
|
||||
const date = new Date(result.toString())
|
||||
if (!Number.isNaN(date.getTime())) {
|
||||
return date
|
||||
}
|
||||
} catch {}
|
||||
|
||||
try {
|
||||
const stats = fs.statSync(sanitizedPath)
|
||||
const date = new Date(stats.mtime)
|
||||
return Number.isNaN(date.getTime()) ? undefined : date
|
||||
} catch {}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
module.exports = copyrightHeaderRule
|
||||
58
configuration/eslint-config/copyright-header-rule.spec.js
Normal file
58
configuration/eslint-config/copyright-header-rule.spec.js
Normal file
@@ -0,0 +1,58 @@
|
||||
// @ts-check
|
||||
"use strict"
|
||||
|
||||
const rule = require('./copyright-header-rule')
|
||||
|
||||
const RuleTester = require('eslint').RuleTester
|
||||
const ruleTester = new RuleTester()
|
||||
|
||||
const options = [{
|
||||
fixedDate: '2023',
|
||||
author: 'Me',
|
||||
license: `*\n * Copyright {{year}} {{author}}\n *\n * contents\n `,
|
||||
}]
|
||||
|
||||
ruleTester.run('copyright-header', rule, {
|
||||
valid: [
|
||||
{
|
||||
code: `/**\n * Copyright 2023 Me\n *\n * contents\n */`,
|
||||
options,
|
||||
},
|
||||
{
|
||||
code: `// @ts-check\n\n\n/**\n * Copyright 2023 Me\n *\n * contents\n */`,
|
||||
options,
|
||||
}
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
code: `// bla\n/**\n * Copyright 2022 Me\n *\n * contents\n */\n\nvar s = "abc"`,
|
||||
options,
|
||||
errors: [
|
||||
{
|
||||
messageId: 'invalid',
|
||||
suggestions: [
|
||||
{
|
||||
messageId: 'tryThisHeader',
|
||||
output: '// bla\n/**\n * Copyright 2023 Me\n *\n * contents\n */\n\nvar s = "abc"'
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
code: 'var s = "abc"',
|
||||
options,
|
||||
errors: [
|
||||
{
|
||||
messageId: 'missing',
|
||||
suggestions: [
|
||||
{
|
||||
messageId: 'tryThisHeader',
|
||||
output: '/**\n * Copyright 2023 Me\n *\n * contents\n */\nvar s = "abc"'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
@@ -1,4 +1,7 @@
|
||||
module.exports = {
|
||||
// @ts-check
|
||||
|
||||
/** @type {import('eslint').Linter.Config} */
|
||||
const config = {
|
||||
root: true,
|
||||
ignorePatterns: ['projects/**/*'],
|
||||
parserOptions: {
|
||||
@@ -93,3 +96,5 @@ module.exports = {
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
module.exports = config
|
||||
|
||||
14
configuration/eslint-config/licenses/GPL-3.0-only.txt
Normal file
14
configuration/eslint-config/licenses/GPL-3.0-only.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
Copyright (C) {{year}} {{author}}
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
@@ -10,6 +10,12 @@
|
||||
"Rainer Killinger <mail-openstapps@killinger.co>"
|
||||
],
|
||||
"main": "index.js",
|
||||
"devDependencies": {
|
||||
"@openstapps/tsconfig": "workspace:*",
|
||||
"@types/node": "18.15.3",
|
||||
"eslint": "8.33.0",
|
||||
"typescript": "4.6.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "5.49.0",
|
||||
"@typescript-eslint/parser": "5.49.0",
|
||||
|
||||
8
configuration/eslint-config/tsconfig.json
Normal file
8
configuration/eslint-config/tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "@openstapps/tsconfig",
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"types": ["node"]
|
||||
},
|
||||
"include": ["*.js"]
|
||||
}
|
||||
Reference in New Issue
Block a user