@babel/code-frame
Advanced tools
Comparing version 7.25.9 to 7.26.0
133
lib/index.js
@@ -1,31 +0,91 @@ | ||
"use strict"; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.codeFrameColumns = codeFrameColumns; | ||
exports.default = _default; | ||
var _highlight = require("@babel/highlight"); | ||
var _picocolors = _interopRequireWildcard(require("picocolors"), true); | ||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } | ||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } | ||
const colors = typeof process === "object" && (process.env.FORCE_COLOR === "0" || process.env.FORCE_COLOR === "false") ? (0, _picocolors.createColors)(false) : _picocolors.default; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var picocolors = require('picocolors'); | ||
var jsTokens = require('js-tokens'); | ||
var helperValidatorIdentifier = require('@babel/helper-validator-identifier'); | ||
function isColorSupported() { | ||
return typeof process === "object" && (process.env.FORCE_COLOR === "0" || process.env.FORCE_COLOR === "false") ? false : picocolors.isColorSupported; | ||
} | ||
const compose = (f, g) => v => f(g(v)); | ||
let pcWithForcedColor = undefined; | ||
function getColors(forceColor) { | ||
if (forceColor) { | ||
var _pcWithForcedColor; | ||
(_pcWithForcedColor = pcWithForcedColor) != null ? _pcWithForcedColor : pcWithForcedColor = (0, _picocolors.createColors)(true); | ||
return pcWithForcedColor; | ||
} | ||
return colors; | ||
} | ||
let deprecationWarningShown = false; | ||
function getDefs(colors) { | ||
function builDefs(colors) { | ||
return { | ||
keyword: colors.cyan, | ||
capitalized: colors.yellow, | ||
jsxIdentifier: colors.yellow, | ||
punctuator: colors.yellow, | ||
number: colors.magenta, | ||
string: colors.green, | ||
regex: colors.magenta, | ||
comment: colors.gray, | ||
invalid: compose(compose(colors.white, colors.bgRed), colors.bold), | ||
gutter: colors.gray, | ||
marker: compose(colors.red, colors.bold), | ||
message: compose(colors.red, colors.bold) | ||
message: compose(colors.red, colors.bold), | ||
reset: colors.reset | ||
}; | ||
} | ||
const defsOn = builDefs(picocolors.createColors(true)); | ||
const defsOff = builDefs(picocolors.createColors(false)); | ||
function getDefs(enabled) { | ||
return enabled ? defsOn : defsOff; | ||
} | ||
const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]); | ||
const NEWLINE$1 = /\r\n|[\n\r\u2028\u2029]/; | ||
const BRACKET = /^[()[\]{}]$/; | ||
let tokenize; | ||
{ | ||
const JSX_TAG = /^[a-z][\w-]*$/i; | ||
const getTokenType = function (token, offset, text) { | ||
if (token.type === "name") { | ||
if (helperValidatorIdentifier.isKeyword(token.value) || helperValidatorIdentifier.isStrictReservedWord(token.value, true) || sometimesKeywords.has(token.value)) { | ||
return "keyword"; | ||
} | ||
if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.slice(offset - 2, offset) === "</")) { | ||
return "jsxIdentifier"; | ||
} | ||
if (token.value[0] !== token.value[0].toLowerCase()) { | ||
return "capitalized"; | ||
} | ||
} | ||
if (token.type === "punctuator" && BRACKET.test(token.value)) { | ||
return "bracket"; | ||
} | ||
if (token.type === "invalid" && (token.value === "@" || token.value === "#")) { | ||
return "punctuator"; | ||
} | ||
return token.type; | ||
}; | ||
tokenize = function* (text) { | ||
let match; | ||
while (match = jsTokens.default.exec(text)) { | ||
const token = jsTokens.matchToToken(match); | ||
yield { | ||
type: getTokenType(token, match.index, text), | ||
value: token.value | ||
}; | ||
} | ||
}; | ||
} | ||
function highlight(text) { | ||
if (text === "") return ""; | ||
const defs = getDefs(true); | ||
let highlighted = ""; | ||
for (const { | ||
type, | ||
value | ||
} of tokenize(text)) { | ||
if (type in defs) { | ||
highlighted += value.split(NEWLINE$1).map(str => defs[type](str)).join("\n"); | ||
} else { | ||
highlighted += value; | ||
} | ||
} | ||
return highlighted; | ||
} | ||
let deprecationWarningShown = false; | ||
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/; | ||
@@ -89,8 +149,4 @@ function getMarkerLines(loc, source, opts) { | ||
function codeFrameColumns(rawLines, loc, opts = {}) { | ||
const highlighted = (opts.highlightCode || opts.forceColor) && (0, _highlight.shouldHighlight)(opts); | ||
const colors = getColors(opts.forceColor); | ||
const defs = getDefs(colors); | ||
const maybeHighlight = (fmt, string) => { | ||
return highlighted ? fmt(string) : string; | ||
}; | ||
const shouldHighlight = opts.forceColor || isColorSupported() && opts.highlightCode; | ||
const defs = getDefs(shouldHighlight); | ||
const lines = rawLines.split(NEWLINE); | ||
@@ -104,3 +160,3 @@ const { | ||
const numberMaxWidth = String(end).length; | ||
const highlightedLines = highlighted ? (0, _highlight.default)(rawLines, opts) : rawLines; | ||
const highlightedLines = shouldHighlight ? highlight(rawLines) : rawLines; | ||
let frame = highlightedLines.split(NEWLINE, end).slice(start, end).map((line, index) => { | ||
@@ -117,10 +173,10 @@ const number = start + 1 + index; | ||
const numberOfMarkers = hasMarker[1] || 1; | ||
markerLine = ["\n ", maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), " ", markerSpacing, maybeHighlight(defs.marker, "^").repeat(numberOfMarkers)].join(""); | ||
markerLine = ["\n ", defs.gutter(gutter.replace(/\d/g, " ")), " ", markerSpacing, defs.marker("^").repeat(numberOfMarkers)].join(""); | ||
if (lastMarkerLine && opts.message) { | ||
markerLine += " " + maybeHighlight(defs.message, opts.message); | ||
markerLine += " " + defs.message(opts.message); | ||
} | ||
} | ||
return [maybeHighlight(defs.marker, ">"), maybeHighlight(defs.gutter, gutter), line.length > 0 ? ` ${line}` : "", markerLine].join(""); | ||
return [defs.marker(">"), defs.gutter(gutter), line.length > 0 ? ` ${line}` : "", markerLine].join(""); | ||
} else { | ||
return ` ${maybeHighlight(defs.gutter, gutter)}${line.length > 0 ? ` ${line}` : ""}`; | ||
return ` ${defs.gutter(gutter)}${line.length > 0 ? ` ${line}` : ""}`; | ||
} | ||
@@ -131,4 +187,4 @@ }).join("\n"); | ||
} | ||
if (highlighted) { | ||
return colors.reset(frame); | ||
if (shouldHighlight) { | ||
return defs.reset(frame); | ||
} else { | ||
@@ -138,3 +194,3 @@ return frame; | ||
} | ||
function _default(rawLines, lineNumber, colNumber, opts = {}) { | ||
function index (rawLines, lineNumber, colNumber, opts = {}) { | ||
if (!deprecationWarningShown) { | ||
@@ -161,2 +217,5 @@ deprecationWarningShown = true; | ||
exports.codeFrameColumns = codeFrameColumns; | ||
exports.default = index; | ||
exports.highlight = highlight; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@babel/code-frame", | ||
"version": "7.25.9", | ||
"version": "7.26.0", | ||
"description": "Generate errors that contain a code frame that point to source locations.", | ||
@@ -19,3 +19,4 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"dependencies": { | ||
"@babel/highlight": "^7.25.9", | ||
"@babel/helper-validator-identifier": "^7.25.9", | ||
"js-tokens": "^4.0.0", | ||
"picocolors": "^1.0.0" | ||
@@ -22,0 +23,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33142
209
3
+ Addedjs-tokens@^4.0.0
- Removed@babel/highlight@^7.25.9
- Removed@babel/highlight@7.25.9(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedsupports-color@5.5.0(transitive)