Skip to content

Commit

Permalink
Merge pull request #1285 from Quramy/upgrade_fretted_strings
Browse files Browse the repository at this point in the history
chore: Upgrade to fretted-strings v2
  • Loading branch information
Quramy committed Apr 2, 2024
2 parents c7a1ffb + d2a109a commit c46a365
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 246 deletions.
6 changes: 2 additions & 4 deletions e2e/lang-server-specs/completions.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
const assert = require('assert');
const path = require('path');
const { mark } = require('fretted-strings');
const { extract } = require('fretted-strings');

function findResponse(responses, commandName) {
return responses.find(response => response.command === commandName);
}

async function run(server) {
const file = path.resolve(__dirname, '../../project-fixtures/simple-prj/main.ts');
const frets = {};
const fileContent = mark(
const [fileContent, frets] = extract(
`
const q = gql\`query {
%%% \\ ^ %%%
%%% \\ p %%%
`,
frets,
);
server.send({ command: 'open', arguments: { file, fileContent, scriptKindName: 'TS' } });
await server.waitEvent('projectLoadingFinish');
Expand Down
6 changes: 2 additions & 4 deletions e2e/lang-server-specs/definition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const assert = require('assert');
const path = require('path');
const { mark } = require('fretted-strings');
const { extract } = require('fretted-strings');

function findResponse(responses, commandName) {
return responses.find(response => response.command === commandName);
Expand All @@ -9,15 +9,14 @@ function findResponse(responses, commandName) {
async function run(server) {
const fileFragments = path.resolve(__dirname, '../../project-fixtures/simple-prj/fragments.ts');
const fileMain = path.resolve(__dirname, '../../project-fixtures/simple-prj/main.ts');
const frets = {};
const fileFragmentsContent = `
const fragment = gql\`
fragment MyFragment on Query {
hello
}
\`;
`;
const fileMainContent = mark(
const [fileMainContent, frets] = extract(
`
const query = gql\`
query MyQuery {
Expand All @@ -27,7 +26,6 @@ async function run(server) {
}
\`;
`,
frets,
);
server.send({
command: 'open',
Expand Down
6 changes: 2 additions & 4 deletions e2e/lang-server-specs/diagnostics-with-update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const assert = require('assert');
const path = require('path');
const { mark } = require('fretted-strings');
const { extract } = require('fretted-strings');

function findResponse(responses, eventName) {
return responses.find(response => response.event === eventName);
Expand All @@ -14,15 +14,13 @@ const f = gql\`fragment MyFragment on Query { hello }\`;
`;

const fileMain = path.resolve(__dirname, '../../project-fixtures/simple-prj/main.ts');
const frets = {};
const fileMainContent = mark(
const [fileMainContent, frets] = extract(
`
import gql from 'graphql-tag';
const q = gql\`query MyQuery { }\`;
%%% \\ ^ %%%
%%% \\ p %%%
`,
frets,
);

server.send({
Expand Down
6 changes: 2 additions & 4 deletions e2e/lang-server-specs/quickinfo.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
const assert = require('assert');
const path = require('path');
const { mark } = require('fretted-strings');
const { extract } = require('fretted-strings');

function findResponse(responses, commandName) {
return responses.find(response => response.command === commandName);
}

async function run(server) {
const file = path.resolve(__dirname, '../../project-fixtures/simple-prj/main.ts');
const frets = {};
const fileContent = mark(
const [fileContent, frets] = extract(
`
const q = gql\`query {
hello
%%% ^ %%%
%%% p %%%
}
`,
frets,
);
server.send({
command: 'open',
Expand Down
11 changes: 5 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"c8": "9.1.0",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"fretted-strings": "1.0.1",
"fretted-strings": "2.0.0",
"glob": "10.3.12",
"graphql": "16.8.1",
"graphql-config": "5.0.3",
Expand Down
40 changes: 20 additions & 20 deletions src/errors/__snapshots__/error-reporter.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ErrorReporter outputError ErrorWithLocation should output location of errors in human readable format 1`] = `
"main.ts:2:25 - some error
"main.ts:2:27 - some error
1
2 const query = invalidQuery;
~~~~~~~~~~~~
2 const query = invalidQuery;
~~~~~~~~~~~~
"
`;

exports[`ErrorReporter outputError ErrorWithLocation should output location of errors in human readable format with 2 lines 1`] = `
"main.ts:3:19 - some error
2 const query = gql\`;
3 query MyQuery {
~~~~~~~~~
4 name
~~~~~~~~~~~~~~~~~~
5 }
"main.ts:3:21 - some error
2 const query = gql\`;
3 query MyQuery {
~~~~~~~~~
4 name
~~~~~~~~~~~~~~~~~~~~
5 }
"
`;

exports[`ErrorReporter outputError ErrorWithLocation should output location of errors in human readable format with 3 or more lines 1`] = `
"main.ts:3:19 - some error
2 const query = gql\`;
3 query MyQuery {
~~~~~~~~~
4 id
~~~~~~~~~~~~~~~~
5 name
"main.ts:3:21 - some error
2 const query = gql\`;
3 query MyQuery {
~~~~~~~~~
4 id
~~~~~~~~~~~~~~~~~~
6 }
5 name
~~~~~~~~~~~~~~~~~~~~
6 }
"
`;

Expand Down
79 changes: 38 additions & 41 deletions src/errors/error-reporter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mark, Frets } from 'fretted-strings';
import extract from 'fretted-strings';
import { ErrorReporter } from './error-reporter';
import { ErrorWithLocation, ErrorWithoutLocation } from '.';
import { clearColor } from '../string-util';
Expand All @@ -25,18 +25,17 @@ describe(ErrorReporter, () => {
it('should output location of errors in human readable format', () => {
let message: string = '';
const reporter = new ErrorReporter('/prj', msg => (message = msg));
const frets: Frets = {};
const [content, frets] = extract(
`
const query = invalidQuery;
%%% ^ ^ %%%
%%% a1 a2 %%%
`,
);
reporter.outputError(
new ErrorWithLocation('some error', {
fileName: '/prj/main.ts',
content: mark(
`
const query = invalidQuery;
%%% ^ ^ %%%
%%% a1 a2 %%%
`,
frets,
),
content,
start: frets.a1.pos,
end: frets.a2.pos,
}),
Expand All @@ -47,24 +46,23 @@ describe(ErrorReporter, () => {
it('should output location of errors in human readable format with 2 lines', () => {
let message: string = '';
const reporter = new ErrorReporter('/prj', msg => (message = msg));
const frets: Frets = {};
const [content, frets] = extract(
`
const query = gql\`;
query MyQuery {
%%% ^ %%%
%%% a1 %%%
name
%%% ^ %%%
%%% a2 %%%
}
\`:
`,
);
reporter.outputError(
new ErrorWithLocation('some error', {
fileName: '/prj/main.ts',
content: mark(
`
const query = gql\`;
query MyQuery {
%%% ^ %%%
%%% a1 %%%
name
%%% ^ %%%
%%% a2 %%%
}
\`:
`,
frets,
),
content,
start: frets.a1.pos,
end: frets.a2.pos,
}),
Expand All @@ -75,25 +73,24 @@ describe(ErrorReporter, () => {
it('should output location of errors in human readable format with 3 or more lines', () => {
let message: string = '';
const reporter = new ErrorReporter('/prj', msg => (message = msg));
const frets: Frets = {};
const [content, frets] = extract(
`
const query = gql\`;
query MyQuery {
%%% ^ %%%
%%% a1 %%%
id
name
%%% ^ %%%
%%% a2 %%%
}
\`:
`,
);
reporter.outputError(
new ErrorWithLocation('some error', {
fileName: '/prj/main.ts',
content: mark(
`
const query = gql\`;
query MyQuery {
%%% ^ %%%
%%% a1 %%%
id
name
%%% ^ %%%
%%% a2 %%%
}
\`:
`,
frets,
),
content,
start: frets.a1.pos,
end: frets.a2.pos,
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from 'typescript';
import { mark, type Frets } from 'fretted-strings';
import extract from 'fretted-strings';
import { AdapterFixture } from './testing/adapter-fixture';

function createFixture(name: string) {
Expand Down Expand Up @@ -67,16 +67,15 @@ describe('getDefinitionAndBoundSpan', () => {
},
])('should return no definition info for $name .', ({ source }) => {
const fixture = createFixture('input.ts');
const frets: Frets = {};
fixture.source = mark(source, frets);
const [content, frets] = extract(source);
fixture.source = content;
fixture.adapter.getDefinitionAndBoundSpan(delegateFn, 'input.ts', frets.s1.pos);
expect(delegateFn).toHaveBeenCalledTimes(1);
});

it('should return definition info when cursor is on fragment spread', () => {
const fixture = createFixture('input.ts');
const frets: Frets = {};
fixture.source = mark(
const [content, frets] = extract(
`
const query = \`
query MyQuery {
Expand All @@ -92,8 +91,8 @@ describe('getDefinitionAndBoundSpan', () => {
}
\`;
`,
frets,
);
fixture.source = content;
const actual = fixture.adapter.getDefinitionAndBoundSpan(delegateFn, 'input.ts', frets.s1.pos);
expect(actual).toMatchObject({
textSpan: {
Expand All @@ -114,15 +113,7 @@ describe('getDefinitionAndBoundSpan', () => {

it('should return definition to other file', () => {
const fixture = createFixture('input.ts');
const frets: Frets = {};
fixture.registerFragment(
'fragments.ts',
`
fragment MyFragment on Query {
__typename
}
`,
).source = mark(
const [content, frets] = extract(
`
const query = \`
query MyQuery {
Expand All @@ -132,8 +123,15 @@ describe('getDefinitionAndBoundSpan', () => {
}
\`;
`,
frets,
);
fixture.registerFragment(
'fragments.ts',
`
fragment MyFragment on Query {
__typename
}
`,
).source = content;
const actual = fixture.adapter.getDefinitionAndBoundSpan(delegateFn, 'input.ts', frets.s1.pos);
expect(actual?.definitions?.[0].fileName).toBe('fragments.ts');
});
Expand Down

0 comments on commit c46a365

Please sign in to comment.