eslint#Rule TypeScript Examples

The following examples show how to use eslint#Rule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: no-parsing-error.ts    From eslint-plugin-jsonc with MIT License 6 votes vote down vote up
/**
 * Report error
 */
function errorReportVisitor(
    context: Rule.RuleContext,
    error: any,
): RuleListener {
    let loc: AST.Position | undefined = undefined
    if ("column" in error && "lineNumber" in error) {
        loc = {
            line: error.lineNumber,
            column: error.column,
        }
    }
    return {
        Program(node) {
            context.report({
                node,
                loc,
                message: error.message,
            })
        },
    }
}
Example #2
Source File: index.ts    From eslint-plugin-jsonc with MIT License 6 votes vote down vote up
/**
 * Get the core rule implementation from the rule name
 */
export function getCoreRule(name: string): Rule.RuleModule {
    let map: Map<string, Rule.RuleModule>
    if (ruleMap) {
        map = ruleMap
    } else {
        // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- load eslint
        ruleMap = map = new (require("eslint").Linter)().getRules()
    }
    return map.get(name)!
}
Example #3
Source File: testkit.ts    From graphql-eslint with MIT License 5 votes vote down vote up
// A simple version of `SourceCodeFixer.applyFixes`
// https://github.com/eslint/eslint/issues/14936#issuecomment-906746754
function applyFix(code: string, { range, text }: Rule.Fix): string {
  return [code.slice(0, range[0]), text, code.slice(range[1])].join('');
}
Example #4
Source File: index.ts    From eslint-plugin-jsonc with MIT License 5 votes vote down vote up
/**
 * Define the rule.
 * @param ruleName ruleName
 * @param rule rule module
 */
export function createRule(
    ruleName: string,
    rule: PartialRuleModule,
): RuleModule {
    return {
        meta: {
            ...rule.meta,
            docs: {
                ...rule.meta.docs,
                url: `https://ota-meshi.github.io/eslint-plugin-jsonc/rules/${ruleName}.html`,
                ruleId: `jsonc/${ruleName}`,
                ruleName,
            },
        },
        jsoncDefineRule: rule,
        create(context: Rule.RuleContext): any {
            if (
                typeof context.parserServices.defineCustomBlocksVisitor ===
                    "function" &&
                path.extname(context.getFilename()) === ".vue"
            ) {
                return context.parserServices.defineCustomBlocksVisitor(
                    context,
                    jsoncESLintParser,
                    {
                        target(lang: string | null, block: V.VElement) {
                            if (lang) {
                                return /^json[5c]?$/i.test(lang)
                            }
                            return block.name === "i18n"
                        },
                        create(blockContext: Rule.RuleContext) {
                            return rule.create(blockContext, {
                                customBlock: true,
                            })
                        },
                    },
                )
            }
            return rule.create(context, {
                customBlock: false,
            })
        },
    }
}
Example #5
Source File: index.ts    From eslint-plugin-jsonc with MIT License 5 votes vote down vote up
/**
 * Define the wrapped core rule.
 */
export function defineWrapperListener(
    coreRule: Rule.RuleModule,
    context: Rule.RuleContext,
    options: any[],
): RuleListener {
    if (!context.parserServices.isJSON) {
        return {}
    }
    const listener = coreRule.create({
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- special
        // @ts-expect-error
        __proto__: context,
        options,
    }) as RuleListener

    const jsonListener: RuleListener = {}
    for (const key of Object.keys(listener)) {
        const original = listener[key]
        if (!original) {
            continue
        }
        const jsonKey = key.replace(
            /(?:^|\b)(ExpressionStatement|ArrayExpression|ObjectExpression|Property|Identifier|Literal|UnaryExpression)(?:\b|$)/gu,
            "JSON$1",
        )
        jsonListener[jsonKey] = function (node: AST.JSONNode, ...args) {
            original.call(this, getProxyNode(node) as never, ...args)
        }
    }

    /**
     *  Check whether a given value is a node.
     */
    function isNode(data: any): boolean {
        return (
            data &&
            typeof data.type === "string" &&
            Array.isArray(data.range) &&
            data.range.length === 2 &&
            typeof data.range[0] === "number" &&
            typeof data.range[1] === "number"
        )
    }

    /**
     * Get the proxy node
     */
    function getProxyNode(node: AST.JSONNode): any {
        const type = node.type.startsWith("JSON")
            ? node.type.slice(4)
            : node.type
        const cache: any = { type }
        return new Proxy(node, {
            get(_t, key) {
                if (key in cache) {
                    return cache[key]
                }
                const data = (node as any)[key]
                if (isNode(data)) {
                    return (cache[key] = getProxyNode(data))
                }
                if (Array.isArray(data)) {
                    return (cache[key] = data.map((e) =>
                        isNode(e) ? getProxyNode(e) : e,
                    ))
                }
                return data
            },
        })
    }

    return jsonListener
}
Example #6
Source File: index.ts    From eslint-plugin-jsonc with MIT License 5 votes vote down vote up
ruleMap: Map<string, Rule.RuleModule> | null = null
Example #7
Source File: CodeLinter.ts    From yumeko with GNU Affero General Public License v3.0 5 votes vote down vote up
export function getRule(query: string): Rule.RuleModule|void {
    return rules.get(query);
}