Skip to content

Commit ee62b0b

Browse files
chore: use no-restricted-syntax to enforce created options in rules (typescript-eslint#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
1 parent 1302b30 commit ee62b0b

File tree

6 files changed

+15
-5
lines changed

6 files changed

+15
-5
lines changed

.eslintrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ module.exports = {
264264

265265
// specifically for rules - default exports makes the tooling easier
266266
'import/no-default-export': 'off',
267+
268+
'no-restricted-syntax': [
269+
'error',
270+
{
271+
selector:
272+
'ExportDefaultDeclaration Property[key.name="create"] MemberExpression[object.name="context"][property.name="options"]',
273+
message:
274+
"Retrieve options from create's second parameter so that defaultOptions are applied.",
275+
},
276+
],
267277
},
268278
},
269279
// plugin rule tests

packages/eslint-plugin/src/rules/brace-style.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default createRule<Options, MessageIds>({
2929
defaultOptions: ['1tbs'],
3030
create(context) {
3131
const [style, { allowSingleLine } = { allowSingleLine: false }] =
32+
// eslint-disable-next-line no-restricted-syntax -- Use raw options for extended rules.
3233
context.options;
3334

3435
const isAllmanStyle = style === 'allman';

packages/eslint-plugin/src/rules/no-unused-vars.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default util.createRule<Options, MessageIds>({
8484
},
8585
},
8686
defaultOptions: [{}],
87-
create(context) {
87+
create(context, [firstOption]) {
8888
const filename = context.getFilename();
8989
const sourceCode = context.getSourceCode();
9090
const MODULE_DECL_CACHE = new Map<TSESTree.TSModuleDeclaration, boolean>();
@@ -97,8 +97,6 @@ export default util.createRule<Options, MessageIds>({
9797
caughtErrors: 'none',
9898
};
9999

100-
const [firstOption] = context.options;
101-
102100
if (firstOption) {
103101
if (typeof firstOption === 'string') {
104102
options.vars = firstOption;

packages/eslint-plugin/src/rules/object-curly-spacing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default createRule<Options, MessageIds>({
3131
},
3232
defaultOptions: ['never'],
3333
create(context) {
34+
// eslint-disable-next-line no-restricted-syntax -- Use raw options for extended rules.
3435
const [firstOption, secondOption] = context.options;
3536
const spaced = firstOption === 'always';
3637
const sourceCode = context.getSourceCode();

packages/eslint-plugin/src/rules/padding-line-between-statements.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ export default util.createRule<Options, MessageIds>({
634634
defaultOptions: [],
635635
create(context) {
636636
const sourceCode = context.getSourceCode();
637+
// eslint-disable-next-line no-restricted-syntax -- We need all raw options.
637638
const configureList = context.options || [];
638639

639640
type Scope = null | {

packages/eslint-plugin/src/rules/space-before-blocks.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ export default util.createRule<Options, MessageIds>({
2929
},
3030
},
3131
defaultOptions: ['always'],
32-
create(context) {
32+
create(context, [config]) {
3333
const rules = baseRule.create(context);
34-
const config = context.options[0];
3534
const sourceCode = context.getSourceCode();
3635

3736
let requireSpace = true;

0 commit comments

Comments
 (0)