Socket
Socket
Sign inDemoInstall

eslint-plugin-ngx-eslint

Package Overview
Dependencies
100
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.0.2

lib/utils.d.ts

4

lib/index.d.ts
declare const _default: {
rules: {
"destroy-service-provider": import("eslint").Rule.RuleModule;
"destroy-service-provider": import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"missing", [{
destroyServiceName?: string | undefined;
}], import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleListener>;
};

@@ -5,0 +7,0 @@ };

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
const destroy_service_provider_1 = __importDefault(require("./rules/destroy-service-provider"));
const destroyServiceProviderRule = __importStar(require("./rules/destroy-service-provider"));
module.exports = {
rules: {
"destroy-service-provider": destroy_service_provider_1.default,
[destroyServiceProviderRule.ruleName]: destroyServiceProviderRule.rule,
},
};
//# sourceMappingURL=index.js.map

@@ -1,4 +0,12 @@

import { Rule } from "eslint";
declare const rule: Rule.RuleModule;
export = rule;
export declare const ruleName = "destroy-service-provider";
declare type Options = [
{
/**
* @default DestroyService
*/
destroyServiceName?: string;
}
];
export declare const rule: import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"missing", Options, import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleListener>;
export {};
//# sourceMappingURL=destroy-service-provider.d.ts.map
"use strict";
const rule = {
create: context => {
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = exports.ruleName = void 0;
const utils_1 = require("@typescript-eslint/utils");
const utils_2 = require("../utils");
exports.ruleName = "destroy-service-provider";
const createRule = utils_1.ESLintUtils.RuleCreator(name => `${utils_2.repositoryUrl}#${name}`);
exports.rule = createRule({
create(context, [options]) {
return {
"ClassDeclaration > Decorator[expression.callee.name=/^(Component|Directive)$/]": (node) => {
const nodeExpression = node.expression;
const isDecoratorEmpty = !nodeExpression.arguments.length ||
!nodeExpression.arguments[0].properties
.length;
if (isDecoratorEmpty) {
return;
}
// whether component has providers decorator property
const providersProperty = node.expression.arguments[0].properties.find((property) => {
const providersProperty = nodeExpression.arguments[0].properties.find((property) => {
return (property.key.type === "Identifier" &&

@@ -17,3 +30,4 @@ property.key.name === "providers");

!!providersProperty.value.elements.find((e) => {
return e.type === "Identifier" && e.name === "DestroyService";
return (e.type === "Identifier" &&
e.name === options.destroyServiceName);
});

@@ -26,3 +40,3 @@ }

const classElements = classDeclaration.body.body;
const classConstructor = classElements.find((e) => {
const classConstructor = classElements.find(e => {
return e.type === "MethodDefinition" && e.kind === "constructor";

@@ -33,3 +47,4 @@ });

// find DestroyService
const params = classConstructor.value.params;
const params = classConstructor.value
.params;
hasDestroy = params.find(param => {

@@ -43,3 +58,3 @@ return (param.type === "TSParameterProperty" &&

param.parameter.typeAnnotation.typeAnnotation.typeName
.name === "DestroyService");
.name === options.destroyServiceName);
});

@@ -50,3 +65,7 @@ }

loc: hasDestroy.loc,
message: `Please provide DestroyService in ${node.expression.callee.name} class providers.`,
messageId: "missing",
data: {
className: nodeExpression.callee.name,
destroyServiceName: options.destroyServiceName,
},
});

@@ -58,4 +77,27 @@ }

},
};
module.exports = rule;
name: exports.ruleName,
meta: {
type: "problem",
docs: {
description: "Destroy service should be provided in Component/Directive providers/viewProviders array.",
recommended: "error",
},
messages: {
missing: "Please provide {{destroyServiceName}} in {{className}} class providers.",
},
schema: [
{
type: "object",
properties: {
destroyServiceName: {
type: "string",
default: "DestroyService",
},
},
additionalProperties: false,
},
],
},
defaultOptions: [{ destroyServiceName: "DestroyService" }],
});
//# sourceMappingURL=destroy-service-provider.js.map
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const eslint_1 = require("eslint");
const destroy_service_provider_1 = __importDefault(require("./destroy-service-provider"));
const tester = new eslint_1.RuleTester({
parser: require.resolve("@typescript-eslint/parser"),
parserOptions: { ecmaVersion: 2015 },
const utils_1 = require("@typescript-eslint/utils");
const destroy_service_provider_1 = require("./destroy-service-provider");
const tester = new utils_1.ESLintUtils.RuleTester({
parser: "@typescript-eslint/parser",
});
tester.run("destroy-service-provider", destroy_service_provider_1.default, {
tester.run("destroy-service-provider", destroy_service_provider_1.rule, {
valid: [

@@ -30,2 +26,31 @@ {

code: `
@Component({
selector: 'my-orgs',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.scss'],
providers: [DestroyService]
})
export class WelcomeComponent implements OnInit {
constructor(
private destroy$: DestroyService,
) {}
}`,
},
{
code: `
@Component({
selector: 'my-orgs',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.scss'],
providers: [Destroy]
})
export class WelcomeComponent implements OnInit {
constructor(
private destroy$: Destroy,
) {}
}`,
options: [{ destroyServiceName: "Destroy" }],
},
{
code: `
@Directive({

@@ -41,2 +66,67 @@ selector: 'my-directive',

},
{
code: `
@Directive({
selector: 'my-directive',
providers: [Destroy]
})
export class MyDirective implements OnInit {
constructor(
private destroy$: Destroy,
) {}
}`,
options: [{ destroyServiceName: "Destroy" }],
},
// developer forgot to config custom DestroyService name to `Destroy`
// so the rule doesn't recognize `Destroy` is a DestroyService
{
code: `
@Directive({
selector: 'my-directive',
providers: []
})
export class MyDirective implements OnInit {
constructor(
private destroy$: Destroy,
) {}
}`,
},
// test cases below will verify when syntax of Component/Directive is invalid
// the rule won't check until Component/Directive syntax error is fixed
{
code: `
@Component()
export class WelcomeComponent implements OnInit {
constructor(
private destroy$: DestroyService,
) {}
}`,
},
{
code: `
@Component({})
export class WelcomeComponent implements OnInit {
constructor(
private destroy$: DestroyService,
) {}
}`,
},
{
code: `
@Directive()
export class MyDirective implements OnInit {
constructor(
private destroy$: DestroyService,
) {}
}`,
},
{
code: `
@Directive({})
export class MyDirective implements OnInit {
constructor(
private destroy$: DestroyService,
) {}
}`,
},
],

@@ -59,3 +149,7 @@ invalid: [

{
message: "Please provide DestroyService in Component class providers.",
messageId: "missing",
data: {
className: "Component",
destroyServiceName: "DestroyService",
},
},

@@ -66,4 +160,74 @@ ],

code: `
@Component({
selector: 'my-orgs',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.scss'],
})
export class WelcomeComponent implements OnInit {
constructor(
private destroy$: DestroyService,
) {}
}`,
errors: [
{
messageId: "missing",
data: {
className: "Component",
destroyServiceName: "DestroyService",
},
},
],
},
{
code: `
@Component({
selector: 'my-orgs',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.scss'],
providers: []
})
export class WelcomeComponent implements OnInit {
constructor(
private destroy$: Destroy,
) {}
}`,
options: [{ destroyServiceName: "Destroy" }],
errors: [
{
messageId: "missing",
data: {
className: "Component",
destroyServiceName: "Destroy",
},
},
],
},
{
code: `
@Component({
selector: 'my-orgs',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.scss'],
})
export class WelcomeComponent implements OnInit {
constructor(
private destroy$: Destroy,
) {}
}`,
options: [{ destroyServiceName: "Destroy" }],
errors: [
{
messageId: "missing",
data: {
className: "Component",
destroyServiceName: "Destroy",
},
},
],
},
{
code: `
@Directive({
selector: 'my-directive',
providers: [],
})

@@ -77,8 +241,75 @@ export class MyDirective implements OnInit {

{
message: "Please provide DestroyService in Directive class providers.",
messageId: "missing",
data: {
className: "Directive",
destroyServiceName: "DestroyService",
},
},
],
},
{
code: `
@Directive({
selector: 'my-directive',
})
export class MyDirective implements OnInit {
constructor(
private destroy$: DestroyService,
) {}
}`,
errors: [
{
messageId: "missing",
data: {
className: "Directive",
destroyServiceName: "DestroyService",
},
},
],
},
{
code: `
@Directive({
selector: 'my-directive',
providers: []
})
export class MyDirective implements OnInit {
constructor(
private destroy$: Destroy,
) {}
}`,
options: [{ destroyServiceName: "Destroy" }],
errors: [
{
messageId: "missing",
data: {
className: "Directive",
destroyServiceName: "Destroy",
},
},
],
},
{
code: `
@Directive({
selector: 'my-directive',
})
export class MyDirective implements OnInit {
constructor(
private destroy$: Destroy,
) {}
}`,
options: [{ destroyServiceName: "Destroy" }],
errors: [
{
messageId: "missing",
data: {
className: "Directive",
destroyServiceName: "Destroy",
},
},
],
},
],
});
//# sourceMappingURL=destroy-service-provider.test.js.map
{
"name": "eslint-plugin-ngx-eslint",
"version": "1.0.0",
"version": "1.0.2",
"description": "Custom ESLint rules for Angular projects",

@@ -36,2 +36,3 @@ "main": "lib/index.js",

"@typescript-eslint/parser": "5.21.0",
"@typescript-eslint/utils": "5.31.0",
"eslint": "8.15.0",

@@ -38,0 +39,0 @@ "husky": "7.0.4",

@@ -9,8 +9,16 @@ # Custom ESLint rules for Angular projects

`destroy-service-provider`: When using `DestroyService` to automatically unsubscribe from an observable, this service MUST be provided in Component/Directive class providers in order for the Service to work properly. This rule ensures the DestroyService is always provided in the `providers` array of the Component or Directive.
- `destroy-service-provider`: When using `DestroyService` to automatically unsubscribe from an observable, this service MUST be provided in Component/Directive class providers in order for the Service to work properly. This rule ensures the DestroyService is always provided in the `providers` array of the Component or Directive.
## Usage
Edit your eslint config file as follow
### Install the package
```bash
npm install --save-dev eslint-plugin-ngx-eslint
```
### Config rule `destroy-service-provider`
Edit your `.eslintrc` config file as follow
```json

@@ -25,4 +33,20 @@ {

If your destroy service class name is different than `DestroyService`, the configuration would be like this
```json
{
"plugins": ["ngx-eslint"],
"rules": {
"ngx-eslint/destroy-service-provider": [
"error",
{
"destroyServiceName": "MyDestroyService"
}
]
}
}
```
## LICENSE
MIT

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc