mkdir eslint-plugin-loplat-test
cd eslint-plugin-loplat-test
yo eslint:plugin
// ? Does this plugin contain custom ESLint rules? Yes
// ? Does this plugin contain one or more processors? No
yo eslint:rule
// ? Where will this rule be published? ESLint Plugin
// lib/rules/spellcheck.js
/**
* @fileoverview test
* @author loplat
*/
'use strict';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem', // `problem`, `suggestion`, or `layout`
fixable: 'code', // Or `code` or `whitespace`
schema: [] // Add a schema if the rule has options
},
create(context) {
// variables should be defined here
const dictionary = {
메세지: '메시지',
Loplat: 'loplat',
'loplat x': 'loplat X',
'loplat I': 'loplat i'
};
return {
// visitor functions for different types of nodes
Literal(node) { // Literal type 일때
const text = node.raw;
Object.entries(dictionary).forEach(([wrong, correct]) => {
if (text.includes(wrong)) { // 오타가 있으면
context.report({
node,
message: "'{{ correct }}'가 맞는 표현입니다.",
data: { correct },
fix: (fixer) =>
fixer.replaceText(node, text.replace(wrong, correct)) // 올바른 표현으로 교정
});
}
});
}
};
}
};
작성한 custom rule은 아래와 같은 상황의 오류를 감지/보고한다. - Literal type의 AST node일 경우 && - node의 text가 '메세지', 'Loplat', 'loplat x', 'loplat I'를 포함할 경우 && - context.report를 이용하여 오류 message를 보고하고, fix속성을 이용하여 올바른 표현으로 교정한다.
작성한 rule을 tests/lib/rules/spellckeck.js를 통해 테스트한다. 테스트 파일은 아래와 같이 작성했다.