eslint-plugin-decorator-position
Advanced tools
Comparing version 1.1.1 to 2.0.0
module.exports = { | ||
extends: require.resolve('./base.js'), | ||
rules: { | ||
'decorator-position/decorator-position': [ | ||
'error', | ||
{ | ||
onSameLine: ['@tracked', '@service', '@attr', '@hasMany', '@belongsTo'], | ||
onDifferentLines: ['@dependentKeyCompat', '@computed', '@action'], | ||
}, | ||
], | ||
'decorator-position/decorator-position': ['error', {}], | ||
}, | ||
}; |
@@ -42,2 +42,6 @@ 'use strict'; | ||
}, | ||
alignmentOptions: { | ||
type: 'string', | ||
enum: ['prefer-inline', 'above'], | ||
}, | ||
}, | ||
@@ -50,18 +54,19 @@ additionalItems: false, | ||
properties: { | ||
onSameLine: { | ||
type: 'array', | ||
uniqeItems: true, | ||
items: { $ref: '#/definitions/decoratorConfig' }, | ||
}, | ||
onDifferentLines: { | ||
type: 'array', | ||
uniqeItems: true, | ||
items: { $ref: '#/definitions/decoratorConfig' }, | ||
}, | ||
defaults: { | ||
properties: { $ref: '#/definitions/alignmentOptions' }, | ||
methods: { $ref: '#/definitions/alignmentOptions' }, | ||
overrides: { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
properties: { type: 'string' }, | ||
methods: { type: 'string' }, | ||
above: { | ||
type: 'array', | ||
uniqeItems: true, | ||
items: { $ref: '#/definitions/decoratorConfig' }, | ||
}, | ||
'prefer-inline': { | ||
type: 'array', | ||
uniqeItems: true, | ||
items: { $ref: '#/definitions/decoratorConfig' }, | ||
}, | ||
}, | ||
@@ -101,6 +106,16 @@ }, | ||
const PREFER_INLINE = 'prefer-inline'; | ||
const ABOVE = 'above'; | ||
const PROPERTIES = 'properties'; | ||
const METHODS = 'methods'; | ||
// specifics set by the eslint config | ||
const defaultOptions = { | ||
onSameLine: [], | ||
onDifferentLines: [], | ||
[PROPERTIES]: PREFER_INLINE, | ||
[METHODS]: ABOVE, | ||
overrides: { | ||
[PREFER_INLINE]: [], | ||
[ABOVE]: [], | ||
}, | ||
}; | ||
@@ -110,11 +125,13 @@ | ||
const userOptions = context.options[0] || {}; | ||
const options = Object.assign({}, defaultOptions, userOptions); | ||
const options = normalizeOptions(userOptions); | ||
return { | ||
'ClassProperty[decorators.length=1]:exit'(node) { | ||
checkDecorators(context, node, options); | ||
applyOverrides(context, node, options); | ||
positionDecorator(context, node, options); | ||
}, | ||
// NOTE: both getters and methods are of type MethodDefinition | ||
'MethodDefinition[decorators.length=1]:exit'(node) { | ||
checkDecorators(context, node, options); | ||
applyOverrides(context, node, options); | ||
positionDecorator(context, node, options); | ||
}, | ||
@@ -124,9 +141,8 @@ }; | ||
function checkDecorators(context, node, options) { | ||
function applyOverrides(context, node, options) { | ||
placeDecoratorsBesideProperty(context, node, options); | ||
placeDecoratorsAboveProperty(context, node, options); | ||
fallbackPlacement(context, node, options); | ||
} | ||
function fallbackPlacement(context, node, options) { | ||
function positionDecorator(context, node, options) { | ||
const namedConfigs = configuredDecoratorsInOptions(options); | ||
@@ -143,15 +159,17 @@ | ||
const fallbackConfig = options.defaults; | ||
Object.keys(options).forEach(key => { | ||
if (key === 'overrides') { | ||
return; | ||
} | ||
Object.keys(fallbackConfig).forEach(key => { | ||
const position = fallbackConfig[key]; | ||
const position = options[key]; | ||
const isMemberRelevant = | ||
(key === 'properties' && node.type === 'ClassProperty') || | ||
(key === 'methods' && node.type === 'MethodDefinition'); | ||
(key === PROPERTIES && node.type === 'ClassProperty') || | ||
(key === METHODS && node.type === 'MethodDefinition'); | ||
if (isMemberRelevant) { | ||
if (position === 'inline') { | ||
if (position === PREFER_INLINE) { | ||
placeDecoratorsBesideProperty(context, node, { | ||
onSameLine: decorators, | ||
overrides: { [PREFER_INLINE]: decorators }, | ||
}); | ||
@@ -161,3 +179,3 @@ } else { | ||
placeDecoratorsAboveProperty(context, node, { | ||
onDifferentLines: decorators, | ||
overrides: { [ABOVE]: decorators }, | ||
}); | ||
@@ -170,3 +188,3 @@ } | ||
function placeDecoratorsBesideProperty(context, node, options) { | ||
for (const decoratorConfig of options.onSameLine) { | ||
for (const decoratorConfig of options.overrides[PREFER_INLINE]) { | ||
const config = normalizeConfig(decoratorConfig, INTENT.SAME_LINE); | ||
@@ -198,3 +216,3 @@ const info = decoratorInfo(node, config); | ||
function placeDecoratorsAboveProperty(context, node, options) { | ||
for (const decoratorConfig of options.onDifferentLines) { | ||
for (const decoratorConfig of options.overrides[ABOVE]) { | ||
const config = normalizeConfig(decoratorConfig, INTENT.DIFFERENT_LINES); | ||
@@ -233,6 +251,18 @@ const info = decoratorInfo(node, config); | ||
function normalizeOptions(userOptions) { | ||
const options = Object.assign({}, defaultOptions, userOptions); | ||
options.overrides = { | ||
[ABOVE]: [], | ||
[PREFER_INLINE]: [], | ||
...options.overrides, | ||
}; | ||
return options; | ||
} | ||
function configuredDecoratorsInOptions(options) { | ||
const { onSameLine, onDifferentLines } = options; | ||
const { [PREFER_INLINE]: preferInline, [ABOVE]: above } = options.overrides; | ||
const allConfigs = [...onSameLine.map(normalizeConfig), ...onDifferentLines.map(normalizeConfig)]; | ||
const allConfigs = [...preferInline.map(normalizeConfig), ...above.map(normalizeConfig)]; | ||
@@ -239,0 +269,0 @@ return allConfigs.map(config => config[0]); |
{ | ||
"name": "eslint-plugin-decorator-position", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "ESLint plugin for enforcing decorator position", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -43,8 +43,4 @@ # eslint-plugin-decorator-position | ||
{ | ||
onSameLine: ['@tracked'], | ||
onDifferentLines: ['@computed'], | ||
defaults: { | ||
properties: 'inline', | ||
methods: 'above' | ||
} | ||
properties: 'above', | ||
methods: 'prefer-inline' | ||
} | ||
@@ -51,0 +47,0 @@ ] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18238
333
99