@mrleebo/prisma-ast
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -864,27 +864,3 @@ 'use strict'; | ||
function printModel(model) { | ||
var pos = 0; | ||
var listBlocks = model.properties.reduce(function (memo, current, index, arr) { | ||
if (current.type !== 'field') return memo; | ||
if (index > 0 && arr[index - 1].type !== 'field') memo[++pos] = []; | ||
memo[pos].push(current); | ||
return memo; | ||
}, [[]]); | ||
var nameLengths = listBlocks.map(function (lists) { | ||
return lists.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'field' ? current.name.length : 0); | ||
}, 0); | ||
}); | ||
var typeLengths = listBlocks.map(function (lists) { | ||
return lists.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'field' ? printFieldType(current).length : 0); | ||
}, 0); | ||
}); | ||
var children = model.properties.map(function (prop, index, arr) { | ||
if (index > 0 && prop.type === 'field' && arr[index - 1].type !== 'field') { | ||
nameLengths.shift(); | ||
typeLengths.shift(); | ||
} | ||
return printProperty(prop, nameLengths[0], typeLengths[0]); | ||
}).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
var children = computePropertyFormatting(model.properties); | ||
return "\nmodel " + model.name + " {\n " + children + "\n}"; | ||
@@ -1014,4 +990,4 @@ } | ||
var listBlocks = list.reduce(function (memo, current, index, arr) { | ||
if (current.type !== 'assignment') return memo; | ||
if (index > 0 && arr[index - 1].type !== 'assignment') memo[++pos] = []; | ||
if (current.type === 'break') return memo; | ||
if (index > 0 && arr[index - 1].type === 'break') memo[++pos] = []; | ||
memo[pos].push(current); | ||
@@ -1026,3 +1002,3 @@ return memo; | ||
return list.map(function (item, index, arr) { | ||
if (index > 0 && item.type === 'assignment' && arr[index - 1].type !== 'assignment') keyLengths.shift(); | ||
if (index > 0 && item.type !== 'break' && arr[index - 1].type === 'break') keyLengths.shift(); | ||
return printAssignment(item, keyLengths[0]); | ||
@@ -1032,4 +1008,32 @@ }).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
function computePropertyFormatting(list) { | ||
var pos = 0; | ||
var listBlocks = list.reduce(function (memo, current, index, arr) { | ||
if (current.type === 'break') return memo; | ||
if (index > 0 && arr[index - 1].type === 'break') memo[++pos] = []; | ||
memo[pos].push(current); | ||
return memo; | ||
}, [[]]); | ||
var nameLengths = listBlocks.map(function (lists) { | ||
return lists.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'field' ? current.name.length : 0); | ||
}, 0); | ||
}); | ||
var typeLengths = listBlocks.map(function (lists) { | ||
return lists.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'field' ? printFieldType(current).length : 0); | ||
}, 0); | ||
}); | ||
return list.map(function (prop, index, arr) { | ||
if (index > 0 && prop.type !== 'break' && arr[index - 1].type === 'break') { | ||
nameLengths.shift(); | ||
typeLengths.shift(); | ||
} | ||
return printProperty(prop, nameLengths[0], typeLengths[0]); | ||
}).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
} | ||
exports.getSchema = getSchema; | ||
exports.printSchema = printSchema; | ||
//# sourceMappingURL=prisma-ast.cjs.development.js.map |
@@ -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]}),f=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),p=e.createToken({name:"ModelAttribute",pattern:/@@/,label:"'@@'",categories:[f]}),L=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[f]}),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:"'('"}),g=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),S=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),T=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),B=e.createToken({name:"Comma",pattern:/,/,label:"','"}),N=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),b=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,f,p,L,E,A,U,y,d,S,T,v,g,B,N,b,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(N),m.SUBRULE(m.value)})),m.array=m.RULE("array",(function(){m.CONSUME(S),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(g)})),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(b),m.SUBRULE(m.value,{LABEL:"propertyValue"})})),m.assignment=m.RULE("assignment",(function(){m.CONSUME(t,{LABEL:"assignmentName"}),m.CONSUME(b),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(p,{LABEL:"modelAttribute"})}},{ALT:function(){return m.CONSUME(L,{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(g)}))})),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=0,n=e.properties.reduce((function(e,n,r,a){return"field"!==n.type||(r>0&&"field"!==a[r-1].type&&(e[++t]=[]),e[t].push(n)),e}),[[]]),r=n.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?t.name.length:0)}),0)})),a=n.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?F(t).length:0)}),0)})),u=e.properties.map((function(e,t,n){return t>0&&"field"===e.type&&"field"!==n[t-1].type&&(r.shift(),a.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,r[0],a[0])})).filter(Boolean).join("\n ").replace(/(\n\s*){3,}/g,"\n\n ");return"\nmodel "+e.name+" {\n "+u+"\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"assignment"!==n.type||(r>0&&"assignment"!==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&&"assignment"===e.type&&"assignment"!==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=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")}; | ||
//# sourceMappingURL=prisma-ast.cjs.production.min.js.map |
@@ -860,27 +860,3 @@ import { Lexer, createToken, CstParser } from 'chevrotain'; | ||
function printModel(model) { | ||
var pos = 0; | ||
var listBlocks = model.properties.reduce(function (memo, current, index, arr) { | ||
if (current.type !== 'field') return memo; | ||
if (index > 0 && arr[index - 1].type !== 'field') memo[++pos] = []; | ||
memo[pos].push(current); | ||
return memo; | ||
}, [[]]); | ||
var nameLengths = listBlocks.map(function (lists) { | ||
return lists.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'field' ? current.name.length : 0); | ||
}, 0); | ||
}); | ||
var typeLengths = listBlocks.map(function (lists) { | ||
return lists.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'field' ? printFieldType(current).length : 0); | ||
}, 0); | ||
}); | ||
var children = model.properties.map(function (prop, index, arr) { | ||
if (index > 0 && prop.type === 'field' && arr[index - 1].type !== 'field') { | ||
nameLengths.shift(); | ||
typeLengths.shift(); | ||
} | ||
return printProperty(prop, nameLengths[0], typeLengths[0]); | ||
}).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
var children = computePropertyFormatting(model.properties); | ||
return "\nmodel " + model.name + " {\n " + children + "\n}"; | ||
@@ -1010,4 +986,4 @@ } | ||
var listBlocks = list.reduce(function (memo, current, index, arr) { | ||
if (current.type !== 'assignment') return memo; | ||
if (index > 0 && arr[index - 1].type !== 'assignment') memo[++pos] = []; | ||
if (current.type === 'break') return memo; | ||
if (index > 0 && arr[index - 1].type === 'break') memo[++pos] = []; | ||
memo[pos].push(current); | ||
@@ -1022,3 +998,3 @@ return memo; | ||
return list.map(function (item, index, arr) { | ||
if (index > 0 && item.type === 'assignment' && arr[index - 1].type !== 'assignment') keyLengths.shift(); | ||
if (index > 0 && item.type !== 'break' && arr[index - 1].type === 'break') keyLengths.shift(); | ||
return printAssignment(item, keyLengths[0]); | ||
@@ -1028,3 +1004,31 @@ }).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
function computePropertyFormatting(list) { | ||
var pos = 0; | ||
var listBlocks = list.reduce(function (memo, current, index, arr) { | ||
if (current.type === 'break') return memo; | ||
if (index > 0 && arr[index - 1].type === 'break') memo[++pos] = []; | ||
memo[pos].push(current); | ||
return memo; | ||
}, [[]]); | ||
var nameLengths = listBlocks.map(function (lists) { | ||
return lists.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'field' ? current.name.length : 0); | ||
}, 0); | ||
}); | ||
var typeLengths = listBlocks.map(function (lists) { | ||
return lists.reduce(function (max, current) { | ||
return Math.max(max, current.type === 'field' ? printFieldType(current).length : 0); | ||
}, 0); | ||
}); | ||
return list.map(function (prop, index, arr) { | ||
if (index > 0 && prop.type !== 'break' && arr[index - 1].type === 'break') { | ||
nameLengths.shift(); | ||
typeLengths.shift(); | ||
} | ||
return printProperty(prop, nameLengths[0], typeLengths[0]); | ||
}).filter(Boolean).join('\n ').replace(/(\n\s*){3,}/g, '\n\n '); | ||
} | ||
export { getSchema, printSchema }; | ||
//# sourceMappingURL=prisma-ast.esm.js.map |
{ | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -85,54 +85,4 @@ import * as Types from './getSchema'; | ||
function printModel(model: Types.Model) { | ||
let pos = 0; | ||
const listBlocks = model.properties.reduce<Array<typeof model.properties>>( | ||
(memo, current, index, arr) => { | ||
if (current.type !== 'field') return memo; | ||
if (index > 0 && arr[index - 1].type !== 'field') memo[++pos] = []; | ||
memo[pos].push(current); | ||
return memo; | ||
}, | ||
[[]] | ||
); | ||
const children = computePropertyFormatting(model.properties); | ||
const nameLengths = listBlocks.map(lists => | ||
lists.reduce( | ||
(max, current) => | ||
Math.max( | ||
max, | ||
// perhaps someone more typescript-savy than I am can fix this | ||
current.type === 'field' ? current.name.length : 0 | ||
), | ||
0 | ||
) | ||
); | ||
const typeLengths = listBlocks.map(lists => | ||
lists.reduce( | ||
(max, current) => | ||
Math.max( | ||
max, | ||
// perhaps someone more typescript-savy than I am can fix this | ||
current.type === 'field' ? printFieldType(current).length : 0 | ||
), | ||
0 | ||
) | ||
); | ||
const children = model.properties | ||
.map((prop, index, arr) => { | ||
if ( | ||
index > 0 && | ||
prop.type === 'field' && | ||
arr[index - 1].type !== 'field' | ||
) { | ||
nameLengths.shift(); | ||
typeLengths.shift(); | ||
} | ||
return printProperty(prop, nameLengths[0], typeLengths[0]); | ||
}) | ||
.filter(Boolean) | ||
.join('\n ') | ||
.replace(/(\n\s*){3,}/g, '\n\n '); | ||
return ` | ||
@@ -253,4 +203,4 @@ model ${model.name} { | ||
(memo, current, index, arr) => { | ||
if (current.type !== 'assignment') return memo; | ||
if (index > 0 && arr[index - 1].type !== 'assignment') memo[++pos] = []; | ||
if (current.type === 'break') return memo; | ||
if (index > 0 && arr[index - 1].type === 'break') memo[++pos] = []; | ||
memo[pos].push(current); | ||
@@ -276,7 +226,3 @@ return memo; | ||
.map((item, index, arr) => { | ||
if ( | ||
index > 0 && | ||
item.type === 'assignment' && | ||
arr[index - 1].type !== 'assignment' | ||
) | ||
if (index > 0 && item.type !== 'break' && arr[index - 1].type === 'break') | ||
keyLengths.shift(); | ||
@@ -289,1 +235,57 @@ return printAssignment(item, keyLengths[0]); | ||
} | ||
function computePropertyFormatting( | ||
list: Array<Types.Break | Types.Comment | Types.Property> | ||
) { | ||
let pos = 0; | ||
const listBlocks = list.reduce<Array<typeof list>>( | ||
(memo, current, index, arr) => { | ||
if (current.type === 'break') return memo; | ||
if (index > 0 && arr[index - 1].type === 'break') memo[++pos] = []; | ||
memo[pos].push(current); | ||
return memo; | ||
}, | ||
[[]] | ||
); | ||
const nameLengths = listBlocks.map(lists => | ||
lists.reduce( | ||
(max, current) => | ||
Math.max( | ||
max, | ||
// perhaps someone more typescript-savy than I am can fix this | ||
current.type === 'field' ? current.name.length : 0 | ||
), | ||
0 | ||
) | ||
); | ||
const typeLengths = listBlocks.map(lists => | ||
lists.reduce( | ||
(max, current) => | ||
Math.max( | ||
max, | ||
// perhaps someone more typescript-savy than I am can fix this | ||
current.type === 'field' ? printFieldType(current).length : 0 | ||
), | ||
0 | ||
) | ||
); | ||
return list | ||
.map((prop, index, arr) => { | ||
if ( | ||
index > 0 && | ||
prop.type !== 'break' && | ||
arr[index - 1].type === 'break' | ||
) { | ||
nameLengths.shift(); | ||
typeLengths.shift(); | ||
} | ||
return printProperty(prop, nameLengths[0], typeLengths[0]); | ||
}) | ||
.filter(Boolean) | ||
.join('\n ') | ||
.replace(/(\n\s*){3,}/g, '\n\n '); | ||
} |
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
240226
3187