Skip to content

Enable the unicorn/prefer-at ESLint plugin rule (PR 15008 follow-up) #15014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"unicorn/no-new-buffer": "error",
"unicorn/no-instanceof-array": "error",
"unicorn/no-useless-spread": "error",
"unicorn/prefer-at": "error",
"unicorn/prefer-date-now": "error",
"unicorn/prefer-dom-node-remove": "error",
"unicorn/prefer-string-starts-ends-with": "error",
Expand Down
4 changes: 4 additions & 0 deletions external/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
"env": {
"node": true,
},

"rules": {
"unicorn/prefer-at": "off",
},
}
2 changes: 1 addition & 1 deletion src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ class Catalog {
}

while (queue.length > 0) {
const queueItem = queue[queue.length - 1];
const queueItem = queue.at(-1);
const { currentNode, posInKids } = queueItem;

let kids = currentNode.getRaw("Kids");
Expand Down
4 changes: 2 additions & 2 deletions src/core/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ class AESBaseCipher {
let outputLength = 16 * result.length;
if (finalize) {
// undo a padding that is described in RFC 2898
const lastBlock = result[result.length - 1];
const lastBlock = result.at(-1);
let psLen = lastBlock[15];
if (psLen <= 16) {
for (let i = 15, ii = 16 - psLen; i >= ii; --i) {
Expand Down Expand Up @@ -1284,7 +1284,7 @@ const PDF20 = (function PDF20Closure() {
let k = calculateSHA256(input, 0, input.length).subarray(0, 32);
let e = [0];
let i = 0;
while (i < 64 || e[e.length - 1] > i - 32) {
while (i < 64 || e.at(-1) > i - 32) {
const combinedLength = password.length + k.length + userBytes.length,
combinedArray = new Uint8Array(combinedLength);
let writeOffset = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ class PDFDocument {
const pdfFonts = [];
const initialState = {
get font() {
return pdfFonts[pdfFonts.length - 1];
return pdfFonts.at(-1);
},
set font(font) {
pdfFonts.push(font);
Expand Down
2 changes: 1 addition & 1 deletion src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3066,7 +3066,7 @@ class PartialEvaluator {
}
}

const item = elements[elements.length - 1];
const item = elements.at(-1);
if (typeof item === "string") {
showSpacedTextBuffer.push(item);
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/font_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ function compileGlyf(code, cmds, font) {
}
const instructionLength = getUint16(code, i);
i += 2 + instructionLength; // skipping the instructions
const numberOfPoints = endPtsOfContours[endPtsOfContours.length - 1] + 1;
const numberOfPoints = endPtsOfContours.at(-1) + 1;
const points = [];
while (points.length < numberOfPoints) {
flags = code[i++];
Expand Down Expand Up @@ -329,15 +329,15 @@ function compileGlyf(code, cmds, font) {
const contour = points.slice(startPoint, endPoint + 1);
if (contour[0].flags & 1) {
contour.push(contour[0]); // using start point at the contour end
} else if (contour[contour.length - 1].flags & 1) {
} else if (contour.at(-1).flags & 1) {
// first is off-curve point, trying to use one from the end
contour.unshift(contour[contour.length - 1]);
contour.unshift(contour.at(-1));
} else {
// start and end are off-curve points, creating implicit one
const p = {
flags: 1,
x: (contour[0].x + contour[contour.length - 1].x) / 2,
y: (contour[0].y + contour[contour.length - 1].y) / 2,
x: (contour[0].x + contour.at(-1).x) / 2,
y: (contour[0].y + contour.at(-1).y) / 2,
};
contour.unshift(p);
contour.push(p);
Expand Down
6 changes: 3 additions & 3 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ function getRanges(glyphs, numGlyphs) {

function createCmapTable(glyphs, numGlyphs) {
const ranges = getRanges(glyphs, numGlyphs);
const numTables = ranges[ranges.length - 1][1] > 0xffff ? 2 : 1;
const numTables = ranges.at(-1)[1] > 0xffff ? 2 : 1;
let cmap =
"\x00\x00" + // version
string16(numTables) + // numTables
Expand Down Expand Up @@ -2291,7 +2291,7 @@ class Font {
// CALL
if (!inFDEF && !inELSE) {
// collecting information about which functions are used
funcId = stack[stack.length - 1];
funcId = stack.at(-1);
if (isNaN(funcId)) {
info("TT: CALL empty stack (or invalid entry).");
} else {
Expand Down Expand Up @@ -2374,7 +2374,7 @@ class Font {
} else if (op === 0x1c) {
// JMPR
if (!inFDEF && !inELSE) {
const offset = stack[stack.length - 1];
const offset = stack.at(-1);
// only jumping forward to prevent infinite loop
if (offset > 0) {
i += offset - 1;
Expand Down
2 changes: 1 addition & 1 deletion src/core/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
i += 6;
break;
}
ast1 = stack[stack.length - 1];
ast1 = stack.at(-1);
if (ast1.type === "literal" || ast1.type === "var") {
// we don't have to save into intermediate variable a literal or
// variable.
Expand Down
2 changes: 1 addition & 1 deletion src/core/operator_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function addState(parentState, pattern, checkFn, iterateFn, processFn) {
const item = pattern[i];
state = state[item] || (state[item] = []);
}
state[pattern[pattern.length - 1]] = {
state[pattern.at(-1)] = {
checkFn,
iterateFn,
processFn,
Expand Down
6 changes: 3 additions & 3 deletions src/core/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class RadialAxialShading extends BaseShading {
}
if (!extendEnd) {
// Same idea as above in extendStart but for the end.
colorStops[colorStops.length - 1][0] -= BaseShading.SMALL_NUMBER;
colorStops.at(-1)[0] -= BaseShading.SMALL_NUMBER;
colorStops.push([1, background]);
}

Expand Down Expand Up @@ -501,11 +501,11 @@ class MeshShading extends BaseShading {
verticesLeft = 3;
break;
case 1:
ps.push(ps[ps.length - 2], ps[ps.length - 1]);
ps.push(ps.at(-2), ps.at(-1));
verticesLeft = 1;
break;
case 2:
ps.push(ps[ps.length - 3], ps[ps.length - 1]);
ps.push(ps.at(-3), ps.at(-1));
verticesLeft = 1;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/type1_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ const Type1CharString = (function Type1CharStringClosure() {
// seac is like type 2's special endchar but it doesn't use the
// first argument asb, so remove it.
if (seacAnalysisEnabled) {
const asb = this.stack[this.stack.length - 5];
const asb = this.stack.at(-5);
this.seac = this.stack.splice(-4, 4);
this.seac[0] += this.lsb - asb;
error = this.executeCommand(0, COMMAND_MAP.endchar);
Expand Down
2 changes: 1 addition & 1 deletion src/core/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function incrementalUpdate({
const refForXrefTable = xrefInfo.newRef;

let buffer, baseOffset;
const lastByte = originalData[originalData.length - 1];
const lastByte = originalData.at(-1);
if (lastByte === /* \n */ 0x0a || lastByte === /* \r */ 0x0d) {
buffer = [];
baseOffset = originalData.length;
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class Builder {
}
const prefixStack = this._namespacePrefixes.get(prefix);
if (prefixStack && prefixStack.length > 0) {
return prefixStack[prefixStack.length - 1];
return prefixStack.at(-1);
}

warn(`Unknown namespace prefix: ${prefix}.`);
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DataHandler {
const stack = [[-1, this.data[$getChildren]()]];

while (stack.length > 0) {
const last = stack[stack.length - 1];
const last = stack.at(-1);
const [i, children] = last;
if (i + 1 === children.length) {
stack.pop();
Expand Down
4 changes: 2 additions & 2 deletions src/core/xfa/formcalc_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class SimpleExprParser {
return [tok, this.getNode()];
case TOKEN.leftParen:
if (this.last === OPERAND) {
const lastOperand = this.operands[this.operands.length - 1];
const lastOperand = this.operands.at(-1);
if (!(lastOperand instanceof AstIdentifier)) {
return [tok, this.getNode()];
}
Expand Down Expand Up @@ -525,7 +525,7 @@ class SimpleExprParser {

flushWithOperator(op) {
while (true) {
const top = this.operators[this.operators.length - 1];
const top = this.operators.at(-1);
if (top) {
if (top.id >= 0 && SimpleExprParser.checkPrecedence(top, op)) {
this.operators.pop();
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/html_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ function isPrintOnly(node) {

function getCurrentPara(node) {
const stack = node[$getTemplateRoot]()[$extra].paraStack;
return stack.length ? stack[stack.length - 1] : null;
return stack.length ? stack.at(-1) : null;
}

function setPara(node, nodeStyle, value) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/som.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function parseExpression(expr, dotDotAllowed, noExpr = true) {
warn("XFA - Invalid index in SOM expression");
return null;
}
parsed[parsed.length - 1].index = parseIndex(match[0]);
parsed.at(-1).index = parseIndex(match[0]);
pos += match[0].length + 1;
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/xfa/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ class Border extends XFAObject {
if (!this[$extra]) {
const edges = this.edge.children.slice();
if (edges.length < 4) {
const defaultEdge = edges[edges.length - 1] || new Edge({});
const defaultEdge = edges.at(-1) || new Edge({});
for (let i = edges.length; i < 4; i++) {
edges.push(defaultEdge);
}
Expand Down Expand Up @@ -950,7 +950,7 @@ class Border extends XFAObject {
if (this.corner.children.some(node => node.radius !== 0)) {
const cornerStyles = this.corner.children.map(node => node[$toStyle]());
if (cornerStyles.length === 2 || cornerStyles.length === 3) {
const last = cornerStyles[cornerStyles.length - 1];
const last = cornerStyles.at(-1);
for (let i = cornerStyles.length; i < 4; i++) {
cornerStyles.push(last);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/xfa/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class FontSelector {
}

pushData(xfaFont, margin, lineHeight) {
const lastFont = this.stack[this.stack.length - 1];
const lastFont = this.stack.at(-1);
for (const name of [
"typeface",
"posture",
Expand Down Expand Up @@ -139,7 +139,7 @@ class FontSelector {
}

topFont() {
return this.stack[this.stack.length - 1];
return this.stack.at(-1);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/xhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ class P extends XhtmlObject {

[$text]() {
const siblings = this[$getParent]()[$getChildren]();
if (siblings[siblings.length - 1] === this) {
if (siblings.at(-1) === this) {
return super[$text]();
}
return super[$text]() + "\n";
Expand Down
5 changes: 1 addition & 4 deletions src/core/xfa_fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,7 @@ function getXfaFontDict(name) {
dict.set("CIDToGIDMap", Name.get("Identity"));
dict.set("W", widths);
dict.set("FirstChar", widths[0]);
dict.set(
"LastChar",
widths[widths.length - 2] + widths[widths.length - 1].length - 1
);
dict.set("LastChar", widths.at(-2) + widths.at(-1).length - 1);
const descriptor = new Dict(null);
dict.set("FontDescriptor", descriptor);
const systemInfo = new Dict(null);
Expand Down
2 changes: 1 addition & 1 deletion src/core/xml_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ class SimpleXMLParser extends XMLParserBase {

onEndElement(name) {
this._currentFragment = this._stack.pop() || [];
const lastElement = this._currentFragment[this._currentFragment.length - 1];
const lastElement = this._currentFragment.at(-1);
if (!lastElement) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/display/editor/fit_curve/fit_curve.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function generateBezier(points, parameters, leftTangent, rightTangent) {
ux;

const firstPoint = points[0];
const lastPoint = points[points.length - 1];
const lastPoint = points.at(-1);

// Bezier curve ctl pts
const bezCurve = [firstPoint, null, null, lastPoint];
Expand Down
2 changes: 1 addition & 1 deletion src/display/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ if (
if (opListElement.fn === "save") {
opTree.push({ fnId: 92, fn: "group", items: [] });
tmp.push(opTree);
opTree = opTree[opTree.length - 1].items;
opTree = opTree.at(-1).items;
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/display/text_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ function expandBoundsLTR(width, bounds) {
affectedBoundary.x2 > boundary.x2 ? affectedBoundary : boundary;
if (lastBoundary === useBoundary) {
// Merging with previous.
changedHorizon[changedHorizon.length - 1].end = horizonPart.end;
changedHorizon.at(-1).end = horizonPart.end;
} else {
changedHorizon.push({
start: horizonPart.start,
Expand All @@ -507,7 +507,7 @@ function expandBoundsLTR(width, bounds) {
});
}
if (boundary.y2 < horizon[j].end) {
changedHorizon[changedHorizon.length - 1].end = boundary.y2;
changedHorizon.at(-1).end = boundary.y2;
changedHorizon.push({
start: boundary.y2,
end: horizon[j].end,
Expand Down
4 changes: 2 additions & 2 deletions src/display/xfa_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ class XfaLayer {
const textDivs = [];

while (stack.length > 0) {
const [parent, i, html] = stack[stack.length - 1];
const [parent, i, html] = stack.at(-1);
if (i + 1 === parent.children.length) {
stack.pop();
continue;
}

const child = parent.children[++stack[stack.length - 1][1]];
const child = parent.children[++stack.at(-1)[1]];
if (child === null) {
continue;
}
Expand Down
8 changes: 8 additions & 0 deletions src/shared/compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ if (
require("core-js/es/array/at.js");
})();

// Support: Firefox<90, Chrome<92, Safari<15.4, Node.js<16.6.0
(function checkTypedArrayAt() {
if (Uint8Array.prototype.at) {
return;
}
require("core-js/es/typed-array/at.js");
})();

// Support: Firefox<94, Chrome<98, Safari<15.4, Node.js<17.0.0
(function checkStructuredClone() {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("IMAGE_DECODERS")) {
Expand Down
2 changes: 1 addition & 1 deletion test/resources/reftest-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ window.onload = function () {
/^ {2}IMAGE[^:]*\((\d+\.?\d*)x(\d+\.?\d*)x(\d+\.?\d*)\): (.*)$/
);
if (match) {
const item = gTestItems[gTestItems.length - 1];
const item = gTestItems.at(-1);
item.images.push({
width: parseFloat(match[1]),
height: parseFloat(match[2]),
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-var */
/* eslint-disable no-var, unicorn/prefer-at */

"use strict";

Expand Down
2 changes: 1 addition & 1 deletion test/unit/ui_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe("ui_utils", function () {
ids.add(view.id);
}
}
return { first: views[0], last: views[views.length - 1], views, ids };
return { first: views[0], last: views.at(-1), views, ids };
}

// This function takes a fixed layout of pages and compares the system under
Expand Down
2 changes: 1 addition & 1 deletion web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ function getVisibleElements({
}

const first = visible[0],
last = visible[visible.length - 1];
last = visible.at(-1);

if (sortByVisibility) {
visible.sort(function (a, b) {
Expand Down