From e0b3e26c5ec1feb95a3228a35acf82dd279d7895 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 26 Mar 2022 13:42:33 +0100 Subject: [PATCH] Add tests for import assertions Closes #90. I thought new code was needed to support them, but it turns out no changes were needed. --- babel.config.json | 5 +++- package-lock.json | 25 ++++++++++++++++++++ package.json | 1 + test/imports.test.js | 56 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/babel.config.json b/babel.config.json index 15c76ce..7c6968e 100644 --- a/babel.config.json +++ b/babel.config.json @@ -1,3 +1,6 @@ { - "plugins": ["@babel/plugin-transform-flow-strip-types"] + "plugins": [ + "@babel/plugin-syntax-import-assertions", + "@babel/plugin-transform-flow-strip-types" + ] } diff --git a/package-lock.json b/package-lock.json index 87950b6..543d443 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,6 +6,7 @@ "": { "devDependencies": { "@babel/eslint-parser": "7.17.0", + "@babel/plugin-syntax-import-assertions": "7.16.7", "@babel/plugin-transform-flow-strip-types": "7.16.7", "@typescript-eslint/parser": "5.16.0", "eslint": "8.12.0", @@ -412,6 +413,21 @@ "@babel/core": "^7.0.0-0" } }, + "node_modules/@babel/plugin-syntax-import-assertions": { + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-import-assertions/-/plugin-syntax-import-assertions-7.16.7.tgz", + "integrity": "sha512-DoM/wsaMaDXpM2fa+QkZeqqfYs340WTY+boLRiZ7ckqt3PAFt1CdGmMXVniFCcN8RuStim2Z4Co3bIKdWjTXIQ==", + "dev": true, + "dependencies": { + "@babel/helper-plugin-utils": "^7.16.7" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, "node_modules/@babel/plugin-syntax-import-meta": { "version": "7.10.4", "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-import-meta/-/plugin-syntax-import-meta-7.10.4.tgz", @@ -7918,6 +7934,15 @@ "@babel/helper-plugin-utils": "^7.16.7" } }, + "@babel/plugin-syntax-import-assertions": { + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-import-assertions/-/plugin-syntax-import-assertions-7.16.7.tgz", + "integrity": "sha512-DoM/wsaMaDXpM2fa+QkZeqqfYs340WTY+boLRiZ7ckqt3PAFt1CdGmMXVniFCcN8RuStim2Z4Co3bIKdWjTXIQ==", + "dev": true, + "requires": { + "@babel/helper-plugin-utils": "^7.16.7" + } + }, "@babel/plugin-syntax-import-meta": { "version": "7.10.4", "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-import-meta/-/plugin-syntax-import-meta-7.10.4.tgz", diff --git a/package.json b/package.json index 6499a10..a4b887e 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "devDependencies": { "@babel/eslint-parser": "7.17.0", + "@babel/plugin-syntax-import-assertions": "7.16.7", "@babel/plugin-transform-flow-strip-types": "7.16.7", "@typescript-eslint/parser": "5.16.0", "eslint": "8.12.0", diff --git a/test/imports.test.js b/test/imports.test.js index 8a91dbd..039aa25 100644 --- a/test/imports.test.js +++ b/test/imports.test.js @@ -1576,6 +1576,7 @@ const flowTests = { `import type a, {b} from "a"`, `import type {} from "a"`, `import type { } from "a"`, + `import json from "./foo.json" assert { type: "json" };`, // typeof `import typeof a from "a"`, @@ -1672,6 +1673,33 @@ const flowTests = { errors: 1, }, + // Import assertions. + { + code: input` + |import json from "./foo.json" assert { type: "json" }; + |import {b, a} from "./bar.json" assert { + | // json + | type: "json", + | a: "b", + |} /* bar */ /* end + | comment */ + |;[].forEach() + `, + output: (actual) => { + expect(actual).toMatchInlineSnapshot(` + |import {a,b} from "./bar.json" assert { + | // json + | type: "json", + | a: "b", + |} /* bar */ + |import json from "./foo.json" assert { type: "json" };/* end + | comment */ + |;[].forEach() + `); + }, + errors: 1, + }, + // https://github.com/graphql/graphql-js/blob/64b194c6c9b9aaa1c139f1b7c3692a6ef851928e/src/execution/execute.js#L10-L69 { code: input` @@ -1812,6 +1840,7 @@ const typescriptTests = { `import type {a} from "a"`, `import type {} from "a"`, `import type { } from "a"`, + `import json from "./foo.json" assert { type: "json" };`, // type specifiers. `import { type b, type c, a } from "a"`, @@ -1939,6 +1968,33 @@ const typescriptTests = { }, errors: 1, }, + + // Import assertions. + { + code: input` + |import json from "./foo.json" assert { type: "json" }; + |import {b, a} from "./bar.json" assert { + | // json + | type: "json", + | a: "b", + |} /* bar */ /* end + | comment */ + |;[].forEach() + `, + output: (actual) => { + expect(actual).toMatchInlineSnapshot(` + |import {a,b} from "./bar.json" assert { + | // json + | type: "json", + | a: "b", + |} /* bar */ + |import json from "./foo.json" assert { type: "json" };/* end + | comment */ + |;[].forEach() + `); + }, + errors: 1, + }, ], };