From d5465f2adf0a7a605a4ae60ff03c28146b4303cc Mon Sep 17 00:00:00 2001
From: Bryan Mishkin <698306+bmish@users.noreply.github.com>
Date: Sun, 24 Oct 2021 12:23:27 -0400
Subject: [PATCH] fix: handle different rule file extensions like `.ts` in
`require-meta-docs-url` rule
---
lib/rules/require-meta-docs-url.js | 2 +-
tests/lib/rules/require-meta-docs-url.js | 70 ++++++++++++++++++++++++
2 files changed, 71 insertions(+), 1 deletion(-)
diff --git a/lib/rules/require-meta-docs-url.js b/lib/rules/require-meta-docs-url.js
index 6b692d3a..2c6565c9 100644
--- a/lib/rules/require-meta-docs-url.js
+++ b/lib/rules/require-meta-docs-url.js
@@ -49,7 +49,7 @@ module.exports = {
const options = context.options[0] || {};
const sourceCode = context.getSourceCode();
const filename = context.getFilename();
- const ruleName = filename === '' ? undefined : path.basename(filename, '.js');
+ const ruleName = filename === '' ? undefined : path.basename(filename, path.extname(filename));
const expectedUrl = !options.pattern || !ruleName
? undefined
: options.pattern.replace(/{{\s*name\s*}}/g, ruleName);
diff --git a/tests/lib/rules/require-meta-docs-url.js b/tests/lib/rules/require-meta-docs-url.js
index f7be9a8c..1dd472df 100644
--- a/tests/lib/rules/require-meta-docs-url.js
+++ b/tests/lib/rules/require-meta-docs-url.js
@@ -66,6 +66,17 @@ tester.run('require-meta-docs-url', rule, {
pattern: 'path/to/{{name}}.md',
}],
},
+ {
+ // CJS file extension
+ filename: 'test-rule.cjs',
+ code: `
+ module.exports = {
+ meta: {docs: {url: "path/to/test-rule.md"}},
+ create() {}
+ }
+ `,
+ options: [{ pattern: 'path/to/{{name}}.md' }],
+ },
{
// ESM
filename: 'test-rule',
@@ -80,6 +91,18 @@ tester.run('require-meta-docs-url', rule, {
}],
parserOptions: { sourceType: 'module' },
},
+ {
+ // TypeScript
+ filename: 'rules/test-rule.ts',
+ code: `
+ export default {
+ meta: {docs: {url: "path/to/test-rule.md"}},
+ create() {}
+ }
+ `,
+ options: [{ pattern: 'path/to/{{name}}.md' }],
+ parserOptions: { sourceType: 'module' },
+ },
{
// `url` in variable.
filename: 'test-rule',
@@ -542,6 +565,30 @@ tester.run('require-meta-docs-url', rule, {
docs: {
url: "plugin-name/test.md"
}
+},
+ create() {}
+ }
+ `,
+ options: [{
+ pattern: 'plugin-name/{{ name }}.md',
+ }],
+ errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
+ },
+ {
+ // CJS file extension
+ filename: 'test.cjs',
+ code: `
+ module.exports = {
+ meta: {},
+ create() {}
+ }
+ `,
+ output: `
+ module.exports = {
+ meta: {
+docs: {
+url: "plugin-name/test.md"
+}
},
create() {}
}
@@ -576,6 +623,29 @@ url: "plugin-name/test.md"
parserOptions: { sourceType: 'module' },
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
},
+ {
+ // TypeScript
+ filename: 'test.ts',
+ code: `
+ export default {
+ meta: {},
+ create() {}
+ }
+ `,
+ output: `
+ export default {
+ meta: {
+docs: {
+url: "plugin-name/test.md"
+}
+},
+ create() {}
+ }
+ `,
+ options: [{ pattern: 'plugin-name/{{ name }}.md' }],
+ parserOptions: { sourceType: 'module' },
+ errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
+ },
{
filename: 'test.js',
code: `