@mrleebo/prisma-ast
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -6,2 +6,3 @@ 'use strict'; | ||
var chevrotain = require('chevrotain'); | ||
var os = require('os'); | ||
@@ -152,3 +153,3 @@ var Identifier = /*#__PURE__*/chevrotain.createToken({ | ||
name: 'LineBreak', | ||
pattern: '\n', | ||
pattern: /\n|\r\n/, | ||
line_breaks: true, | ||
@@ -798,3 +799,3 @@ label: 'LineBreak' | ||
function printSchema(schema) { | ||
return schema.list.map(printBlock).filter(Boolean).join('\n').replace(/(\n\s*){3,}/g, '\n\n'); | ||
return schema.list.map(printBlock).filter(Boolean).join(os.EOL).replace(/(\r?\n\s*){3,}/g, os.EOL + os.EOL); | ||
} | ||
@@ -832,3 +833,3 @@ | ||
function printBreak() { | ||
return '\n'; | ||
return os.EOL; | ||
} | ||
@@ -842,3 +843,3 @@ | ||
function printEnum(enumerator) { | ||
var children = enumerator.enumerators.map(printEnumerator).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
var children = enumerator.enumerators.map(printEnumerator).filter(Boolean).join(os.EOL + " ").replace(/(\r?\n\s*){3,}/g, os.EOL + os.EOL + " "); | ||
return "\nenum " + enumerator.name + " {\n " + children + "\n}"; | ||
@@ -1007,3 +1008,3 @@ } | ||
return printAssignment(item, keyLengths[0]); | ||
}).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
}).filter(Boolean).join(os.EOL + " ").replace(/(\r?\n\s*){3,}/g, os.EOL + os.EOL + " "); | ||
} | ||
@@ -1036,3 +1037,3 @@ | ||
return printProperty(prop, nameLengths[0], typeLengths[0]); | ||
}).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
}).filter(Boolean).join(os.EOL + " ").replace(/(\r?\n\s*){3,}/g, os.EOL + os.EOL + " "); | ||
} | ||
@@ -1039,0 +1040,0 @@ |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("chevrotain"),t=e.createToken({name:"Identifier",pattern:/[a-zA-Z]\w*/}),n=e.createToken({name:"Datasource",pattern:/datasource/,longer_alt:t}),r=e.createToken({name:"Generator",pattern:/generator/,longer_alt:t}),a=e.createToken({name:"Model",pattern:/model/,longer_alt:t}),u=e.createToken({name:"Enum",pattern:/enum/,longer_alt:t}),i=e.createToken({name:"True",pattern:/true/,longer_alt:t}),o=e.createToken({name:"False",pattern:/false/,longer_alt:t}),c=e.createToken({name:"Null",pattern:/null/,longer_alt:t}),l=e.createToken({name:"Comment",pattern:e.Lexer.NA}),m=e.createToken({name:"DocComment",pattern:/\/\/\/\s*(.+)/,categories:[l]}),s=e.createToken({name:"LineComment",pattern:/\/\/\s*(.+)/,categories:[l]}),L=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),f=e.createToken({name:"ModelAttribute",pattern:/@@/,label:"'@@'",categories:[L]}),p=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[L]}),E=e.createToken({name:"Dot",pattern:/\./,label:"'.'"}),A=e.createToken({name:"QuestionMark",pattern:/\?/,label:"'?'"}),U=e.createToken({name:"Array",pattern:/\[\]/,label:"'[]'"}),y=e.createToken({name:"LCurly",pattern:/{/,label:"'{'"}),d=e.createToken({name:"RCurly",pattern:/}/,label:"'}'"}),v=e.createToken({name:"LRound",pattern:/\(/,label:"'('"}),S=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),g=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),T=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),b=e.createToken({name:"Comma",pattern:/,/,label:"','"}),B=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),N=e.createToken({name:"Equals",pattern:/=/,label:"'='"}),k=e.createToken({name:"StringLiteral",pattern:/"(:?[^\\"\n\r]+|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/}),O=e.createToken({name:"NumberLiteral",pattern:/-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/}),M=e.createToken({name:"WhiteSpace",pattern:/\s+/,group:e.Lexer.SKIPPED}),h=e.createToken({name:"LineBreak",pattern:"\n",line_breaks:!0,label:"LineBreak"}),C=[l,m,s,h,M,L,f,p,E,A,U,y,d,g,T,v,S,b,B,N,i,o,c,n,r,a,u,k,O,t],R=new e.Lexer(C);function w(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var x=new(function(e){function m(){var m;return(m=e.call(this,C)||this).break=m.RULE("break",(function(){m.CONSUME1(h),m.CONSUME2(h)})),m.keyedArg=m.RULE("keyedArg",(function(){m.CONSUME(t,{LABEL:"keyName"}),m.CONSUME(B),m.SUBRULE(m.value)})),m.array=m.RULE("array",(function(){m.CONSUME(g),m.MANY_SEP({SEP:b,DEF:function(){m.SUBRULE(m.value)}}),m.CONSUME(T)})),m.func=m.RULE("func",(function(){m.CONSUME(t,{LABEL:"funcName"}),m.CONSUME(v),m.MANY_SEP({SEP:b,DEF:function(){m.SUBRULE(m.value)}}),m.CONSUME(S)})),m.value=m.RULE("value",(function(){m.OR([{ALT:function(){return m.CONSUME(k,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(O,{LABEL:"value"})}},{ALT:function(){return m.SUBRULE(m.array,{LABEL:"value"})}},{ALT:function(){return m.SUBRULE(m.func,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(i,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(o,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(c,{LABEL:"value"})}},{ALT:function(){return m.CONSUME(t,{LABEL:"value"})}}])})),m.property=m.RULE("property",(function(){m.CONSUME(t,{LABEL:"propertyName"}),m.CONSUME(N),m.SUBRULE(m.value,{LABEL:"propertyValue"})})),m.assignment=m.RULE("assignment",(function(){m.CONSUME(t,{LABEL:"assignmentName"}),m.CONSUME(N),m.SUBRULE(m.value,{LABEL:"assignmentValue"})})),m.field=m.RULE("field",(function(){m.CONSUME(t,{LABEL:"fieldName"}),m.SUBRULE(m.value,{LABEL:"fieldType"}),m.OPTION((function(){m.OR([{ALT:function(){return m.CONSUME(U,{LABEL:"array"})}},{ALT:function(){return m.CONSUME(A,{LABEL:"optional"})}}])})),m.MANY((function(){m.SUBRULE(m.attribute,{LABEL:"attributeList"})}))})),m.block=m.RULE("block",(function(e){var t=(void 0===e?{}:e).componentType,n="enum"===t,r="model"===t;m.CONSUME(y),m.CONSUME1(h),m.MANY((function(){m.OR([{ALT:function(){return m.SUBRULE(m.comment,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return m.SUBRULE(m.property,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return m.SUBRULE(m.attribute,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return m.SUBRULE(m.field,{LABEL:"list"})}},{GATE:function(){return n},ALT:function(){return m.SUBRULE(m.enum,{LABEL:"list"})}},{GATE:function(){return!r},ALT:function(){return m.SUBRULE(m.assignment,{LABEL:"list"})}},{ALT:function(){return m.SUBRULE(m.break,{LABEL:"list"})}},{ALT:function(){return m.CONSUME2(h)}}])})),m.CONSUME(d)})),m.enum=m.RULE("enum",(function(){m.CONSUME(t,{LABEL:"enumName"})})),m.attribute=m.RULE("attribute",(function(){m.OR1([{ALT:function(){return m.CONSUME(f,{LABEL:"modelAttribute"})}},{ALT:function(){return m.CONSUME(p,{LABEL:"fieldAttribute"})}}]),m.OR2([{ALT:function(){m.CONSUME1(t,{LABEL:"groupName"}),m.CONSUME(E),m.CONSUME2(t,{LABEL:"attributeName"})}},{ALT:function(){return m.CONSUME(t,{LABEL:"attributeName"})}}]),m.OPTION((function(){m.CONSUME(v),m.MANY_SEP({SEP:b,DEF:function(){m.SUBRULE(m.attributeArg)}}),m.CONSUME(S)}))})),m.attributeArg=m.RULE("attributeArg",(function(){m.OR([{ALT:function(){return m.SUBRULE(m.keyedArg,{LABEL:"value"})}},{ALT:function(){return m.SUBRULE(m.value,{LABEL:"value"})}}])})),m.component=m.RULE("component",(function(){var e=m.OR1([{ALT:function(){return m.CONSUME(n,{LABEL:"type"})}},{ALT:function(){return m.CONSUME(r,{LABEL:"type"})}},{ALT:function(){return m.CONSUME(a,{LABEL:"type"})}},{ALT:function(){return m.CONSUME(u,{LABEL:"type"})}}]);m.OR2([{ALT:function(){m.CONSUME1(t,{LABEL:"groupName"}),m.CONSUME(E),m.CONSUME2(t,{LABEL:"componentName"})}},{ALT:function(){return m.CONSUME(t,{LABEL:"componentName"})}}]),m.SUBRULE(m.block,{ARGS:[{componentType:e.image}]})})),m.comment=m.RULE("comment",(function(){m.CONSUME(l,{LABEL:"text"})})),m.schema=m.RULE("schema",(function(){m.MANY((function(){m.OR([{ALT:function(){return m.SUBRULE(m.comment,{LABEL:"list"})}},{ALT:function(){return m.SUBRULE(m.component,{LABEL:"list"})}},{ALT:function(){return m.SUBRULE(m.break,{LABEL:"list"})}},{ALT:function(){return m.CONSUME(h)}}])}))})),m.performSelfAnalysis(),m}return w(m,e),m}(e.CstParser)),_=function(e){function t(){var t;return(t=e.call(this)||this).validateVisitor(),t}w(t,e);var n=t.prototype;return n.schema=function(e){var t,n=this;return{type:"schema",list:null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))}},n.component=function(e){var t=e.type[0].image,n=e.componentName[0].image,r=this.visit(e.block);switch(t){case"datasource":return{type:"datasource",name:n,assignments:r};case"generator":return{type:"generator",name:n,assignments:r};case"model":return{type:"model",name:n,properties:r};case"enum":return{type:"enum",name:n,enumerators:r};default:throw new Error("Unexpected block type: "+t)}},n.break=function(){return{type:"break"}},n.comment=function(e){return{type:"comment",text:e.text[0].image}},n.block=function(e){var t,n=this;return null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))},n.assignment=function(e){var t=this.visit(e.assignmentValue);return{type:"assignment",key:e.assignmentName[0].image,value:t}},n.field=function(e){var t=this,n=this.visit(e.fieldType),r=e.fieldName[0].image,a=e.attributeList&&e.attributeList.map((function(e){return t.visit([e])}));return{type:"field",name:r,fieldType:n,array:null!=e.array,optional:null!=e.optional,attributes:a}},n.attribute=function(e){var t=this,n=e.attributeName[0].image,r=(e.groupName||[{}])[0].image,a=e.attributeArg&&e.attributeArg.map((function(e){return t.visit(e)}));return{type:"attribute",name:n,kind:null!=e.modelAttribute?"model":"field",group:r,args:a}},n.attributeArg=function(e){return{type:"attributeArgument",value:this.visit(e.value)}},n.func=function(e){var t=this;return{type:"function",name:e.funcName[0].image,params:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.array=function(e){var t=this;return{type:"array",args:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.keyedArg=function(e){return{type:"keyValue",key:e.keyName[0].image,value:this.visit(e.value)}},n.value=function(e){return"image"in e.value[0]?e.value[0].image:this.visit(e.value)},n.enum=function(e){return{type:"enumerator",name:e.enumName[0].image}},t}(x.getBaseCstVisitorConstructorWithDefaults());function j(e){switch(e.type){case"comment":return P(e);case"datasource":return n=q((t=e).assignments),"\ndatasource "+t.name+" {\n "+n+"\n}";case"enum":return function(e){var t=e.enumerators.map(D).filter(Boolean).join("\n ").replace(/(\n\s*){3,}/g,"\n\n ");return"\nenum "+e.name+" {\n "+t+"\n}"}(e);case"generator":return function(e){var t=q(e.assignments);return"\ngenerator "+e.name+" {\n "+t+"\n}"}(e);case"model":return function(e){var t,n,r,a,u,i=(n=0,r=(t=e.properties).reduce((function(e,t,r,a){return"break"===t.type||(r>0&&"break"===a[r-1].type&&(e[++n]=[]),e[n].push(t)),e}),[[]]),a=r.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?t.name.length:0)}),0)})),u=r.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?F(t).length:0)}),0)})),t.map((function(e,t,n){return t>0&&"break"!==e.type&&"break"===n[t-1].type&&(a.shift(),u.shift()),function(e,t,n){switch(void 0===t&&(t=0),void 0===n&&(n=0),e.type){case"attribute":return G(e);case"field":return function(e,t,n){void 0===t&&(t=0),void 0===n&&(n=0);var r=e.name.padEnd(t),a=F(e).padEnd(n),u=e.attributes?e.attributes.map(G):[];return[r,a].concat(u).filter(Boolean).join(" ").trim()}(e,t,n);case"comment":return P(e);case"break":return"\n";default:throw new Error("Unrecognized property type")}}(e,a[0],u[0])})).filter(Boolean).join("\n ").replace(/(\n\s*){3,}/g,"\n\n "));return"\nmodel "+e.name+" {\n "+i+"\n}"}(e);case"break":return"\n";default:throw new Error("Unrecognized block type")}var t,n}function P(e){return e.text}function D(e){switch(e.type){case"enumerator":return e.name;case"comment":return P(e);case"break":return"\n";default:throw new Error("Unexpected enumerator type")}}function G(e){var t=e.args&&e.args.length>0?"("+e.args.map(V).filter(Boolean).join(", ")+")":"";return("field"===e.kind?"@":"@@")+e.name+t}function V(e){return Y(e.value)}function F(e){var t=e.array?"[]":e.optional?"?":"";if("object"==typeof e.fieldType)switch(e.fieldType.type){case"function":var n=e.fieldType.params.map(Y);return e.fieldType.name+"("+n+")"+t;default:throw new Error("Unexpected field type")}return""+e.fieldType+t}function Y(e){switch(typeof e){case"object":if("type"in e)switch(e.type){case"keyValue":return e.key+": "+Y(e.value);case"function":return e.name+"("+(e.params?e.params.map(Y):"")+")";case"array":return"["+e.args.join(", ")+"]";default:throw new Error("Unexpected value type")}throw new Error("Unexpected object value");default:return String(e)}}function q(e){var t=0,n=e.reduce((function(e,n,r,a){return"break"===n.type||(r>0&&"break"===a[r-1].type&&(e[++t]=[]),e[t].push(n)),e}),[[]]).map((function(e){return e.reduce((function(e,t){return Math.max(e,"assignment"===t.type?t.key.length:0)}),0)}));return e.map((function(e,t,r){return t>0&&"break"!==e.type&&"break"===r[t-1].type&&n.shift(),function(e,t){switch(void 0===t&&(t=0),e.type){case"comment":return P(e);case"break":return"\n";case"assignment":return e.key.padEnd(t)+" = "+Y(e.value);default:throw new Error("Unexpected assignment type")}}(e,n[0])})).filter(Boolean).join("\n ").replace(/(\n\s*){3,}/g,"\n\n ")}exports.getSchema=function(e){var t=R.tokenize(e);x.input=t.tokens;var n=x.schema();if(x.errors.length>0)throw x.errors[0];return(new _).visit(n)},exports.printSchema=function(e){return e.list.map(j).filter(Boolean).join("\n").replace(/(\n\s*){3,}/g,"\n\n")}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("chevrotain"),t=require("os"),n=e.createToken({name:"Identifier",pattern:/[a-zA-Z]\w*/}),r=e.createToken({name:"Datasource",pattern:/datasource/,longer_alt:n}),a=e.createToken({name:"Generator",pattern:/generator/,longer_alt:n}),u=e.createToken({name:"Model",pattern:/model/,longer_alt:n}),i=e.createToken({name:"Enum",pattern:/enum/,longer_alt:n}),o=e.createToken({name:"True",pattern:/true/,longer_alt:n}),c=e.createToken({name:"False",pattern:/false/,longer_alt:n}),l=e.createToken({name:"Null",pattern:/null/,longer_alt:n}),m=e.createToken({name:"Comment",pattern:e.Lexer.NA}),L=e.createToken({name:"DocComment",pattern:/\/\/\/\s*(.+)/,categories:[m]}),s=e.createToken({name:"LineComment",pattern:/\/\/\s*(.+)/,categories:[m]}),f=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),p=e.createToken({name:"ModelAttribute",pattern:/@@/,label:"'@@'",categories:[f]}),E=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[f]}),A=e.createToken({name:"Dot",pattern:/\./,label:"'.'"}),U=e.createToken({name:"QuestionMark",pattern:/\?/,label:"'?'"}),y=e.createToken({name:"Array",pattern:/\[\]/,label:"'[]'"}),d=e.createToken({name:"LCurly",pattern:/{/,label:"'{'"}),v=e.createToken({name:"RCurly",pattern:/}/,label:"'}'"}),S=e.createToken({name:"LRound",pattern:/\(/,label:"'('"}),g=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),T=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),b=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),B=e.createToken({name:"Comma",pattern:/,/,label:"','"}),N=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),k=e.createToken({name:"Equals",pattern:/=/,label:"'='"}),O=e.createToken({name:"StringLiteral",pattern:/"(:?[^\\"\n\r]+|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/}),M=e.createToken({name:"NumberLiteral",pattern:/-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/}),h=e.createToken({name:"WhiteSpace",pattern:/\s+/,group:e.Lexer.SKIPPED}),C=e.createToken({name:"LineBreak",pattern:/\n|\r\n/,line_breaks:!0,label:"LineBreak"}),R=[m,L,s,C,h,f,p,E,A,U,y,d,v,T,b,S,g,B,N,k,o,c,l,r,a,u,i,O,M,n],w=new e.Lexer(R);function x(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var _=new(function(e){function t(){var t;return(t=e.call(this,R)||this).break=t.RULE("break",(function(){t.CONSUME1(C),t.CONSUME2(C)})),t.keyedArg=t.RULE("keyedArg",(function(){t.CONSUME(n,{LABEL:"keyName"}),t.CONSUME(N),t.SUBRULE(t.value)})),t.array=t.RULE("array",(function(){t.CONSUME(T),t.MANY_SEP({SEP:B,DEF:function(){t.SUBRULE(t.value)}}),t.CONSUME(b)})),t.func=t.RULE("func",(function(){t.CONSUME(n,{LABEL:"funcName"}),t.CONSUME(S),t.MANY_SEP({SEP:B,DEF:function(){t.SUBRULE(t.value)}}),t.CONSUME(g)})),t.value=t.RULE("value",(function(){t.OR([{ALT:function(){return t.CONSUME(O,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(M,{LABEL:"value"})}},{ALT:function(){return t.SUBRULE(t.array,{LABEL:"value"})}},{ALT:function(){return t.SUBRULE(t.func,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(o,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(c,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(l,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"value"})}}])})),t.property=t.RULE("property",(function(){t.CONSUME(n,{LABEL:"propertyName"}),t.CONSUME(k),t.SUBRULE(t.value,{LABEL:"propertyValue"})})),t.assignment=t.RULE("assignment",(function(){t.CONSUME(n,{LABEL:"assignmentName"}),t.CONSUME(k),t.SUBRULE(t.value,{LABEL:"assignmentValue"})})),t.field=t.RULE("field",(function(){t.CONSUME(n,{LABEL:"fieldName"}),t.SUBRULE(t.value,{LABEL:"fieldType"}),t.OPTION((function(){t.OR([{ALT:function(){return t.CONSUME(y,{LABEL:"array"})}},{ALT:function(){return t.CONSUME(U,{LABEL:"optional"})}}])})),t.MANY((function(){t.SUBRULE(t.attribute,{LABEL:"attributeList"})}))})),t.block=t.RULE("block",(function(e){var n=(void 0===e?{}:e).componentType,r="enum"===n,a="model"===n;t.CONSUME(d),t.CONSUME1(C),t.MANY((function(){t.OR([{ALT:function(){return t.SUBRULE(t.comment,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return t.SUBRULE(t.property,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return t.SUBRULE(t.attribute,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return t.SUBRULE(t.field,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return t.SUBRULE(t.enum,{LABEL:"list"})}},{GATE:function(){return!a},ALT:function(){return t.SUBRULE(t.assignment,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.break,{LABEL:"list"})}},{ALT:function(){return t.CONSUME2(C)}}])})),t.CONSUME(v)})),t.enum=t.RULE("enum",(function(){t.CONSUME(n,{LABEL:"enumName"})})),t.attribute=t.RULE("attribute",(function(){t.OR1([{ALT:function(){return t.CONSUME(p,{LABEL:"modelAttribute"})}},{ALT:function(){return t.CONSUME(E,{LABEL:"fieldAttribute"})}}]),t.OR2([{ALT:function(){t.CONSUME1(n,{LABEL:"groupName"}),t.CONSUME(A),t.CONSUME2(n,{LABEL:"attributeName"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"attributeName"})}}]),t.OPTION((function(){t.CONSUME(S),t.MANY_SEP({SEP:B,DEF:function(){t.SUBRULE(t.attributeArg)}}),t.CONSUME(g)}))})),t.attributeArg=t.RULE("attributeArg",(function(){t.OR([{ALT:function(){return t.SUBRULE(t.keyedArg,{LABEL:"value"})}},{ALT:function(){return t.SUBRULE(t.value,{LABEL:"value"})}}])})),t.component=t.RULE("component",(function(){var e=t.OR1([{ALT:function(){return t.CONSUME(r,{LABEL:"type"})}},{ALT:function(){return t.CONSUME(a,{LABEL:"type"})}},{ALT:function(){return t.CONSUME(u,{LABEL:"type"})}},{ALT:function(){return t.CONSUME(i,{LABEL:"type"})}}]);t.OR2([{ALT:function(){t.CONSUME1(n,{LABEL:"groupName"}),t.CONSUME(A),t.CONSUME2(n,{LABEL:"componentName"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"componentName"})}}]),t.SUBRULE(t.block,{ARGS:[{componentType:e.image}]})})),t.comment=t.RULE("comment",(function(){t.CONSUME(m,{LABEL:"text"})})),t.schema=t.RULE("schema",(function(){t.MANY((function(){t.OR([{ALT:function(){return t.SUBRULE(t.comment,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.component,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.break,{LABEL:"list"})}},{ALT:function(){return t.CONSUME(C)}}])}))})),t.performSelfAnalysis(),t}return x(t,e),t}(e.CstParser)),j=function(e){function t(){var t;return(t=e.call(this)||this).validateVisitor(),t}x(t,e);var n=t.prototype;return n.schema=function(e){var t,n=this;return{type:"schema",list:null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))}},n.component=function(e){var t=e.type[0].image,n=e.componentName[0].image,r=this.visit(e.block);switch(t){case"datasource":return{type:"datasource",name:n,assignments:r};case"generator":return{type:"generator",name:n,assignments:r};case"model":return{type:"model",name:n,properties:r};case"enum":return{type:"enum",name:n,enumerators:r};default:throw new Error("Unexpected block type: "+t)}},n.break=function(){return{type:"break"}},n.comment=function(e){return{type:"comment",text:e.text[0].image}},n.block=function(e){var t,n=this;return null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))},n.assignment=function(e){var t=this.visit(e.assignmentValue);return{type:"assignment",key:e.assignmentName[0].image,value:t}},n.field=function(e){var t=this,n=this.visit(e.fieldType),r=e.fieldName[0].image,a=e.attributeList&&e.attributeList.map((function(e){return t.visit([e])}));return{type:"field",name:r,fieldType:n,array:null!=e.array,optional:null!=e.optional,attributes:a}},n.attribute=function(e){var t=this,n=e.attributeName[0].image,r=(e.groupName||[{}])[0].image,a=e.attributeArg&&e.attributeArg.map((function(e){return t.visit(e)}));return{type:"attribute",name:n,kind:null!=e.modelAttribute?"model":"field",group:r,args:a}},n.attributeArg=function(e){return{type:"attributeArgument",value:this.visit(e.value)}},n.func=function(e){var t=this;return{type:"function",name:e.funcName[0].image,params:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.array=function(e){var t=this;return{type:"array",args:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.keyedArg=function(e){return{type:"keyValue",key:e.keyName[0].image,value:this.visit(e.value)}},n.value=function(e){return"image"in e.value[0]?e.value[0].image:this.visit(e.value)},n.enum=function(e){return{type:"enumerator",name:e.enumName[0].image}},t}(_.getBaseCstVisitorConstructorWithDefaults());function P(e){switch(e.type){case"comment":return D(e);case"datasource":return r=I((n=e).assignments),"\ndatasource "+n.name+" {\n "+r+"\n}";case"enum":return function(e){var n=e.enumerators.map(V).filter(Boolean).join(t.EOL+" ").replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL+" ");return"\nenum "+e.name+" {\n "+n+"\n}"}(e);case"generator":return function(e){var t=I(e.assignments);return"\ngenerator "+e.name+" {\n "+t+"\n}"}(e);case"model":return function(e){var n,r,a,u,i,o=(r=0,a=(n=e.properties).reduce((function(e,t,n,a){return"break"===t.type||(n>0&&"break"===a[n-1].type&&(e[++r]=[]),e[r].push(t)),e}),[[]]),u=a.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?t.name.length:0)}),0)})),i=a.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?q(t).length:0)}),0)})),n.map((function(e,t,n){return t>0&&"break"!==e.type&&"break"===n[t-1].type&&(u.shift(),i.shift()),function(e,t,n){switch(void 0===t&&(t=0),void 0===n&&(n=0),e.type){case"attribute":return F(e);case"field":return function(e,t,n){void 0===t&&(t=0),void 0===n&&(n=0);var r=e.name.padEnd(t),a=q(e).padEnd(n),u=e.attributes?e.attributes.map(F):[];return[r,a].concat(u).filter(Boolean).join(" ").trim()}(e,t,n);case"comment":return D(e);case"break":return G();default:throw new Error("Unrecognized property type")}}(e,u[0],i[0])})).filter(Boolean).join(t.EOL+" ").replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL+" "));return"\nmodel "+e.name+" {\n "+o+"\n}"}(e);case"break":return G();default:throw new Error("Unrecognized block type")}var n,r}function D(e){return e.text}function G(){return t.EOL}function V(e){switch(e.type){case"enumerator":return e.name;case"comment":return D(e);case"break":return G();default:throw new Error("Unexpected enumerator type")}}function F(e){var t=e.args&&e.args.length>0?"("+e.args.map(Y).filter(Boolean).join(", ")+")":"";return("field"===e.kind?"@":"@@")+e.name+t}function Y(e){return z(e.value)}function q(e){var t=e.array?"[]":e.optional?"?":"";if("object"==typeof e.fieldType)switch(e.fieldType.type){case"function":var n=e.fieldType.params.map(z);return e.fieldType.name+"("+n+")"+t;default:throw new Error("Unexpected field type")}return""+e.fieldType+t}function z(e){switch(typeof e){case"object":if("type"in e)switch(e.type){case"keyValue":return e.key+": "+z(e.value);case"function":return e.name+"("+(e.params?e.params.map(z):"")+")";case"array":return"["+e.args.join(", ")+"]";default:throw new Error("Unexpected value type")}throw new Error("Unexpected object value");default:return String(e)}}function I(e){var n=0,r=e.reduce((function(e,t,r,a){return"break"===t.type||(r>0&&"break"===a[r-1].type&&(e[++n]=[]),e[n].push(t)),e}),[[]]).map((function(e){return e.reduce((function(e,t){return Math.max(e,"assignment"===t.type?t.key.length:0)}),0)}));return e.map((function(e,t,n){return t>0&&"break"!==e.type&&"break"===n[t-1].type&&r.shift(),function(e,t){switch(void 0===t&&(t=0),e.type){case"comment":return D(e);case"break":return G();case"assignment":return e.key.padEnd(t)+" = "+z(e.value);default:throw new Error("Unexpected assignment type")}}(e,r[0])})).filter(Boolean).join(t.EOL+" ").replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL+" ")}exports.getSchema=function(e){var t=w.tokenize(e);_.input=t.tokens;var n=_.schema();if(_.errors.length>0)throw _.errors[0];return(new j).visit(n)},exports.printSchema=function(e){return e.list.map(P).filter(Boolean).join(t.EOL).replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL)}; | ||
//# sourceMappingURL=prisma-ast.cjs.production.min.js.map |
import { Lexer, createToken, CstParser } from 'chevrotain'; | ||
import { EOL } from 'os'; | ||
@@ -147,3 +148,3 @@ var Identifier = /*#__PURE__*/createToken({ | ||
name: 'LineBreak', | ||
pattern: '\n', | ||
pattern: /\n|\r\n/, | ||
line_breaks: true, | ||
@@ -793,3 +794,3 @@ label: 'LineBreak' | ||
function printSchema(schema) { | ||
return schema.list.map(printBlock).filter(Boolean).join('\n').replace(/(\n\s*){3,}/g, '\n\n'); | ||
return schema.list.map(printBlock).filter(Boolean).join(EOL).replace(/(\r?\n\s*){3,}/g, EOL + EOL); | ||
} | ||
@@ -827,3 +828,3 @@ | ||
function printBreak() { | ||
return '\n'; | ||
return EOL; | ||
} | ||
@@ -837,3 +838,3 @@ | ||
function printEnum(enumerator) { | ||
var children = enumerator.enumerators.map(printEnumerator).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
var children = enumerator.enumerators.map(printEnumerator).filter(Boolean).join(EOL + " ").replace(/(\r?\n\s*){3,}/g, EOL + EOL + " "); | ||
return "\nenum " + enumerator.name + " {\n " + children + "\n}"; | ||
@@ -1002,3 +1003,3 @@ } | ||
return printAssignment(item, keyLengths[0]); | ||
}).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
}).filter(Boolean).join(EOL + " ").replace(/(\r?\n\s*){3,}/g, EOL + EOL + " "); | ||
} | ||
@@ -1031,3 +1032,3 @@ | ||
return printProperty(prop, nameLengths[0], typeLengths[0]); | ||
}).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
}).filter(Boolean).join(EOL + " ").replace(/(\r?\n\s*){3,}/g, EOL + EOL + " "); | ||
} | ||
@@ -1034,0 +1035,0 @@ |
{ | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -148,3 +148,3 @@ import { createToken, Lexer } from 'chevrotain'; | ||
name: 'LineBreak', | ||
pattern: '\n', | ||
pattern: /\n|\r\n/, | ||
line_breaks: true, | ||
@@ -151,0 +151,0 @@ label: 'LineBreak', |
import * as Types from './getSchema'; | ||
import { EOL } from 'os'; | ||
@@ -7,4 +8,4 @@ export function printSchema(schema: Types.Schema): string { | ||
.filter(Boolean) | ||
.join('\n') | ||
.replace(/(\n\s*){3,}/g, '\n\n'); | ||
.join(EOL) | ||
.replace(/(\r?\n\s*){3,}/g, EOL + EOL); | ||
} | ||
@@ -36,3 +37,3 @@ | ||
function printBreak() { | ||
return '\n'; | ||
return EOL; | ||
} | ||
@@ -53,4 +54,4 @@ | ||
.filter(Boolean) | ||
.join('\n ') | ||
.replace(/(\n\s*){3,}/g, '\n\n '); | ||
.join(`${EOL} `) | ||
.replace(/(\r?\n\s*){3,}/g, `${EOL + EOL} `); | ||
@@ -232,4 +233,4 @@ return ` | ||
.filter(Boolean) | ||
.join('\n ') | ||
.replace(/(\n\s*){3,}/g, '\n\n '); | ||
.join(`${EOL} `) | ||
.replace(/(\r?\n\s*){3,}/g, `${EOL + EOL} `); | ||
} | ||
@@ -289,4 +290,4 @@ | ||
.filter(Boolean) | ||
.join('\n ') | ||
.replace(/(\n\s*){3,}/g, '\n\n '); | ||
.join(`${EOL} `) | ||
.replace(/(\r?\n\s*){3,}/g, `${EOL + EOL} `); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
241031
3190