Skip to content

Commit

Permalink
feature: goldstein: broken string: improve
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Dec 22, 2024
1 parent bd08987 commit a7981c9
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 7 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions packages/goldstein/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 'hello world’;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 'hello world';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = ‘hello world';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 'hello world';
1 change: 1 addition & 0 deletions packages/keyword-broken-string/fixture/mobile-quote.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = ‘hello world’;
1 change: 1 addition & 0 deletions packages/keyword-broken-string/fixture/mobile-quote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 'hello world';
15 changes: 15 additions & 0 deletions packages/keyword-broken-string/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions packages/keyword-broken-string/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit a7981c9

Please sign in to comment.