Skip to content

Commit 9d00269

Browse files
authored
Modernize foresta-js library with NPM build process, latest esprima, comprehensive tests, and CI/CD (#2)
1 parent 02bd6be commit 9d00269

File tree

9 files changed

+5565
-6
lines changed

9 files changed

+5565
-6
lines changed

.github/workflows/ci.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [14.x, 16.x, 18.x, 20.x]
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Use Node.js ${{ matrix.node-version }}
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
cache: 'npm'
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Run tests
31+
run: npm test
32+
33+
- name: Run tests with coverage
34+
run: npm run test:coverage
35+
36+
- name: Upload coverage to Codecov
37+
if: matrix.node-version == '20.x'
38+
uses: codecov/codecov-action@v3
39+
with:
40+
file: ./coverage/lcov.info
41+
flags: unittests
42+
name: codecov-umbrella
43+
fail_ci_if_error: false

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Dependencies
2+
node_modules/
3+
npm-debug.log*
4+
yarn-debug.log*
5+
yarn-error.log*
6+
7+
# Build outputs
8+
dist/
9+
build/
10+
*.tgz
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Coverage directory used by tools like istanbul
19+
coverage/
20+
.nyc_output
21+
22+
# IDE files
23+
.vscode/
24+
.idea/
25+
*.swp
26+
*.swo
27+
*~
28+
29+
# OS generated files
30+
.DS_Store
31+
.DS_Store?
32+
._*
33+
.Spotlight-V100
34+
.Trashes
35+
ehthumbs.db
36+
Thumbs.db
37+
38+
# Environment variables
39+
.env
40+
.env.local
41+
.env.development.local
42+
.env.test.local
43+
.env.production.local
44+
45+
# Test artifacts
46+
test-results/

examples/basic.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const foresta = require('../dist/foresta');
2+
const esprima = require('esprima');
3+
4+
// Example code to analyze
5+
const code = `
6+
var theValue = 4 + 2;
7+
var config = {
8+
update: function() {
9+
var s = "hello";
10+
return s;
11+
},
12+
data: [1, 2, 3]
13+
};
14+
`;
15+
16+
console.log('=== Foresta.js Examples ===\n');
17+
18+
// Parse the code with esprima
19+
const ast = esprima.parseScript(code);
20+
21+
// Example 1: Find all literal values
22+
console.log('1. All literal values:');
23+
const literalQuery = new foresta("Literal");
24+
literalQuery.visit(ast);
25+
literalQuery.results.forEach((result, i) => {
26+
console.log(` ${i + 1}. ${JSON.stringify(result.value)} (${typeof result.value})`);
27+
});
28+
29+
// Example 2: Find specific variable by name
30+
console.log('\n2. Variable named "theValue":');
31+
const identifierQuery = new foresta("#theValue");
32+
identifierQuery.visit(ast);
33+
if (identifierQuery.results.length > 0) {
34+
const result = identifierQuery.results[0];
35+
console.log(` Found: ${result.name}`);
36+
console.log(` Initialization: ${result.parent.init.type} (${result.parent.init.operator})`);
37+
}
38+
39+
// Example 3: Find all global variables
40+
console.log('\n3. All global variable declarations:');
41+
const globalVarsQuery = new foresta("Program VariableDeclaration VariableDeclarator");
42+
globalVarsQuery.visit(ast);
43+
globalVarsQuery.results.forEach((result, i) => {
44+
console.log(` ${i + 1}. ${result.id.name}`);
45+
});
46+
47+
// Example 4: Use property modifiers to find function expression
48+
console.log('\n4. Function expression using property modifier:');
49+
const functionQuery = new foresta("ObjectExpression Property #update:parent:value");
50+
functionQuery.visit(ast);
51+
if (functionQuery.results.length > 0) {
52+
const result = functionQuery.results[0];
53+
console.log(` Found: ${result.type} with ${result.params.length} parameters`);
54+
}
55+
56+
// Example 5: Find all binary expressions
57+
console.log('\n5. All binary expressions:');
58+
const binaryQuery = new foresta("BinaryExpression");
59+
binaryQuery.visit(ast);
60+
binaryQuery.results.forEach((result, i) => {
61+
console.log(` ${i + 1}. ${result.left.value || result.left.name} ${result.operator} ${result.right.value || result.right.name}`);
62+
});
63+
64+
console.log('\n=== End Examples ===');

jest.config.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"testEnvironment": "node",
3+
"testMatch": [
4+
"**/test/**/*.test.js",
5+
"**/test/**/*.spec.js"
6+
],
7+
"collectCoverageFrom": [
8+
"src/**/*.js",
9+
"!src/**/*.test.js"
10+
],
11+
"coverageDirectory": "coverage",
12+
"coverageReporters": [
13+
"text",
14+
"lcov",
15+
"html"
16+
]
17+
}

0 commit comments

Comments
 (0)