Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-decorator-position

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-decorator-position - npm Package Compare versions

Comparing version 1.0.4 to 1.1.0

92

lib/rules/decorator-position.js

@@ -21,2 +21,5 @@ 'use strict';

type: 'array',
minItems: 1,
maxItems: 2,
additionalItems: false,
items: [

@@ -26,2 +29,3 @@ { type: 'string' },

type: 'object',
additionalProperties: false,
properties: {

@@ -32,8 +36,4 @@ withArgs: {

},
additionalProperties: false,
},
],
additionalItems: false,
minItems: 1,
maxItems: 2,
},

@@ -46,5 +46,7 @@ {

},
additionalItems: false,
items: [
{
type: 'object',
additionalProperties: false,
properties: {

@@ -61,7 +63,13 @@ onSameLine: {

},
defaults: {
type: 'object',
additionalProperties: false,
properties: {
properties: { type: 'string' },
methods: { type: 'string' },
},
},
},
additionalProperties: false,
},
],
additionalItems: false,
},

@@ -95,15 +103,7 @@ },

'ClassProperty[decorators.length=1]:exit'(node) {
const { decorators } = node;
if (decorators && decorators.length === 1) {
checkDecorators(context, node, options);
}
checkDecorators(context, node, options);
},
// NOTE: both getters and methods are of type MethodDefinition
'MethodDefinition[decorators.length=1]:exit'(node) {
const { decorators } = node;
if (decorators && decorators.length === 1) {
checkDecorators(context, node, options);
}
checkDecorators(context, node, options);
},

@@ -114,2 +114,44 @@ };

function checkDecorators(context, node, options) {
placeDecoratorsBesideProperty(context, node, options);
placeDecoratorsAboveProperty(context, node, options);
fallbackPlacement(context, node, options);
}
function fallbackPlacement(context, node, options) {
const namedConfigs = configuredDecoratorsInOptions(options);
const decorators = node.decorators.map(nameOfDecorator);
for (const name of decorators) {
if (namedConfigs.includes(name)) {
// there exists a more specific config for this decorator
return;
}
}
const fallbackConfig = options.defaults;
Object.keys(fallbackConfig).forEach(key => {
const position = fallbackConfig[key];
const isMemberRelevant =
(key === 'properties' && node.type === 'ClassProperty') ||
(key === 'methods' && node.type === 'MethodDefinition');
if (isMemberRelevant) {
if (position === 'inline') {
placeDecoratorsBesideProperty(context, node, {
onSameLine: decorators,
});
} else {
// above
placeDecoratorsAboveProperty(context, node, {
onDifferentLines: decorators,
});
}
}
});
}
function placeDecoratorsBesideProperty(context, node, options) {
for (const decoratorConfig of options.onSameLine) {

@@ -139,3 +181,5 @@ const config = normalizeConfig(decoratorConfig, INTENT.SAME_LINE);

}
}
function placeDecoratorsAboveProperty(context, node, options) {
for (const decoratorConfig of options.onDifferentLines) {

@@ -171,2 +215,14 @@ const config = normalizeConfig(decoratorConfig, INTENT.DIFFERENT_LINES);

// ///////////////////////////////////
// Helpers
// ///////////////////////////////////
function configuredDecoratorsInOptions(options) {
const { onSameLine, onDifferentLines } = options;
const allConfigs = [...onSameLine.map(normalizeConfig), ...onDifferentLines.map(normalizeConfig)];
return allConfigs.map(config => config[0]);
}
function normalizeConfig(config, intent) {

@@ -190,6 +246,2 @@ let name;

// ///////////////////////////////////
// Helpers
// ///////////////////////////////////
function linePositioning(decorator, key) {

@@ -196,0 +248,0 @@ const decoratorLine = decorator.expression.loc.end.line;

{
"name": "eslint-plugin-decorator-position",
"version": "1.0.4",
"version": "1.1.0",
"description": "ESLint plugin for enforcing decorator position",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -44,3 +44,7 @@ # eslint-plugin-decorator-position

onSameLine: ['@tracked'],
onDifferentLines: ['@computed']
onDifferentLines: ['@computed'],
defaults: {
properties: 'inline',
methods: 'above'
}
}

@@ -47,0 +51,0 @@ ]

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc