'use strict';var _messages;function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}

var isCoreModule = require('is-core-module');var _require =
require('../docsUrl'),docsUrl = _require['default'];

var DO_PREFER_MESSAGE_ID = 'requireNodeProtocol';
var NEVER_PREFER_MESSAGE_ID = 'forbidNodeProtocol';
var messages = (_messages = {}, _defineProperty(_messages,
DO_PREFER_MESSAGE_ID, 'Prefer `node:{{moduleName}}` over `{{moduleName}}`.'), _defineProperty(_messages,
NEVER_PREFER_MESSAGE_ID, 'Prefer `{{moduleName}}` over `node:{{moduleName}}`.'), _messages);


function replaceStringLiteral(
fixer,
node,
text,
relativeRangeStart,
relativeRangeEnd)
{
  var firstCharacterIndex = node.range[0] + 1;
  var start = Number.isInteger(relativeRangeEnd) ?
  relativeRangeStart + firstCharacterIndex :
  firstCharacterIndex;
  var end = Number.isInteger(relativeRangeEnd) ?
  relativeRangeEnd + firstCharacterIndex :
  node.range[1] - 1;

  return fixer.replaceTextRange([start, end], text);
}

function isStringLiteral(node) {
  return node && node.type === 'Literal' && typeof node.value === 'string';
}

function isStaticRequireWith1Param(node) {
  return !node.optional &&
  node.callee.type === 'Identifier' &&
  node.callee.name === 'require'
  // check for only 1 argument
  && node.arguments.length === 1 &&
  node.arguments[0] &&
  isStringLiteral(node.arguments[0]);
}

function checkAndReport(src, context) {
  // TODO use src.quasis[0].value.raw
  if (!src || src.type === 'TemplateLiteral') {return;}
  var moduleName = 'value' in src ? src.value : src.name;
  if (typeof moduleName !== 'string') {console.log(src, moduleName);}var
  settings = context.settings;
  var nodeVersion = settings && settings['import/node-version'];
  if (
  typeof nodeVersion !== 'undefined' && (

  typeof nodeVersion !== 'string' ||
  !/^[0-9]+\.[0-9]+\.[0-9]+$/.test(nodeVersion)))

  {
    throw new TypeError('`import/node-version` setting must be a string in the format "10.23.45" (a semver version, with no leading zero)');
  }

  if (context.options[0] === 'never') {
    if (!moduleName.startsWith('node:')) {return;}

    var actualModuleName = moduleName.slice(5);
    if (!isCoreModule(actualModuleName, nodeVersion || undefined)) {return;}

    context.report({
      node: src,
      message: messages[NEVER_PREFER_MESSAGE_ID],
      data: { moduleName: actualModuleName },
      /** @param {import('eslint').Rule.RuleFixer} fixer */
      fix: function () {function fix(fixer) {
          return replaceStringLiteral(fixer, src, '', 0, 5);
        }return fix;}() });

  } else if (context.options[0] === 'always') {
    if (
    moduleName.startsWith('node:') ||
    !isCoreModule(moduleName, nodeVersion || undefined) ||
    !isCoreModule('node:' + String(moduleName), nodeVersion || undefined))
    {
      return;
    }

    context.report({
      node: src,
      message: messages[DO_PREFER_MESSAGE_ID],
      data: { moduleName: moduleName },
      /** @param {import('eslint').Rule.RuleFixer} fixer */
      fix: function () {function fix(fixer) {
          return replaceStringLiteral(fixer, src, 'node:', 0, 0);
        }return fix;}() });

  } else if (typeof context.options[0] === 'undefined') {
    throw new Error('Missing option');
  } else {
    throw new Error('Unexpected option: ' + String(context.options[0]));
  }
}

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'Enforce either using, or omitting, the `node:` protocol when importing Node.js builtin modules.',
      recommended: true,
      category: 'Static analysis',
      url: docsUrl('enforce-node-protocol-usage') },

    fixable: 'code',
    schema: {
      type: 'array',
      minItems: 1,
      maxItems: 1,
      items: [
      {
        'enum': ['always', 'never'] }] },



    messages: messages },

  create: function () {function create(context) {
      return {
        CallExpression: function () {function CallExpression(node) {
            if (!isStaticRequireWith1Param(node)) {return;}

            var arg = node.arguments[0];

            return checkAndReport(arg, context);
          }return CallExpression;}(),
        ExportNamedDeclaration: function () {function ExportNamedDeclaration(node) {
            return checkAndReport(node.source, context);
          }return ExportNamedDeclaration;}(),
        ImportDeclaration: function () {function ImportDeclaration(node) {
            return checkAndReport(node.source, context);
          }return ImportDeclaration;}(),
        ImportExpression: function () {function ImportExpression(node) {
            if (!isStringLiteral(node.source)) {return;}

            return checkAndReport(node.source, context);
          }return ImportExpression;}() };

    }return create;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 11932 characters]