UNPKG

@rushstack/eslint-plugin

Version:

An ESLint plugin providing supplementary rules for use with the @rushstack/eslint-config package

59 lines 2.61 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.noBackslashImportsRule = exports.MESSAGE_ID = void 0; const LintUtilities_1 = require("./LintUtilities"); exports.MESSAGE_ID = 'no-backslash-imports'; exports.noBackslashImportsRule = { defaultOptions: [], meta: { type: 'problem', messages: { [exports.MESSAGE_ID]: 'The specified import target path contains backslashes.' }, schema: [], docs: { description: 'Prevents imports using paths that use backslashes', url: 'https://www.npmjs.com/package/@rushstack/eslint-plugin' }, fixable: 'code' }, create: (context) => { const checkImportExpression = (importExpression) => { if (!importExpression) { // Can't validate, return return; } // Determine the target file path and find the most direct relative path from the source file const importSpecifier = (0, LintUtilities_1.parseImportSpecifierFromExpression)(importExpression); if (importSpecifier === undefined) { // Can't validate, return return; } // Check if the import path contains backslashes. If it does, suggest a fix to replace them with forward // slashes. const { importTarget } = importSpecifier; if (importTarget.includes('\\')) { context.report({ node: importExpression, messageId: exports.MESSAGE_ID, fix: (fixer) => { const normalizedSpecifier = { ...importSpecifier, importTarget: importTarget.replace(/\\/g, '/') }; return fixer.replaceText(importExpression, `'${(0, LintUtilities_1.serializeImportSpecifier)(normalizedSpecifier)}'`); } }); } }; return { ImportDeclaration: (node) => checkImportExpression(node.source), ImportExpression: (node) => checkImportExpression(node.source), ExportAllDeclaration: (node) => checkImportExpression(node.source), ExportNamedDeclaration: (node) => checkImportExpression(node.source) }; } }; //# sourceMappingURL=no-backslash-imports.js.map