Skip to content

Commit

Permalink
chore: use no-restricted-syntax to enforce created options in rules (#…
Browse files Browse the repository at this point in the history
…6074)

* fix(eslint-plugin): [keyword-spacing] prevent crash on no options

* chore: add internal lint rule to always prefer created options

* All right base rules, you do you
  • Loading branch information
JoshuaKGoldberg committed Nov 24, 2022
1 parent 1302b30 commit ee62b0b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.js
Expand Up @@ -264,6 +264,16 @@ module.exports = {

// specifically for rules - default exports makes the tooling easier
'import/no-default-export': 'off',

'no-restricted-syntax': [
'error',
{
selector:
'ExportDefaultDeclaration Property[key.name="create"] MemberExpression[object.name="context"][property.name="options"]',
message:
"Retrieve options from create's second parameter so that defaultOptions are applied.",
},
],
},
},
// plugin rule tests
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/brace-style.ts
Expand Up @@ -29,6 +29,7 @@ export default createRule<Options, MessageIds>({
defaultOptions: ['1tbs'],
create(context) {
const [style, { allowSingleLine } = { allowSingleLine: false }] =
// eslint-disable-next-line no-restricted-syntax -- Use raw options for extended rules.
context.options;

const isAllmanStyle = style === 'allman';
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Expand Up @@ -84,7 +84,7 @@ export default util.createRule<Options, MessageIds>({
},
},
defaultOptions: [{}],
create(context) {
create(context, [firstOption]) {
const filename = context.getFilename();
const sourceCode = context.getSourceCode();
const MODULE_DECL_CACHE = new Map<TSESTree.TSModuleDeclaration, boolean>();
Expand All @@ -97,8 +97,6 @@ export default util.createRule<Options, MessageIds>({
caughtErrors: 'none',
};

const [firstOption] = context.options;

if (firstOption) {
if (typeof firstOption === 'string') {
options.vars = firstOption;
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/object-curly-spacing.ts
Expand Up @@ -31,6 +31,7 @@ export default createRule<Options, MessageIds>({
},
defaultOptions: ['never'],
create(context) {
// eslint-disable-next-line no-restricted-syntax -- Use raw options for extended rules.
const [firstOption, secondOption] = context.options;
const spaced = firstOption === 'always';
const sourceCode = context.getSourceCode();
Expand Down
Expand Up @@ -634,6 +634,7 @@ export default util.createRule<Options, MessageIds>({
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode();
// eslint-disable-next-line no-restricted-syntax -- We need all raw options.
const configureList = context.options || [];

type Scope = null | {
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/space-before-blocks.ts
Expand Up @@ -29,9 +29,8 @@ export default util.createRule<Options, MessageIds>({
},
},
defaultOptions: ['always'],
create(context) {
create(context, [config]) {
const rules = baseRule.create(context);
const config = context.options[0];
const sourceCode = context.getSourceCode();

let requireSpace = true;
Expand Down

0 comments on commit ee62b0b

Please sign in to comment.