fix packages
This commit is contained in:
13
eslint/plugins/eslint-plugin-vega/index.js
Normal file
13
eslint/plugins/eslint-plugin-vega/index.js
Normal file
@ -0,0 +1,13 @@
|
||||
var rxjsNoMemoryLeak = require('./rules/rxjs-no-memory-leaks/rxjs-no-memory-leaks');
|
||||
|
||||
var plugin = {
|
||||
meta: {
|
||||
name: "eslint-plugin-vega",
|
||||
version: "0.0.1"
|
||||
},
|
||||
rules: {
|
||||
"rxjs-no-memory-leaks": rxjsNoMemoryLeak
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = plugin;
|
@ -0,0 +1,76 @@
|
||||
function getSubscribeNodeFrom(observableNode) {
|
||||
return observableNode.callee;
|
||||
}
|
||||
|
||||
function isSubscribedObservable(observableNode) {
|
||||
try {
|
||||
return observableNode.callee.property.name === 'subscribe';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function getPipeNodeOf(subscribeNode) {
|
||||
try {
|
||||
var pipeNodeRef = subscribeNode.object.callee;
|
||||
|
||||
if (pipeNodeRef.property.name !== 'pipe') throw new Error();
|
||||
|
||||
return pipeNodeRef;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function getOperatorsFor(pipeNode) {
|
||||
return pipeNode.parent.arguments;
|
||||
}
|
||||
|
||||
function isReleaseOperator(operatorNode) {
|
||||
return operatorNode.callee.name === 'takeUntil' || operatorNode.callee.name === 'untilDestroyed';
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
type: "problem",
|
||||
docs: {
|
||||
description: "Check every rx.js subscription handling with release operator.",
|
||||
},
|
||||
fixable: "code",
|
||||
schema: []
|
||||
},
|
||||
create: function (context) {
|
||||
return {
|
||||
CallExpression(node) {
|
||||
if (isSubscribedObservable(node)) {
|
||||
const subscribeNode = getSubscribeNodeFrom(node);
|
||||
const pipeNode = getPipeNodeOf(subscribeNode);
|
||||
|
||||
if (pipeNode) {
|
||||
const operators = getOperatorsFor(pipeNode);
|
||||
|
||||
if (operators.length) {
|
||||
if (!isReleaseOperator(operators[operators.length - 1])) {
|
||||
context.report({
|
||||
node,
|
||||
message: 'Last operator in pipe should release subscription. Example: `subj$.pipe(operator1(), operator2(), takeUntil(destroy$))`'
|
||||
});
|
||||
}
|
||||
} else {
|
||||
context.report({
|
||||
node,
|
||||
message: 'Pipe associated with subscribed observable should have release operator such `takeUntil(destroy$)`'
|
||||
});
|
||||
}
|
||||
} else {
|
||||
context.report({
|
||||
node,
|
||||
message: 'Subscriptions Should be piped with release operator'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
@ -0,0 +1,53 @@
|
||||
var RuleTester = require('eslint').RuleTester;
|
||||
var rxjsNoMemoryLeaksRule = require('./rxjs-no-memory-leaks');
|
||||
|
||||
describe('rxjs-no-memory-leak', () => {
|
||||
let ruleTester = new RuleTester({
|
||||
parserOptions: {ecmaVersion: 2015}
|
||||
});
|
||||
|
||||
ruleTester.run(
|
||||
"rxjs-no-memory-leaks",
|
||||
rxjsNoMemoryLeaksRule,
|
||||
{
|
||||
valid: [{
|
||||
code: `
|
||||
let subjValid$ = new Subject();
|
||||
let destroy$ = new Subject();
|
||||
|
||||
subjValid$.pipe(map(() => 1234),
|
||||
tap(console.log),
|
||||
takeUntil(destroy$)).subscribe(console.log);
|
||||
|
||||
subjValid$.next();
|
||||
`,
|
||||
}],
|
||||
invalid: [
|
||||
{
|
||||
code: `
|
||||
let subjInval1$ = new Subject();
|
||||
|
||||
subjInval$1.subscribe(() => console.log(1234));
|
||||
`,
|
||||
errors: [{message: 'Subscriptions Should be piped with release operator'}],
|
||||
},
|
||||
{
|
||||
code: `
|
||||
let subjInval2$ = new Subject();
|
||||
|
||||
subjInval2$.pipe().subscribe(() => console.log(1234));
|
||||
`,
|
||||
errors: [{message: 'Pipe associated with subscribed observable should have release operator such `takeUntil(destroy$)`'}],
|
||||
},
|
||||
{
|
||||
code: `
|
||||
let subjInval3$ = new Subject();
|
||||
|
||||
subjInval3$.pipe(map(() => console.log(4567))).subscribe(() => console.log(1234));
|
||||
`,
|
||||
errors: [{message: 'Last operator in pipe should release subscription. Example: `subj$.pipe(operator1(), operator2(), takeUntil(destroy$))`'}],
|
||||
}
|
||||
],
|
||||
}
|
||||
);
|
||||
});
|
Reference in New Issue
Block a user