Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checkBrowserSupported where target should not contain itself. #4

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions __test__/check_browser_supported/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Multiple occurrences of the same syntax results in multiple calculations. 1`] = `
[
{
"astNode": {
"loc": {
"end": {
"col": 18,
"line": 1,
},
"start": {
"col": 11,
"line": 1,
},
},
"span": {
"end": 17,
"start": 10,
},
},
"compat": {
"description": "空值合并运算符 (??)",
"mdnUrl": "https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator",
"name": "operators.nullish_coalescing",
"support": {
"chrome": "80",
"chromeAndroid": "80",
"deno": "1.0.0",
"edge": "80",
"firefox": "72",
"firefoxAndroid": "72",
"node": "14.0.0",
"safari": "13.1",
"safariIos": "13.1",
},
"tags": [
"web-features:snapshot:ecmascript-2020",
],
},
"filePath": "",
"name": "operators.nullish_coalescing",
},
]
`;

exports[`Multiple occurrences of the same syntax results in multiple calculations. 2`] = `
[
{
"astNode": {
"loc": {
"end": {
"col": 18,
"line": 1,
},
"start": {
"col": 11,
"line": 1,
},
},
"span": {
"end": 17,
"start": 10,
},
},
"compat": {
"description": "空值合并运算符 (??)",
"mdnUrl": "https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator",
"name": "operators.nullish_coalescing",
"support": {
"chrome": "80",
"chromeAndroid": "80",
"deno": "1.0.0",
"edge": "80",
"firefox": "72",
"firefoxAndroid": "72",
"node": "14.0.0",
"safari": "13.1",
"safariIos": "13.1",
},
"tags": [
"web-features:snapshot:ecmascript-2020",
],
},
"filePath": "",
"name": "operators.nullish_coalescing",
},
{
"astNode": {
"loc": {
"end": {
"col": 37,
"line": 1,
},
"start": {
"col": 30,
"line": 1,
},
},
"span": {
"end": 36,
"start": 29,
},
},
"compat": {
"description": "空值合并运算符 (??)",
"mdnUrl": "https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator",
"name": "operators.nullish_coalescing",
"support": {
"chrome": "80",
"chromeAndroid": "80",
"deno": "1.0.0",
"edge": "80",
"firefox": "72",
"firefoxAndroid": "72",
"node": "14.0.0",
"safari": "13.1",
"safariIos": "13.1",
},
"tags": [
"web-features:snapshot:ecmascript-2020",
],
},
"filePath": "",
"name": "operators.nullish_coalescing",
},
]
`;
3 changes: 3 additions & 0 deletions __test__/check_browser_supported/features/normal/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = 1;

const b = cc ?? 3;
3 changes: 3 additions & 0 deletions __test__/check_browser_supported/features/normal/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const name = async () => {
await Promise.resolve();
};
32 changes: 32 additions & 0 deletions __test__/check_browser_supported/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, test } from "vitest";
import { fileURLToPath } from "node:url";
import path, { dirname } from "node:path";
import { checkBrowserSupportedWithSourceCode, checkBrowserSupported } from "../../index.js";

const __dirname = dirname(fileURLToPath(import.meta.url));

test("There are 4 syntaxes under normal that are incompatible under chrome 40", () => {
const cwd = path.resolve(__dirname, "features", "normal");

const response = checkBrowserSupported({ chrome: "40" }, { cwd });

expect(response.length).toBe(4);
});

test("There are 3 syntaxes under normal that are incompatible under chrome 45", () => {
const cwd = path.resolve(__dirname, "features", "normal");

const response = checkBrowserSupported({ chrome: "45" }, { cwd });

expect(response.length).toBe(3);
});

test("Multiple occurrences of the same syntax results in multiple calculations.", () => {
const response1 = checkBrowserSupportedWithSourceCode({ chrome: "70" }, "const b = cc ?? 3;");
expect(response1.length).toBe(1);
expect(response1).toMatchSnapshot();

const response2 = checkBrowserSupportedWithSourceCode({ chrome: "70" }, "const b = cc ?? 3; const c = cc ?? 3;");
expect(response2.length).toBe(2);
expect(response2).toMatchSnapshot();
});
4 changes: 2 additions & 2 deletions crates/check_browser_supported/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn check_browser_supported_with_source_code(
) -> Result<Vec<CompatBox>> {
debug!("User-specified browser target: {:?}", target);

let chrome_queries = format!("chrome >= {}", target.chrome);
let chrome_queries = format!("chrome > {}", target.chrome);

let browser_list = resolve(&[chrome_queries], &Opts::default())
.map_err(|err| Error::new(napi::Status::GenericFailure, err.to_string()))?;
Expand Down Expand Up @@ -175,7 +175,7 @@ pub fn check_browser_supported(
) -> Result<Vec<CompatBox>> {
debug!("User-specified browser target: {:?}", target);

let chrome_queries = format!("chrome >= {}", target.chrome);
let chrome_queries = format!("chrome > {}", target.chrome);

let browser_list = resolve(&[chrome_queries], &Opts::default())
.map_err(|err| Error::new(napi::Status::GenericFailure, err.to_string()))?;
Expand Down