diff --git a/README.md b/README.md index de2c52f8a..bf186accf 100644 --- a/README.md +++ b/README.md @@ -601,7 +601,7 @@ Built-in parsers: * [`flow`](https://github.com/facebook/flow/tree/master/src/parser) * [`typescript`](https://github.com/eslint/typescript-eslint-parser) _Since v1.4.0_ * [`postcss`](https://github.com/postcss/postcss) _Since v1.4.0_ - * [`json`](https://github.com/vtrushin/json-to-ast) _Since v1.5.0_ + * [`json`](https://github.com/babel/babylon/tree/f09eb3200f57ea94d51c2a5b1facf2149fb406bf#babylonparseexpressioncode-options) _Since v1.5.0_ * [`graphql`](https://github.com/graphql/graphql-js/tree/master/src/language) _Since v1.5.0_ [Custom parsers](#custom-parser-api) are also supported. _Since v1.5.0_ diff --git a/index.js b/index.js index edec1736d..cb351acc6 100644 --- a/index.js +++ b/index.js @@ -182,8 +182,6 @@ function isSourceElement(opts, node) { case "NumericLiteral": // JSON case "BooleanLiteral": // JSON case "NullLiteral": // JSON - case "json-identifier": // JSON - return opts.parser === "json"; case "FunctionDeclaration": case "BlockStatement": case "BreakStatement": diff --git a/package.json b/package.json index 6890006bc..f1b159426 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "main": "./index.js", "dependencies": { "babel-code-frame": "7.0.0-alpha.12", - "babylon": "7.0.0-beta.13", + "babylon": "7.0.0-beta.17", "chalk": "2.0.1", "cosmiconfig": "2.1.3", "dashify": "0.2.2", @@ -24,7 +24,6 @@ "graphql": "0.10.1", "ignore": "^3.3.3", "jest-validate": "20.0.3", - "json-to-ast": "2.0.0-alpha1.2", "minimatch": "3.0.4", "minimist": "1.2.0", "parse5": "3.0.2", diff --git a/scripts/build/build.js b/scripts/build/build.js index 4c7704477..953a296da 100755 --- a/scripts/build/build.js +++ b/scripts/build/build.js @@ -14,8 +14,7 @@ const parsers = [ "typescript", "graphql", "postcss", - "parse5", - "json" + "parse5" ]; process.env.PATH += path.delimiter + path.join(rootDir, "node_modules", ".bin"); diff --git a/src/parser-babylon.js b/src/parser-babylon.js index 65a5b5353..0adb9f652 100644 --- a/src/parser-babylon.js +++ b/src/parser-babylon.js @@ -2,7 +2,7 @@ const createError = require("./parser-create-error"); -function parse(text) { +function parse(text, parsers, opts) { // Inline the require to avoid loading all the JS if we don't use it const babylon = require("babylon"); @@ -27,12 +27,15 @@ function parse(text) { ] }; + const parseMethod = + opts && opts.parser === "json" ? "parseExpression" : "parse"; + let ast; try { - ast = babylon.parse(text, babylonOptions); + ast = babylon[parseMethod](text, babylonOptions); } catch (originalError) { try { - ast = babylon.parse( + ast = babylon[parseMethod]( text, Object.assign({}, babylonOptions, { strictMode: false }) ); diff --git a/src/parser-flow.js b/src/parser-flow.js index 2037e9490..d83260d2e 100644 --- a/src/parser-flow.js +++ b/src/parser-flow.js @@ -3,7 +3,7 @@ const createError = require("./parser-create-error"); const includeShebang = require("./parser-include-shebang"); -function parse(text) { +function parse(text /*, parsers, opts*/) { // Fixes Node 4 issue (#1986) "use strict"; // eslint-disable-line // Inline the require to avoid loading all the JS if we don't use it diff --git a/src/parser-graphql.js b/src/parser-graphql.js index 5f4ab6905..fe1e118e0 100644 --- a/src/parser-graphql.js +++ b/src/parser-graphql.js @@ -34,7 +34,7 @@ function removeTokens(node) { return node; } -function parse(text) { +function parse(text /*, parsers, opts*/) { // Inline the require to avoid loading all the JS if we don't use it const parser = require("graphql/language"); try { diff --git a/src/parser-json.js b/src/parser-json.js deleted file mode 100644 index ea103224d..000000000 --- a/src/parser-json.js +++ /dev/null @@ -1,82 +0,0 @@ -"use strict"; - -const createError = require("./parser-create-error"); - -function parse(text) { - const jsonToAst = require("json-to-ast"); - try { - const ast = jsonToAst(text); - return toBabylon(ast); - } catch (err) { - const firstNewlineIndex = err.message.indexOf("\n"); - const firstLine = err.message.slice(0, firstNewlineIndex); - const lastSpaceIndex = firstLine.lastIndexOf(" "); - const message = firstLine.slice(0, lastSpaceIndex); - const locString = firstLine.slice(lastSpaceIndex + 1); - const lineCol = locString.split(":").map(Number); - - throw createError("(json-to-ast) " + message, { - start: { - line: lineCol[0], - column: lineCol[1] - } - }); - } -} - -function toBabylon(node) { - const typeMap = { - object: "ObjectExpression", - property: "ObjectProperty", - identifier: "json-identifier", - array: "ArrayExpression" - }; - - const result = { - type: typeMap[node.type], - start: node.loc.start.offset, - end: node.loc.end.offset, - loc: node.loc - }; - - switch (node.type) { - case "object": - return Object.assign(result, { - properties: node.children.map(toBabylon) - }); - case "property": - return Object.assign(result, { - key: toBabylon(node.key), - value: toBabylon(node.value) - }); - case "identifier": - return Object.assign(result, { - value: node.value - }); - case "array": - return Object.assign(result, { - elements: node.children.map(toBabylon) - }); - case "literal": { - const constructorTypes = { - String: "StringLiteral", - Number: "NumericLiteral", - Boolean: "BooleanLiteral" - }; - - const value = JSON.parse(node.rawValue); - const type = - value === null - ? "NullLiteral" - : constructorTypes[value.constructor.name]; - - return Object.assign(result, { - type: type, - value: value, - extra: { raw: node.rawValue } - }); - } - } -} - -module.exports = parse; diff --git a/src/parser-parse5.js b/src/parser-parse5.js index 35334f233..eee3eacd5 100644 --- a/src/parser-parse5.js +++ b/src/parser-parse5.js @@ -2,7 +2,7 @@ // const createError = require("./parser-create-error"); -function parse(text) { +function parse(text /*, parsers, opts*/) { // Inline the require to avoid loading all the JS if we don't use it const parse5 = require("parse5"); try { diff --git a/src/parser-postcss.js b/src/parser-postcss.js index f4b2981d4..6d5e829af 100644 --- a/src/parser-postcss.js +++ b/src/parser-postcss.js @@ -208,7 +208,7 @@ function requireParser(isSCSS) { return require("postcss-less"); } -function parse(text) { +function parse(text /*, parsers, opts*/) { const isLikelySCSS = !!text.match(/(\w\s*: [^}:]+|#){|@import[^\n]+(url|,)/); try { return parseWithParser(requireParser(isLikelySCSS), text); diff --git a/src/parser-typescript.js b/src/parser-typescript.js index 44414b7c9..6c22e9e5b 100644 --- a/src/parser-typescript.js +++ b/src/parser-typescript.js @@ -3,7 +3,7 @@ const createError = require("./parser-create-error"); const includeShebang = require("./parser-include-shebang"); -function parse(text) { +function parse(text /*, parsers, opts*/) { const jsx = isProbablyJsx(text); let ast; try { diff --git a/src/parser.js b/src/parser.js index 933accb78..57923a820 100644 --- a/src/parser.js +++ b/src/parser.js @@ -22,7 +22,7 @@ const parsers = { return eval("require")("./parser-postcss"); }, get json() { - return eval("require")("./parser-json"); + return eval("require")("./parser-babylon"); } }; diff --git a/src/printer.js b/src/printer.js index c89cac4d2..07143925a 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2636,8 +2636,6 @@ function genericPrintNoParens(path, options, print, args) { return path.call(bodyPath => { return printStatementSequence(bodyPath, options, print); }, "body"); - case "json-identifier": - return '"' + n.value + '"'; default: /* istanbul ignore next */ @@ -2709,7 +2707,12 @@ function printPropertyKey(path, options, print) { const node = path.getNode(); const key = node.key; - if (isStringLiteral(key) && isIdentifierName(key.value) && !node.computed) { + if ( + isStringLiteral(key) && + isIdentifierName(key.value) && + !node.computed && + options.parser !== "json" + ) { // 'a' -> a return path.call( keyPath => comments.printComments(keyPath, () => key.value, options), diff --git a/tests/json/__snapshots__/jsfmt.spec.js.snap b/tests/json/__snapshots__/jsfmt.spec.js.snap index b7df329ce..93e1dc130 100644 --- a/tests/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/json/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`block-comment.json 1`] = ` +{/*comment*/"K":"V"} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +{ /*comment*/ "K": "V" } + +`; + +exports[`block-comment.json 2`] = ` +{/*comment*/"K":"V"} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +{ /*comment*/ "K": "V" } + +`; + exports[`boolean.json 1`] = ` true ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -14,6 +28,32 @@ true `; +exports[`line-comment.json 1`] = ` +{ + //comment + "K":"V" +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +{ + //comment + "K": "V" +} + +`; + +exports[`line-comment.json 2`] = ` +{ + //comment + "K":"V" +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +{ + //comment + "K": "V" +} + +`; + exports[`multi-line.json 1`] = ` {"key1":[true,false,null],"key2":{"key3":[1,2,"3", 1e10,1e-3]}} @@ -168,7 +208,8 @@ exports[`pass1.json 1`] = ` "compact": [1, 2, 3, 4, 5, 6, 7], "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}", "quotes": "" \\u0022 %22 0x22 034 "", - "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "A key can be any string" + "/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?": + "A key can be any string" }, 0.5, 98.6, @@ -285,7 +326,8 @@ exports[`pass1.json 2`] = ` "compact": [1, 2, 3, 4, 5, 6, 7], "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}", "quotes": "" \\u0022 %22 0x22 034 "", - "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "A key can be any string" + "/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?": + "A key can be any string" }, 0.5, 98.6, diff --git a/tests/json/block-comment.json b/tests/json/block-comment.json new file mode 100644 index 000000000..d353120de --- /dev/null +++ b/tests/json/block-comment.json @@ -0,0 +1 @@ +{/*comment*/"K":"V"} diff --git a/tests/json/line-comment.json b/tests/json/line-comment.json new file mode 100644 index 000000000..6431cdc1e --- /dev/null +++ b/tests/json/line-comment.json @@ -0,0 +1,4 @@ +{ + //comment + "K":"V" +} diff --git a/website/static/lib/index.js b/website/static/lib/index.js index 5c3a77115..ab7e44e1f 100644 --- a/website/static/lib/index.js +++ b/website/static/lib/index.js @@ -3020,7 +3020,7 @@ var parsers = { return eval("require")("./parser-postcss"); }, get json() { - return eval("require")("./parser-json"); + return eval("require")("./parser-babylon"); } }; diff --git a/website/static/lib/parser-json.js b/website/static/lib/parser-json.js deleted file mode 100644 index ae639388b..000000000 --- a/website/static/lib/parser-json.js +++ /dev/null @@ -1,8 +0,0 @@ -var json = (function () { -var commonjsGlobal$1 = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; - -function createError$1(e,t){const n=new SyntaxError(e+" ("+t.start.line+":"+t.start.column+")");return n.loc=t,n}function createCommonjsModule$$1(e,t){return t={exports:{}},e(t,t.exports),t.exports}function parse(e){const t=parse$1;try{const n=t(e);return toBabylon(n)}catch(e){const t=e.message.indexOf("\n"),n=e.message.slice(0,t),r=n.lastIndexOf(" "),o=n.slice(0,r),l=n.slice(r+1),a=l.split(":").map(Number);throw createError("(json-to-ast) "+o,{start:{line:a[0],column:a[1]}})}}function toBabylon(e){const t={type:{object:"ObjectExpression",property:"ObjectProperty",identifier:"json-identifier",array:"ArrayExpression"}[e.type],start:e.loc.start.offset,end:e.loc.end.offset,loc:e.loc};switch(e.type){case"object":return Object.assign(t,{properties:e.children.map(toBabylon)});case"property":return Object.assign(t,{key:toBabylon(e.key),value:toBabylon(e.value)});case"identifier":return Object.assign(t,{value:e.value});case"array":return Object.assign(t,{elements:e.children.map(toBabylon)});case"literal":{const n={String:"StringLiteral",Number:"NumericLiteral",Boolean:"BooleanLiteral"},r=JSON.parse(e.rawValue),o=null===r?"NullLiteral":n[r.constructor.name];return Object.assign(t,{type:o,value:r,extra:{raw:e.rawValue}})}}}var parserCreateError=createError$1; var commonjsGlobal$$1="undefined"!=typeof window?window:"undefined"!=typeof commonjsGlobal$1?commonjsGlobal$1:"undefined"!=typeof self?self:{}; var parse$1=createCommonjsModule$$1(function(e,t){!function(t,n){n(e);}(0,function(e){function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function r(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t);}function o(e,t,n){return e.split(/\n|\r\n?|\f/)[t-1]+"\n"+(new Array(n).join(" ")+"^")}function l(e){return e>="1"&&e<="9"}function a(e){return e>="0"&&e<="9"}function c(e){return a(e)||e>="a"&&e<="f"||e>="A"&&e<="F"}function s(e){return"e"===e||"E"===e}function u(e,t,n,r){var o=e.charAt(t);if("\r"===o)t++,n++,r=1,"\n"===e.charAt(t)&&t++;else if("\n"===o)t++,n++,r=1;else{if("\t"!==o&&" "!==o)return null;t++,r++;}return{index:t,line:n,column:r}}function i(e,t,n,r){var o=e.charAt(t);return o in x?{type:x[o],line:n,column:r+1,index:t+1,value:null}:null}function f(e,t,n,r){for(var o in S)if(S.hasOwnProperty(o)&&e.substr(t,o.length)===o){var l=S[o],a=l.type,c=l.value;return{type:a,line:n,column:r+o.length,index:t+o.length,value:c}}return null}function p(e,t,n,r){for(var o=t,l="",a=N._START_;t0?{type:I.NUMBER,line:n,column:r+c-o,index:c,value:parseFloat(e.substring(o,c))}:null}function d(e,t){for(var n=1,r=1,o=0,l=[];o at "+t+":"+n}},C={cannotTokenizeSymbol:function(e,t,n){return"Cannot tokenize symbol <"+e+"> at "+t+":"+n}},I={LEFT_BRACE:0,RIGHT_BRACE:1,LEFT_BRACKET:2,RIGHT_BRACKET:3,COLON:4,COMMA:5,STRING:6,NUMBER:7,TRUE:8,FALSE:9,NULL:10},x={"{":I.LEFT_BRACE,"}":I.RIGHT_BRACE,"[":I.LEFT_BRACKET,"]":I.RIGHT_BRACKET,":":I.COLON,",":I.COMMA},S={true:{type:I.TRUE,value:!0},false:{type:I.FALSE,value:!1},null:{type:I.NULL,value:null}},N={_START_:0,START_QUOTE_OR_CHAR:1,ESCAPE:2},k={'"':0,"\\":1,"/":2,b:3,f:4,n:5,r:6,t:7,u:8},P={_START_:0,MINUS:1,ZERO:2,DIGIT:3,POINT:4,DIGIT_FRACTION:5,EXP:6,EXP_DIGIT_OR_SIGN:7},g=[I.STRING,I.NUMBER,I.TRUE,I.FALSE,I.NULL],w={_START_:0,OPEN_OBJECT:1,PROPERTY:2,COMMA:3},L={_START_:0,KEY:1,COLON:2},M={_START_:0,OPEN_ARRAY:1,VALUE:2,COMMA:3},B={verbose:!0,source:null},G=function(e,t){var n=d(e,t=y({},B,t));0===n.length&&m(h.unexpectedEnd());var r=O(e,n,0,t);if(r.index===n.length)return r.value;var o=n[r.index];m(h.unexpectedToken(e.substring(o.loc.start.offset,o.loc.end.offset),o.loc.start.line,o.loc.start.column),e,o.loc.start.line,o.loc.start.column);};e.exports=G;});});const createError=parserCreateError;var parserJson=parse;var parserJson_1=parserJson; - -return parserJson_1; - -}()); diff --git a/website/static/service-worker.js b/website/static/service-worker.js index 9cceb5f17..b6ee9ec58 100644 --- a/website/static/service-worker.js +++ b/website/static/service-worker.js @@ -14,7 +14,6 @@ toolbox.precache([ "lib/parser-postcss.js", "lib/parser-flow.js", "lib/parser-graphql.js", - "lib/parser-json.js", "playground.js", "lib/sw-toolbox.js", "lib/sw-toolbox-companion.js", diff --git a/yarn.lock b/yarn.lock index bd1927bb5..a73487b35 100644 --- a/yarn.lock +++ b/yarn.lock @@ -664,9 +664,9 @@ babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.24.1, babel-types@^6.25 lodash "^4.2.0" to-fast-properties "^1.0.1" -babylon@7.0.0-beta.13: - version "7.0.0-beta.13" - resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.13.tgz#12425c1bfd9498be419021ed36b43fe4f0289c0a" +babylon@7.0.0-beta.17: + version "7.0.0-beta.17" + resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.17.tgz#2aad4d6764f409dfb3ac216d855dc93d70d37911" babylon@^6.13.0, babylon@^6.17.2: version "6.17.3" @@ -2421,10 +2421,6 @@ json-stringify-safe@~5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" -json-to-ast@2.0.0-alpha1.2: - version "2.0.0-alpha1.2" - resolved "https://registry.yarnpkg.com/json-to-ast/-/json-to-ast-2.0.0-alpha1.2.tgz#fe27fd89eb639eca1e01f578e79d46ee36e238e8" - json5@^0.5.0, json5@^0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"