Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle different rule file extensions like .ts in require-meta-docs-url rule #224

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/require-meta-docs-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = {
const options = context.options[0] || {};
const sourceCode = context.getSourceCode();
const filename = context.getFilename();
const ruleName = filename === '<input>' ? undefined : path.basename(filename, '.js');
const ruleName = filename === '<input>' ? undefined : path.basename(filename, path.extname(filename));
const expectedUrl = !options.pattern || !ruleName
? undefined
: options.pattern.replace(/{{\s*name\s*}}/g, ruleName);
Expand Down
70 changes: 70 additions & 0 deletions tests/lib/rules/require-meta-docs-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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() {}
}
Expand Down Expand Up @@ -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: `
Expand Down