Skip to content

Commit

Permalink
AG-35801 Fix last position in position provider
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/tsurlfilter from fix/AG-35801 to master

Squashed commit of the following:

commit 8048333
Author: scripthunter7 <[email protected]>
Date:   Tue Sep 10 11:50:59 2024 +0200

    fix nit

commit bd365fb
Author: scripthunter7 <[email protected]>
Date:   Mon Sep 9 19:07:07 2024 +0200

    fix

commit cffdc60
Author: scripthunter7 <[email protected]>
Date:   Mon Sep 9 18:55:57 2024 +0200

    smoke tests

commit 606eec5
Author: scripthunter7 <[email protected]>
Date:   Mon Sep 9 18:42:34 2024 +0200

    finalize builds

commit 23fc68f
Author: scripthunter7 <[email protected]>
Date:   Mon Sep 9 15:33:17 2024 +0200

    remove browser builds

commit ea9dda1
Author: scripthunter7 <[email protected]>
Date:   Mon Sep 9 15:33:00 2024 +0200

    update exports

commit 9718065
Author: scripthunter7 <[email protected]>
Date:   Mon Sep 9 10:08:51 2024 +0200

    changelog

commit 87704d0
Author: scripthunter7 <[email protected]>
Date:   Mon Sep 9 10:06:11 2024 +0200

    fix nits

commit 9b64888
Author: scripthunter7 <[email protected]>
Date:   Fri Sep 6 18:44:47 2024 +0200

    fix nit

commit 559b996
Author: scripthunter7 <[email protected]>
Date:   Fri Sep 6 18:13:12 2024 +0200

    fix position provider
  • Loading branch information
scripthunter7 committed Sep 10, 2024
1 parent eade72c commit b2df551
Show file tree
Hide file tree
Showing 24 changed files with 408 additions and 152 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
npx lerna run --scope @adguard/agtree lint
npx lerna run --scope @adguard/agtree test
npx lerna run --scope @adguard/agtree build
npx lerna run --scope @adguard/agtree test:smoke
# tsurlfilter
npx lerna run --scope @adguard/tsurlfilter lint
Expand Down
3 changes: 3 additions & 0 deletions bamboo-specs/scripts/agtree-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ pnpm --filter @adguard/agtree lint
# Run tests with Jest
pnpm --filter @adguard/agtree test

# Run smoke tests
pnpm --filter @adguard/agtree test:smoke

echo "@adguard/agtree tests completed"
4 changes: 3 additions & 1 deletion packages/agtree/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
dist
dist
coverage
test/smoke
2 changes: 1 addition & 1 deletion packages/agtree/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ node_modules
.eslintcache

# Release file for npm.
agtree.tgz
*.tgz
13 changes: 13 additions & 0 deletions packages/agtree/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ The format is based on [Keep a Changelog][keepachangelog], and this project adhe
[keepachangelog]: https://keepachangelog.com/en/1.0.0/
[semver]: https://semver.org/spec/v2.0.0.html

## [2.0.2] - 2024-09-10

### Fixed

- Handling last position in `PositionProvider`.

### Removed

- Browser-specific builds. If you need to use AGTree in browser environment, you can use jsDelivr's automatic builds
or bundlers like Webpack / Rollup / ESBuild without any issues, AGTree does not have any non-browser-compatible code.

[2.0.2]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/agtree-v2.0.2

## [2.0.1] - 2024-09-06

### Added
Expand Down
4 changes: 2 additions & 2 deletions packages/agtree/browser-benchmark/helpers/build-iife.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import { rollup } from 'rollup';

import { browserPlugins } from '../../rollup.config';
import { commonPlugins } from '../../rollup.config';

/**
* Create an IIFE bundle for the browser environment.
Expand All @@ -20,7 +20,7 @@ import { browserPlugins } from '../../rollup.config';
export const buildIife = async (input: string, name: string): Promise<string> => {
const bundle = await rollup({
input,
plugins: browserPlugins,
plugins: commonPlugins,
});

const { output } = await bundle.generate({
Expand Down
23 changes: 9 additions & 14 deletions packages/agtree/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adguard/agtree",
"version": "2.0.1",
"version": "2.0.2",
"description": "Universal adblock filter list parser.",
"keywords": [
"adblock",
Expand All @@ -20,19 +20,15 @@
"url": "https://github.com/AdguardTeam/tsurlfilter/issues"
},
"homepage": "https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme",
"main": "dist/agtree.cjs",
"module": "dist/agtree.esm.js",
"browser": "dist/agtree.umd.min.js",
"main": "dist/agtree.js",
"module": "dist/agtree.mjs",
"types": "dist/agtree.d.ts",
"exports": {
".": {
"types": "./dist/agtree.d.ts",
"import": "./dist/agtree.esm.js",
"require": "./dist/agtree.cjs"
},
"./es": "./dist/agtree.esm.js",
"./iife": "./dist/agtree.iife.min.js",
"./umd": "./dist/agtree.umd.min.js"
"import": "./dist/agtree.mjs",
"require": "./dist/agtree.js"
}
},
"files": [
"dist"
Expand All @@ -55,7 +51,8 @@
"lint:md": "markdownlint .",
"lint:ts": "eslint . --cache --ext .ts",
"precommit": "pnpm check-types && pnpm lint && pnpm test",
"test": "jest --runInBand"
"test": "jest --runInBand",
"test:smoke": "(cd test/smoke/esm && pnpm test) && (cd test/smoke/cjs && pnpm test) && (cd test/smoke/typescript && pnpm test)"
},
"devDependencies": {
"@babel/core": "^7.22.5",
Expand All @@ -65,9 +62,8 @@
"@rollup/plugin-commonjs": "^25.0.2",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.2",
"@swc/core": "^1.3.100",
"@swc/core": "^1.7.24",
"@swc/helpers": "^0.5.3",
"@swc/jest": "^0.2.29",
"@types/benchmark": "^2.1.5",
Expand Down Expand Up @@ -108,7 +104,6 @@
"rollup": "^3.25.3",
"rollup-plugin-dts": "^5.3.0",
"rollup-plugin-node-externals": "^6.1.1",
"rollup-plugin-polyfill-node": "^0.12.0",
"systeminformation": "^5.22.0",
"ts-node": "^10.9.2",
"tsx": "^4.7.3",
Expand Down
89 changes: 4 additions & 85 deletions packages/agtree/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import externals from 'rollup-plugin-node-externals';
import dtsPlugin from 'rollup-plugin-dts';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import alias from '@rollup/plugin-alias';
import { getBabelOutputPlugin } from '@rollup/plugin-babel';
import json from '@rollup/plugin-json';
import terser from '@rollup/plugin-terser';
import path from 'node:path';
import { readFileSync } from 'node:fs';
import { type Plugin } from 'rollup';
Expand Down Expand Up @@ -64,7 +62,7 @@ const typeScriptPlugin = typescript({
});

// Common plugins for all types of builds
const commonPlugins = [
export const commonPlugins = [
alias({
entries: [
// replace dynamic compatibility table data builder with the pre-built data file
Expand Down Expand Up @@ -121,61 +119,12 @@ export const getNodePlugins = (esm = false): Plugin[] => [
}),
];

// Plugins for browser builds
export const browserPlugins = [
...commonPlugins,
nodePolyfills(),
// The build of CSSTree is a bit complicated (patches, require "emulation", etc.),
// so here we only specify the pre-built version by an alias
alias({
entries: [
{
find: '@adguard/ecss-tree',
replacement: path.resolve(
'node_modules/@adguard/ecss-tree/dist/ecsstree.umd.min.js',
),
},
],
}),
// Provide better browser compatibility with Babel
getBabelOutputPlugin({
presets: [
[
'@babel/preset-env',
{
targets: {
browsers: [
'chrome >= 80',
'firefox >= 78',
'edge >= 80',
'opera >= 67',
'safari >= 14',
],
},
},
],
],
allowAllFormats: true,
compact: false,
}),
// Minify the output with Terser
terser({
output: {
// Keep the banner in the minified output
preamble: banner,
},
compress: {
passes: 3,
},
}),
];

// CommonJS build configuration
const cjs = {
input: path.join(ROOT_DIR, 'src', 'index.ts'),
output: [
{
file: path.join(distDir, `${BASE_FILE_NAME}.cjs`),
file: path.join(distDir, `${BASE_FILE_NAME}.js`),
format: 'cjs',
exports: 'auto',
sourcemap: false,
Expand All @@ -190,7 +139,7 @@ const esm = {
input: path.join(ROOT_DIR, 'src', 'index.ts'),
output: [
{
file: path.join(distDir, `${BASE_FILE_NAME}.esm.js`),
file: path.join(distDir, `${BASE_FILE_NAME}.mjs`),
format: 'esm',
sourcemap: false,
banner,
Expand All @@ -199,36 +148,6 @@ const esm = {
plugins: getNodePlugins(),
};

// Browser-friendly UMD build configuration
const umd = {
input: path.join(ROOT_DIR, 'src', 'index.ts'),
output: [
{
file: path.join(distDir, `${BASE_FILE_NAME}.umd.min.js`),
name: BASE_NAME,
format: 'umd',
sourcemap: false,
banner,
},
],
plugins: browserPlugins,
};

// Browser-friendly IIFE build configuration
const iife = {
input: path.join(ROOT_DIR, 'src', 'index.ts'),
output: [
{
file: path.join(distDir, `${BASE_FILE_NAME}.iife.min.js`),
name: BASE_NAME,
format: 'iife',
sourcemap: false,
banner,
},
],
plugins: browserPlugins,
};

// Merge .d.ts files (requires `tsc` to be run first,
// because it merges .d.ts files from `dist/types` directory)
const dts = {
Expand All @@ -247,4 +166,4 @@ const dts = {
};

// Export build configs for Rollup
export default [cjs, esm, umd, iife, dts];
export default [cjs, esm, dts];
7 changes: 5 additions & 2 deletions packages/agtree/src/utils/position-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ export class PositionProvider {
? i + 2
: i + 1;

if (sourceCode[i] === CR) {
if (sourceCode[i] === CR && sourceCode[i + 1] === LF) {
// Skip the '\n' in a '\r\n' sequence
i += 1;
}
}
}

// Handle the case where the last offset is at the end of the source code
this.offsetToLine[sourceCode.length] = currentLine;
}

/**
Expand All @@ -72,7 +75,7 @@ export class PositionProvider {
* @returns A Position object containing the 1-based line and column number, or null if the offset is out of range.
*/
convertOffsetToPosition(offset: number): Position | null {
if (offset < 0 || offset >= this.offsetToLine.length) {
if (offset < 0 || offset > this.offsetToLine.length - 1) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/agtree/src/utils/type-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const isNull = (value: unknown): value is null => {
* @returns `true` if the value is a number, `false` otherwise.
*/
export const isNumber = (value: unknown): value is number => {
return typeof value === 'number';
return typeof value === 'number' && !Number.isNaN(value);
};

/**
Expand All @@ -39,7 +39,7 @@ export const isNumber = (value: unknown): value is number => {
* @returns `true` if the value is an integer, `false` otherwise.
*/
export const isInteger = (value: unknown): value is number => {
return isNumber(value) && Number.isInteger(value);
return Number.isInteger(value);
};

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/agtree/test/smoke/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { RuleParser } = require('@adguard/agtree');
const { ok } = require('assert');

const ruleNode = RuleParser.parse('||example.com^');

ok(ruleNode);

console.log('Smoke test passed');
11 changes: 11 additions & 0 deletions packages/agtree/test/smoke/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "cjs",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"author": "Adguard Software Ltd.",
"scripts": {
"start": "node index.js",
"test": "./test.sh"
}
}
28 changes: 28 additions & 0 deletions packages/agtree/test/smoke/cjs/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

set -e # Exit on error

# pack @adguard/agtree
curr_path="test/smoke/cjs"
agtree="agtree.tgz"
nm_path="node_modules"

# Define cleanup function
cleanup() {
echo "Cleaning up..."
rm -f $agtree && rm -rf $nm_path
echo "Cleanup complete"
}

# Set trap to execute the cleanup function on script exit
trap cleanup EXIT

(cd ../../.. && pnpm pack && mv adguard-agtree-*.tgz "$curr_path/$agtree")

# unzip to @adguard/tsurlfilter to node_modules
agtree_node_modules=$nm_path"/@adguard/agtree"
mkdir -p $agtree_node_modules
tar -xzf $agtree --strip-components=1 -C $agtree_node_modules

pnpm start
echo "Test successfully built."
8 changes: 8 additions & 0 deletions packages/agtree/test/smoke/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RuleParser } from '@adguard/agtree';
import { ok } from 'assert';

const ruleNode = RuleParser.parse('||example.com^');

ok(ruleNode);

console.log('Smoke test passed');
12 changes: 12 additions & 0 deletions packages/agtree/test/smoke/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "esm",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"author": "Adguard Software Ltd.",
"type": "module",
"scripts": {
"start": "node index.js",
"test": "./test.sh"
}
}
Loading

0 comments on commit b2df551

Please sign in to comment.