From a7981c912bf09a212eb7181b26010527ef4a1a87 Mon Sep 17 00:00:00 2001 From: coderiaser Date: Sun, 22 Dec 2024 13:58:21 +0200 Subject: [PATCH] feature: goldstein: broken string: improve --- README.md | 14 +++++++------- packages/goldstein/index.spec.js | 14 ++++++++++++++ .../fixture/mobile-quote-close.gs | 1 + .../fixture/mobile-quote-close.js | 1 + .../fixture/mobile-quote-open.gs | 1 + .../fixture/mobile-quote-open.js | 1 + .../keyword-broken-string/fixture/mobile-quote.gs | 1 + .../keyword-broken-string/fixture/mobile-quote.js | 1 + packages/keyword-broken-string/index.js | 15 +++++++++++++++ packages/keyword-broken-string/index.spec.js | 15 +++++++++++++++ 10 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 packages/keyword-broken-string/fixture/mobile-quote-close.gs create mode 100644 packages/keyword-broken-string/fixture/mobile-quote-close.js create mode 100644 packages/keyword-broken-string/fixture/mobile-quote-open.gs create mode 100644 packages/keyword-broken-string/fixture/mobile-quote-open.js create mode 100644 packages/keyword-broken-string/fixture/mobile-quote.gs create mode 100644 packages/keyword-broken-string/fixture/mobile-quote.js diff --git a/README.md b/README.md index a6e3446..f1a55a4 100644 --- a/README.md +++ b/README.md @@ -451,18 +451,18 @@ function hello() {} ### Broken String -When you accidentally broke string: +When you accidentally broke string, Goldstein will fix it: -```gs -const a = 'hello -const b = 'world'; +```diff +-const a = 'hello ++const a = 'hello'; +-const a = ‘hello world’; ++const a = 'hello world'; ``` -Goldstein will fix it to: +to: ```js -const a = 'hello'; -const b = 'world'; ``` ### Missing Initializer diff --git a/packages/goldstein/index.spec.js b/packages/goldstein/index.spec.js index 1724040..7bb0321 100644 --- a/packages/goldstein/index.spec.js +++ b/packages/goldstein/index.spec.js @@ -621,3 +621,17 @@ test('goldstein: wrong brace', (t) => { t.equal(result, expected); t.end(); }); + +test('goldstein: punctuation: mobile quote', (t) => { + const result = compile(montag` + const a = ‘hello world’; + `); + + const expected = montag` + const a = 'hello world'; + + `; + + t.equal(result, expected); + t.end(); +}); diff --git a/packages/keyword-broken-string/fixture/mobile-quote-close.gs b/packages/keyword-broken-string/fixture/mobile-quote-close.gs new file mode 100644 index 0000000..22310f3 --- /dev/null +++ b/packages/keyword-broken-string/fixture/mobile-quote-close.gs @@ -0,0 +1 @@ +const a = 'hello world’; diff --git a/packages/keyword-broken-string/fixture/mobile-quote-close.js b/packages/keyword-broken-string/fixture/mobile-quote-close.js new file mode 100644 index 0000000..a4e9aa3 --- /dev/null +++ b/packages/keyword-broken-string/fixture/mobile-quote-close.js @@ -0,0 +1 @@ +const a = 'hello world'; diff --git a/packages/keyword-broken-string/fixture/mobile-quote-open.gs b/packages/keyword-broken-string/fixture/mobile-quote-open.gs new file mode 100644 index 0000000..7011229 --- /dev/null +++ b/packages/keyword-broken-string/fixture/mobile-quote-open.gs @@ -0,0 +1 @@ +const a = ‘hello world'; \ No newline at end of file diff --git a/packages/keyword-broken-string/fixture/mobile-quote-open.js b/packages/keyword-broken-string/fixture/mobile-quote-open.js new file mode 100644 index 0000000..a4e9aa3 --- /dev/null +++ b/packages/keyword-broken-string/fixture/mobile-quote-open.js @@ -0,0 +1 @@ +const a = 'hello world'; diff --git a/packages/keyword-broken-string/fixture/mobile-quote.gs b/packages/keyword-broken-string/fixture/mobile-quote.gs new file mode 100644 index 0000000..69afa5d --- /dev/null +++ b/packages/keyword-broken-string/fixture/mobile-quote.gs @@ -0,0 +1 @@ +const a = ‘hello world’; \ No newline at end of file diff --git a/packages/keyword-broken-string/fixture/mobile-quote.js b/packages/keyword-broken-string/fixture/mobile-quote.js new file mode 100644 index 0000000..a4e9aa3 --- /dev/null +++ b/packages/keyword-broken-string/fixture/mobile-quote.js @@ -0,0 +1 @@ +const a = 'hello world'; diff --git a/packages/keyword-broken-string/index.js b/packages/keyword-broken-string/index.js index e0ab91e..35e9171 100644 --- a/packages/keyword-broken-string/index.js +++ b/packages/keyword-broken-string/index.js @@ -1,7 +1,17 @@ import {tokTypes as tt} from '../operator/index.js'; +const QUOTE = `'`.charCodeAt(0); +const MOBILE_CLOSE_QUOTE = '’'.charCodeAt(0); +const MOBILE_OPEN_QUOTE = '‘'.charCodeAt(0); + export default function keywordBrokenString(Parser) { return class extends Parser { + getTokenFromCode(code) { + if (code === MOBILE_OPEN_QUOTE) + return this.readString(MOBILE_CLOSE_QUOTE); + + return super.getTokenFromCode(code); + } parseVarStatement(node, kind, allowMissingInitializer) { this.next(); this.parseVar(node, false, kind, allowMissingInitializer); @@ -20,8 +30,13 @@ export default function keywordBrokenString(Parser) { if (!ch) break; + if (ch === QUOTE || ch === MOBILE_CLOSE_QUOTE) + break; + + /* c8 ignore start */ if (ch === quote) break; + /* c8 ignore end */ /* c8 ignore start */ if (ch === 92) { diff --git a/packages/keyword-broken-string/index.spec.js b/packages/keyword-broken-string/index.spec.js index d9e5224..44edcf8 100644 --- a/packages/keyword-broken-string/index.spec.js +++ b/packages/keyword-broken-string/index.spec.js @@ -17,3 +17,18 @@ test('goldstein: broken-string: infinite', (t) => { t.compile('infinite'); t.end(); }); + +test('goldstein: punctuation: mobile-quote', (t) => { + t.compile('mobile-quote'); + t.end(); +}); + +test('goldstein: punctuation: mobile-quote-open', (t) => { + t.compile('mobile-quote-open'); + t.end(); +}); + +test('goldstein: punctuation: mobile-quote-close', (t) => { + t.compile('mobile-quote-close'); + t.end(); +});