Skip to content

Commit

Permalink
fix: handle different rule file extensions like .ts in `require-met…
Browse files Browse the repository at this point in the history
…a-docs-url` rule (#224)
  • Loading branch information
bmish committed Oct 25, 2021
1 parent 0fc8c45 commit 6a09dbe
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
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

0 comments on commit 6a09dbe

Please sign in to comment.