Skip to content

Commit

Permalink
fix: handle scoped plugin names
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Oct 2, 2022
1 parent 7322787 commit 06d137e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ export function getPluginPrefix(path: string): string {
"Could not find `name` field in ESLint plugin's package.json."
);
}
return pluginPackageJson.name.replace('eslint-plugin-', ''); // TODO: also need to support scoped plugins.
return pluginPackageJson.name.endsWith('/eslint-plugin')
? pluginPackageJson.name.split('/')[0] // Scoped plugin name like @my-scope/eslint-plugin.
: pluginPackageJson.name.replace('eslint-plugin-', ''); // Unscoped name like eslint-plugin-foo.
}
9 changes: 9 additions & 0 deletions test/lib/__snapshots__/generator-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ exports[`generator #generate Rule description needs to be formatted capitalizes
"
`;

exports[`generator #generate Scoped plugin name determines the correct plugin prefix 1`] = `
"# Disallow foo (\`@my-scope/no-foo\`)
✅ This rule is enabled in the \`recommended\` config.
<!-- end rule header -->
"
`;

exports[`generator #generate adds extra column to rules table for TypeScript rules updates the documentation 1`] = `
"<!-- begin rules list -->
Expand Down
42 changes: 42 additions & 0 deletions test/lib/generator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,5 +799,47 @@ describe('generator', function () {
await expect(generate('.')).rejects.toThrowErrorMatchingSnapshot();
});
});

describe('Scoped plugin name', function () {
beforeEach(function () {
mockFs({
'package.json': JSON.stringify({
name: '@my-scope/eslint-plugin',
main: 'index.js',
type: 'module',
}),

'index.js': `
export default {
rules: {
'no-foo': {
meta: { docs: { description: 'disallow foo.' }, },
create(context) {}
},
},
configs: {
'recommended': { rules: { '@my-scope/no-foo': 'error', } }
}
};`,

'README.md': '<!-- begin rules list --><!-- end rules list -->',

'docs/rules/no-foo.md': '',

// Needed for some of the test infrastructure to work.
node_modules: mockFs.load(
resolve(__dirname, '..', '..', 'node_modules')
),
});
});
afterEach(function () {
mockFs.restore();
jest.resetModules();
});
it('determines the correct plugin prefix', async function () {
await generate('.');
expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot();
});
});
});
});

0 comments on commit 06d137e

Please sign in to comment.