Replies: 2 comments 2 replies
-
I want to write my own custom rules too, so making
And I haven't tried your rule, but I have created a rule myself and it worked. I think this is all that you would need to change from your code above: -import {
- createESLintRule,
- getTemplateParserServices
-} from '@angular-eslint/eslint-plugin-template/dist/utils/create-eslint-rule';
import { TmplAstElement } from '@angular-eslint/bundled-angular-compiler';
import { TSESLint } from '@typescript-eslint/experimental-utils';
export const RULE_NAME = 'no-javascript-links';
export type MessageIds = 'illegalHref';
export type Options = [];
-export default createESLintRule<Options, MessageIds>({
+const RULE = {
name: RULE_NAME,
defaultOptions: [],
meta: {
type: 'problem',
docs: {
description: 'Usage of href="javascript:" is forbidden',
recommended: 'error'
},
schema: [],
messages: {
illegalHref: 'Usage of href="javascript:" is forbidden'
}
},
create(context: Readonly<TSESLint.RuleContext<MessageIds, Options>>) {
- const parserServices = getTemplateParserServices(context);
+ const parserServices = (context as any).parserServices as ParserServices;
return {
'Element$1[name=/^(a)$/]'(node: TmplAstElement) {
const href = node.attributes.find(({ name }) => name === 'href');
if (href && href.value && href.value.match(/^javascript:/)) {
const loc = parserServices.convertElementSourceSpanToLoc(context, node);
context.report({
loc,
messageId: 'illegalHref',
data: {
element: node.name
}
});
}
}
};
}
-});
+};
+
+export default RULE;
+interface ParserServices {
+ convertNodeSourceSpanToLoc: (sourceSpan: ParseSourceSpan, ) => TSESTree.SourceLocation;
+ convertElementSourceSpanToLoc: (context: Readonly<TSESLint.RuleContext<string, readonly unknown[]>>, node: TmplAstElement, ) => TSESTree.SourceLocation;
+} |
Beta Was this translation helpful? Give feedback.
-
I started another discussion to try to get an overview of other Angular ESLint custom rules: #1106 Besides being useful for projects developers, it might also be helpful for rule developers. |
Beta Was this translation helpful? Give feedback.
-
Is there a tutorial somewhere?
I tried to figure out how to write a custom rule for my templates.
But it seems like I can't reach required utilities, that are contained in the
@angular-eslint/eslint-plugin-template
package.I can't import
@angular-eslint/eslint-plugin-template/dist/utils/create-eslint-rule
in@angular-eslint/eslint-plugin-template@13.0.1
I started writing a rule to forbid the use of
<a href="javascript:">
links.Does anyone know how to solve this?
Beta Was this translation helpful? Give feedback.
All reactions