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

JS-159 Send real AST using protobufjs #4716

Merged
merged 66 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
c9da18a
initial scaffold
kebetsi May 31, 2024
f62baab
try something with protobufjs
kebetsi May 31, 2024
439e440
fix end location
kebetsi Jun 3, 2024
edc866e
dynamic serialization - first attempt
kebetsi Jun 4, 2024
131ac98
test for non null keys
kebetsi Jun 4, 2024
7853327
use res instead of node
kebetsi Jun 4, 2024
4808d49
refactor function name for clarity
kebetsi Jun 4, 2024
7131a64
Merge branch 'master' into JS-159-send-real-AST-using-protobuf
kebetsi Jun 4, 2024
fbc9dda
fix merge
kebetsi Jun 4, 2024
db6b651
Start implementation
quentin-jaquier-sonarsource Jun 5, 2024
c56d1d6
Use correct serializer
quentin-jaquier-sonarsource Jun 5, 2024
a9a2ff9
Start to handle literals
quentin-jaquier-sonarsource Jun 5, 2024
f065e65
Finish the visitor implementation
quentin-jaquier-sonarsource Jun 5, 2024
bf44d08
fix path
kebetsi Jun 5, 2024
e743969
Finish first draft
quentin-jaquier-sonarsource Jun 5, 2024
b2408a2
setup jest for tests
kebetsi Jun 5, 2024
45e1b9d
Merge branch 'qj/NewProtobuf' into JS-159-send-real-AST-using-protobuf
kebetsi Jun 5, 2024
39fe6fa
fix protobuf file
quentin-jaquier-sonarsource Jun 5, 2024
a9aa719
Handle function expression name conflict
quentin-jaquier-sonarsource Jun 5, 2024
d568762
Revert "fix protobuf file"
quentin-jaquier-sonarsource Jun 5, 2024
e1be9c8
Start autogeneration
quentin-jaquier-sonarsource Jun 5, 2024
0313e14
fix visitNode
kebetsi Jun 5, 2024
41241d3
cleanup and setup deserialize debug
kebetsi Jun 5, 2024
a0da501
ugly: return {node, type} in case the type and `type` prop are differ…
kebetsi Jun 5, 2024
695676c
Do not create intermediate messages
quentin-jaquier-sonarsource Jun 5, 2024
2b3a5e9
Clean up
quentin-jaquier-sonarsource Jun 5, 2024
8697a8d
implement ast compare (#4729)
ilia-kebets-sonarsource Jun 5, 2024
5b1c72b
Fix after rebase
quentin-jaquier-sonarsource Jun 5, 2024
b659c6a
add TS classes for protobuf
kebetsi Jun 5, 2024
b3c33d6
Merge branch 'qj/NewProtobuf' into JS-159-send-real-AST-using-protobuf
kebetsi Jun 5, 2024
0111b6a
Clean up
quentin-jaquier-sonarsource Jun 6, 2024
6740eda
fix ts compilation issues
kebetsi Jun 6, 2024
584a4e5
Correctly serialize type from enum
quentin-jaquier-sonarsource Jun 6, 2024
bcf5e95
simplify serialization, remove special classes, or 1-to-1 indirections
kebetsi Jun 6, 2024
705beb7
remove code that parses the intermediate messagremove code that parse…
kebetsi Jun 6, 2024
2bf6ff7
serializing works
kebetsi Jun 6, 2024
eff3326
cleanup
kebetsi Jun 6, 2024
c64baa0
make it work for typescript as well
kebetsi Jun 6, 2024
90a5e34
make extra literal props optional
kebetsi Jun 6, 2024
e954ca8
cleanup test
kebetsi Jun 6, 2024
7e2feaa
cleanup
kebetsi Jun 6, 2024
138f508
parse form data ast from HTTP works!
kebetsi Jun 6, 2024
94eec7e
fix test
kebetsi Jun 6, 2024
22dd084
handle null literal
kebetsi Jun 6, 2024
a532521
monkey patch TS AST serialization until we fix it
kebetsi Jun 7, 2024
de41da6
fix patch
kebetsi Jun 7, 2024
5f777ec
Fix Sonar issues
quentin-jaquier-sonarsource Jun 7, 2024
6999582
add copy of protobuf to bridge
kebetsi Jun 7, 2024
c0ee148
Merge branch 'JS-159-send-real-AST-using-protobuf' of github.com:Sona…
kebetsi Jun 7, 2024
2f6cd1e
adapt script
kebetsi Jun 7, 2024
df0acf5
Add further tests for coverage
quentin-jaquier-sonarsource Jun 7, 2024
a275774
Revert "adapt script"
kebetsi Jun 7, 2024
eadf408
copy the protofile to the lib
kebetsi Jun 7, 2024
6b3022e
Merge branch 'JS-159-send-real-AST-using-protobuf' of github.com:Sona…
kebetsi Jun 7, 2024
c9d38a8
also copy to packages/
kebetsi Jun 7, 2024
67c5fd6
Merge branch 'master' into JS-159-send-real-AST-using-protobuf
quentin-jaquier-sonarsource Jun 7, 2024
30bea5d
filter out other files that are currently unsupported
kebetsi Jun 7, 2024
5364a55
refactor check
kebetsi Jun 7, 2024
98f7572
Merge branch 'JS-159-send-real-AST-using-protobuf' of github.com:Sona…
kebetsi Jun 7, 2024
64dad2d
don't crash on unknown nodes
kebetsi Jun 7, 2024
ac5d94a
comment out assertion that will be fixed in next ticket
kebetsi Jun 7, 2024
fcd9304
try to fix sonar issues
kebetsi Jun 7, 2024
99b7dca
Revert "try to fix sonar issues"
kebetsi Jun 7, 2024
96458d9
remove comment
kebetsi Jun 7, 2024
61a0031
improve coverage
kebetsi Jun 7, 2024
2c9d121
remove unsupported statement
kebetsi Jun 10, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ private void assertAnalyzeJs(Bridge bridge) throws IOException, InterruptedExcep
JsonObject metrics = jsonObject.getAsJsonObject("metrics");
assertThat(metrics.entrySet()).hasSize(1);
assertThat(metrics.get("nosonarLines").getAsJsonArray()).containsExactly(new JsonPrimitive(3));
assertThat(parsedResponse.ast()).contains("plop");
// put back new assertion when we know how to parse this
//assertThat(parsedResponse.ast()).contains("plop");
}

private static BridgeResponse parseFormData(HttpResponse<String> response) {
Expand Down
74 changes: 62 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"ruling": "node tools/prepare-ruling.js && jest ./packages/ruling/tests/projects/*.ruling.test.ts",
"ruling-sync": "rsync -avh packages/ruling/tests/actual/jsts/ its/ruling/src/test/expected/jsts/ --delete",
"update-ruling-data": "mvn -f sonar-plugin/sonar-javascript-plugin/pom.xml compile && ts-node packages/ruling/tests/tools/parseRulesData.ts",
"bridge:compile": "tsc -b packages profiling",
"bridge:compile": "tsc -b packages profiling && npm run _:bridge:copy-protofile",
"bridge:test": "jest --maxWorkers=8 --coverage --testPathIgnorePatterns=/packages/ruling/tests/",
"bridge:build": "npm run bridge:build:fast && npm run bridge:test",
"bridge:build:fast": "npm ci && npm run _:bridge:clear && npm run bridge:compile",
Expand All @@ -24,6 +24,7 @@
"prepare": "husky install",
"precommit": "pretty-quick --staged",
"count-rules": "node tools/count-rules.js",
"_:bridge:copy-protofile": "node tools/estree/copy-to-lib.js",
"_:bridge:clear": "rimraf lib/*",
"_:plugin:prepare-bridge": "npm pack && node tools/check-distribution-filepath-length.js && npm run _:plugin:copy-bridge",
"_:plugin-fetch-node": "node tools/fetch-node/scripts/wrapper.mjs",
Expand Down Expand Up @@ -151,6 +152,7 @@
"postcss-scss",
"postcss-syntax",
"postcss-value-parser",
"protobufjs",
"run-node",
"semver",
"scslre",
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/src/errors/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ export const EMPTY_JSTS_ANALYSIS_OUTPUT: JsTsAnalysisOutput = {
cognitiveComplexity: 0,
},
cpdTokens: [],
ast: '',
ast: new Uint8Array(),
};
3 changes: 2 additions & 1 deletion packages/bridge/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ exports.delegate = function (worker, type) {
case 'success':
if (message.format === 'multipart') {
const fd = new formData();
fd.append('ast', message.result.ast);
const buf = Buffer.from(message.result.ast);
fd.append('ast', buf);
delete message.result.ast;
fd.append('json', JSON.stringify(message.result));
// this adds the boundary string that will be used to separate the parts
Expand Down
20 changes: 15 additions & 5 deletions packages/bridge/tests/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
*/
import { setContext, toUnixPath } from '@sonar/shared';
import http from 'http';
import { createAndSaveProgram, ProjectAnalysisInput, RuleConfig } from '@sonar/jsts';
import {
createAndSaveProgram,
deserializeProtobuf,
ProjectAnalysisInput,
RuleConfig,
} from '@sonar/jsts';
import path from 'path';
import { start } from '../src/server';
import { request } from './tools';
Expand Down Expand Up @@ -128,8 +133,12 @@ describe('router', () => {
message: `Use a regular expression literal instead of the 'RegExp' constructor.`,
}),
);
const ast = response.get('ast');
expect(ast).toEqual('plop');
const ast = response.get('ast') as File;
const buffer = Buffer.from(await ast.arrayBuffer());
const protoMessage = deserializeProtobuf(buffer);
expect(protoMessage.type).toEqual(0);
expect(protoMessage.program.body).toHaveLength(1);
expect(protoMessage.program.body[0].expressionStatement.expression.newExpression).toBeDefined();
});

it('should route /analyze-ts requests', async () => {
Expand All @@ -154,8 +163,9 @@ describe('router', () => {
message: `Remove this duplicated type or replace with another one.`,
}),
);
const ast = response.get('ast');
expect(ast).toEqual('plop');
const ast = response.get('ast') as File;
const buffer = Buffer.from(await ast.arrayBuffer());
expect(buffer.toString()).toEqual('plop');
});

it('should route /analyze-with-program requests', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/analysis/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ export interface JsTsAnalysisOutput extends AnalysisOutput {
metrics?: Metrics;
cpdTokens?: CpdToken[];
ucfgPaths?: string[];
ast: string;
ast: Uint8Array | string;
}
23 changes: 22 additions & 1 deletion packages/jsts/src/analysis/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../linter';
import { buildSourceCode } from '../builders';
import { JsTsAnalysisInput, JsTsAnalysisOutput } from './analysis';
import { serializeInProtobuf } from '../parsers';

/**
* Analyzes a JavaScript / TypeScript analysis input
Expand Down Expand Up @@ -84,7 +85,12 @@ function analyzeFile(
highlightedSymbols,
cognitiveComplexity,
);
return { issues, ucfgPaths, ...extendedMetrics, ast: 'plop' };
return {
issues,
ucfgPaths,
...extendedMetrics,
ast: serializeAst(sourceCode, filePath),
};
} catch (e) {
/** Turns exceptions from TypeScript compiler into "parsing" errors */
if (e.stack.indexOf('typescript.js:') > -1) {
Expand All @@ -95,6 +101,21 @@ function analyzeFile(
}
}

/**
* Remove this when we figure out how to serialize the TypeScript AST
*/
function serializeAst(sourceCode: SourceCode, filePath: string) {
if (isSupported(filePath)) {
return serializeInProtobuf(sourceCode.ast);
} else {
return 'plop';
}

function isSupported(filePath: string) {
return filePath.endsWith('.js');
}
}

/**
* Computes extended metrics about the analyzed code
*
Expand Down
Loading