Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Dec 4, 2024
1 parent e29599b commit 0caaba3
Show file tree
Hide file tree
Showing 10 changed files with 507 additions and 21 deletions.
13 changes: 5 additions & 8 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import {binarySearch, compareTextSpans, getNextSibling, getPreviousSibling, TextSpan} from "./parserUtils";
import {JavaScriptTypeMapping} from "./typeMapping";
import path from "node:path";
import {ExpressionStatement} from ".";

export class JavaScriptParser extends Parser {

Expand Down Expand Up @@ -582,10 +583,6 @@ export class JavaScriptParserVisitor {
}

visitIdentifier(node: ts.Identifier) {
if (node.text === 'undefined') {
// unsure why this appears as a ts.Identifier in the AST
return this.mapLiteral(node, undefined);
}
return this.mapIdentifier(node, node.text);
}

Expand Down Expand Up @@ -905,7 +902,7 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(node.name),
Markers.EMPTY,
this.visit(node.name),
nameExpression,
[],
node.initializer ? this.leftPadded(this.prefix(node.getChildAt(node.getChildren().indexOf(node.initializer) - 1)), this.visit(node.initializer)) : null,
this.mapVariableType(node)
Expand Down Expand Up @@ -2416,7 +2413,7 @@ export class JavaScriptParserVisitor {
Markers.EMPTY,
[node.initializer ?
(ts.isVariableDeclarationList(node.initializer) ? this.rightPadded(this.visit(node.initializer), Space.EMPTY) :
this.rightPadded(this.convert(node.initializer), this.suffix(node.initializer.getLastToken()!))) :
this.rightPadded(new ExpressionStatement(randomId(), this.visit(node.initializer)), this.suffix(node.initializer.getLastToken()!))) :
this.rightPadded(this.newJEmpty(), this.suffix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!))], // to handle for (/*_*/; ; );
node.condition ? this.rightPadded(this.convert(node.condition), this.suffix(node.condition.getLastToken()!)) :
this.rightPadded(this.newJEmpty(), this.suffix(this.findChildNode(node, ts.SyntaxKind.SemicolonToken)!)), // to handle for ( ;/*_*/; );
Expand All @@ -2440,7 +2437,7 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!),
Markers.EMPTY,
this.rightPadded(this.visit(node.initializer), this.prefix(node.initializer)),
this.rightPadded(this.visit(node.initializer), this.suffix(node.initializer)),
this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
),
this.rightPadded(
Expand All @@ -2461,7 +2458,7 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!),
Markers.EMPTY,
this.rightPadded(this.visit(node.initializer), this.prefix(node.initializer)),
this.rightPadded(this.visit(node.initializer), this.suffix(node.initializer)),
this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
),
this.rightPadded(
Expand Down
2 changes: 1 addition & 1 deletion openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,7 @@ class Factory implements ReceiverFactory {
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<JRightPadded<Statement>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<JRightPadded<J>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!
);
}
Expand Down
12 changes: 6 additions & 6 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3902,7 +3902,7 @@ export class JSForInLoop extends JSMixin(Object) implements Loop {

@LstType("org.openrewrite.javascript.tree.JS$JSForInOfLoopControl")
export class JSForInOfLoopControl extends JSMixin(Object) {
public constructor(id: UUID, prefix: Space, markers: Markers, variable: JRightPadded<Statement>, iterable: JRightPadded<Expression>) {
public constructor(id: UUID, prefix: Space, markers: Markers, variable: JRightPadded<J>, iterable: JRightPadded<Expression>) {
super();
this._id = id;
this._prefix = prefix;
Expand Down Expand Up @@ -3941,13 +3941,13 @@ export class JSForInOfLoopControl extends JSMixin(Object) {
return markers === this._markers ? this : new JSForInOfLoopControl(this._id, this._prefix, markers, this._variable, this._iterable);
}

private readonly _variable: JRightPadded<Statement>;
private readonly _variable: JRightPadded<J>;

public get variable(): Statement {
public get variable(): J {
return this._variable.element;
}

public withVariable(variable: Statement): JSForInOfLoopControl {
public withVariable(variable: J): JSForInOfLoopControl {
return this.padding.withVariable(this._variable.withElement(variable));
}

Expand All @@ -3968,10 +3968,10 @@ export class JSForInOfLoopControl extends JSMixin(Object) {
get padding() {
const t = this;
return new class {
public get variable(): JRightPadded<Statement> {
public get variable(): JRightPadded<J> {
return t._variable;
}
public withVariable(variable: JRightPadded<Statement>): JSForInOfLoopControl {
public withVariable(variable: JRightPadded<J>): JSForInOfLoopControl {
return t._variable === variable ? t : new JSForInOfLoopControl(t._id, t._prefix, t._markers, variable, t._iterable);
}
public get iterable(): JRightPadded<Expression> {
Expand Down
47 changes: 47 additions & 0 deletions openrewrite/test/javascript/e2e/highlight_js_files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,51 @@ describe('highlight.js files tests', () => {
);
});

test('test/api/unregisterLanguage.js', () => {
rewriteRun(
//language=typescript
typeScript(`
"use strict";
const hljs = require("../../build");
const jQuery = function() {
return {
name: "jQuery",
contains: [{ beginKeywords: "class" }]
};
};
describe(".unregisterLanguage()", () => {
beforeEach(() => {
hljs.registerLanguage("test", jQuery);
});
it("should remove an existing language", () => {
hljs.unregisterLanguage("test");
const result = hljs.getLanguage("test");
should(result).be.undefined();
});
it("should remove an existing language and its aliases", () => {
hljs.registerAliases(["jquery", "jqueryui"], {
languageName: "test"
});
{
const result = hljs.getLanguage("jquery");
should(result.name).equal("jQuery");
}
hljs.unregisterLanguage("test");
{
const result = hljs.getLanguage("jquery");
should(result).be.undefined();
}
});
});
`)
);
});

});
Loading

0 comments on commit 0caaba3

Please sign in to comment.