trullock / NUglify

NUglify is a HTML, JavaScript and CSS minification Library for .NET (fork of AjaxMin + new features)
Other
396 stars 79 forks source link

NullReferenceException when parsing object literal with computed property name expression starting with `(` #374

Closed zonets closed 9 months ago

zonets commented 1 year ago

NUglify 1.20.7 crashes on certain ES6-specific input.

Repro steps Run NUglifyApp with the following input:

const constructObject = (direction) => {
  return { [(direction === Direction.Up ? "up" : "down")+"-direction"]: !0 }
}

Alternatively, insert () in one of the property name expressions in src/NUglify.Tests/TestData/JS/Input/Literals/ObjectLiteral.js such as

        [('c' + x())]: 'hello',

and run the tests.

Result

System.NullReferenceException: 'Object reference not set to an instance of an object.'
name was null.

>   NUglify.JavaScript.JSParser.ParseFunctionPart2(NUglify.JavaScript.Syntax.FunctionType functionType, NUglify.JavaScript.SourceContext context, NUglify.JavaScript.Syntax.BindingIdentifier name, NUglify.JavaScript.Syntax.ArrayLiteral computedName, bool isGenerator, bool isAsync)    C#
    NUglify.JavaScript.JSParser.ParseFunction(NUglify.JavaScript.Syntax.FunctionType functionType, NUglify.JavaScript.SourceContext fncCtx) C#
    NUglify.JavaScript.JSParser.ParseObjectLiteralProperty(bool isBindingPattern)   C#
    NUglify.JavaScript.JSParser.ParseObjectLiteral(bool isBindingPattern)   C#
    NUglify.JavaScript.JSParser.ParseLeftHandSideExpression(bool isMinus)   C#
    NUglify.JavaScript.JSParser.ParseUnaryExpression(out bool isLeftHandSideExpr, bool isMinus) C#
    NUglify.JavaScript.JSParser.ParseExpression(bool single, NUglify.JavaScript.JSToken inToken)    C#
    NUglify.JavaScript.JSParser.ParseReturnStatement()  C#
    NUglify.JavaScript.JSParser.ParseStatement(bool fSourceElement, bool skipImportantComment)  C#
    NUglify.JavaScript.JSParser.ParseBlock()    C#
    NUglify.JavaScript.JSParser.ParseArrowFunction(NUglify.JavaScript.Syntax.AstNode parameters)    C#
    NUglify.JavaScript.JSParser.ParseLeftHandSideExpression(bool isMinus)   C#
    NUglify.JavaScript.JSParser.ParseUnaryExpression(out bool isLeftHandSideExpr, bool isMinus) C#
    NUglify.JavaScript.JSParser.ParseExpression(bool single, NUglify.JavaScript.JSToken inToken)    C#
    NUglify.JavaScript.JSParser.ParseVarDecl(NUglify.JavaScript.JSToken inToken)    C#
    NUglify.JavaScript.JSParser.ParseVariableStatement()    C#
    NUglify.JavaScript.JSParser.ParseStatement(bool fSourceElement, bool skipImportantComment)  C#
    NUglify.JavaScript.JSParser.ParseStatements(NUglify.JavaScript.Syntax.BlockStatement block) C#
    NUglify.JavaScript.JSParser.InternalParse() C#
    NUglify.JavaScript.JSParser.Parse(NUglify.JavaScript.DocumentContext sourceContext) C#
    NUglify.JavaScript.JSParser.Parse(string source)    C#
    NUglify.JavaScript.JSParser.Parse(string source, NUglify.JavaScript.CodeSettings settings)  C#
    NUglify.MainClass.ProcessJSFile(System.Collections.Generic.IList<NUglify.InputGroup> inputGroups, NUglify.UglifyCommandParser uglifyCommandParser, System.Text.StringBuilder outputBuilder) C#
    NUglify.MainClass.ProcessOutputGroup(NUglify.OutputGroup outputGroup, NUglify.UglifyCommandParser uglifyCommandParser)  C#
    NUglify.MainClass.ProcessOutputGroups(System.Collections.Generic.IList<NUglify.OutputGroup> outputGroups)   C#
    NUglify.MainClass.Run() C#
    NUglify.MainClass.Main(string[] args)   C#

Excepted output code

const constructObject=n=>({[(n===Direction.Up?"up":"down")+"-direction"]:!0})
trullock commented 1 year ago

Thanks for the report

I dont have time to look at this but PRs are very welcome. See the other Object literal issues as theres a number of related bugs here.

The stack trace is indicative of where the problem lies, around NUglify.JavaScript.JSParser.ParseObjectLiteralProperty(bool isBindingPattern)