Skip to content

Commit

Permalink
chore: match svelte and kit eslint config (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Aug 23, 2023
1 parent 462ba22 commit f4d5113
Show file tree
Hide file tree
Showing 43 changed files with 252 additions and 187 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
plugins: ['@typescript-eslint', 'html', 'markdown'],
plugins: ['@typescript-eslint', 'html', 'markdown', 'unicorn'],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
Expand Down Expand Up @@ -56,7 +56,9 @@ module.exports = {
],
'n/no-unpublished-import': 'off',
'n/no-unpublished-require': 'off',
'no-process-exit': 'off'
'no-process-exit': 'off',
quotes: ['error', 'single', { avoidEscape: true }],
'unicorn/prefer-node-protocol': 'error'
},
overrides: [
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-svelte": "^2.32.4",
"eslint-plugin-unicorn": "^48.0.1",
"execa": "^7.2.0",
"fs-extra": "^11.1.1",
"husky": "^8.0.3",
Expand Down
6 changes: 3 additions & 3 deletions packages/e2e-tests/_test_dependencies/vite-plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require('path');
const fs = require('fs');
const path = require('node:path');
const fs = require('node:fs');
/**
* Ensure transform flow is not interrupted
* @returns {import('vite').Plugin[]}
Expand Down Expand Up @@ -68,7 +68,7 @@ function writeResolvedConfig() {
fs.mkdirSync(dir);
}
const filename = path.join(dir, `vite.config.${cmd}${config.build.ssr ? '.ssr' : ''}.json`);
fs.writeFileSync(filename, JSON.stringify(serializableConfig, replacer, `\t`), 'utf-8');
fs.writeFileSync(filename, JSON.stringify(serializableConfig, replacer, '\t'), 'utf-8');
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/e2e-tests/autoprefixer-browerslist/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sveltePreprocess from 'svelte-preprocess';
import path from 'path';
import { fileURLToPath } from 'url';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

export default {
preprocess: sveltePreprocess({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (!isBuild) {
// TODO split into different tests
it('should load custom cjs config and work', async () => {
await editViteConfig((c) =>
c.replace(/svelte\([^)]*\)/, `svelte({configFile:'svelte.config.custom.cjs'})`)
c.replace(/svelte\([^)]*\)/, "svelte({configFile:'svelte.config.custom.cjs'})")
);
expect(e2eServer.logs.server.out).toContain('custom svelte config loaded cjs');
expect(await page.textContent('h1')).toMatch('Hello world!');
Expand All @@ -23,7 +23,7 @@ if (!isBuild) {

it('should not read default config when explicitly disabled', async () => {
const currentLogPos = e2eServer.logs.server.out.length;
await editViteConfig((c) => c.replace(/svelte\([^)]*\)/, `svelte({configFile: false})`));
await editViteConfig((c) => c.replace(/svelte\([^)]*\)/, 'svelte({configFile: false})'));
const logsAfterChange = e2eServer.logs.server.out.slice(currentLogPos);
expect(logsAfterChange).not.toContain('default svelte config loaded');
expect(await page.textContent('h1')).toMatch('Hello world!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ it('should load default config and work', async () => {

it('should load custom cjs config and work', async () => {
await editViteConfig((c) =>
c.replace('svelte()', `svelte({configFile:'svelte.config.custom.cjs'})`)
c.replace('svelte()', "svelte({configFile:'svelte.config.custom.cjs'})")
);
expect(await page.textContent('h1')).toMatch('Hello world!');
expect(await page.textContent('#test-child')).toBe('test-child');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('should apply css compiled from scss', async () => {
if (!isBuild) {
test('should generate sourcemap', async () => {
const style = await getText('style[data-vite-dev-id*="App.svelte"]');
const lines = style.split(`\n`).map((l) => l.trim());
const lines = style.split('\n').map((l) => l.trim());
const css = lines[0];
const mapComment = lines[1];
expect(css).toBe(
Expand Down
4 changes: 2 additions & 2 deletions packages/e2e-tests/e2e-server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// script to start package.json dev/build/preview scripts with execa for e2e tests
import { execa } from 'execa';
import treeKill from 'tree-kill';
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
const isWin = process.platform === 'win32';

async function startedOnPort(serverProcess, port, timeout) {
Expand Down
100 changes: 50 additions & 50 deletions packages/e2e-tests/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,53 +62,53 @@ if (!isBuild) {

test('should have expected initial state', async () => {
// initial state, both counters 0, both labels red
expect(await getText(`#hmr-test-1 .counter`)).toBe('0');
expect(await getText(`#hmr-test-2 .counter`)).toBe('0');
expect(await getText(`#hmr-test-1 .label`)).toBe('hmr-test');
expect(await getText(`#hmr-test-2 .label`)).toBe('hmr-test');
expect(await getColor(`#hmr-test-1 .label`)).toBe('red');
expect(await getColor(`#hmr-test-2 .label`)).toBe('red');
expect(await getText('#hmr-test-1 .counter')).toBe('0');
expect(await getText('#hmr-test-2 .counter')).toBe('0');
expect(await getText('#hmr-test-1 .label')).toBe('hmr-test');
expect(await getText('#hmr-test-2 .label')).toBe('hmr-test');
expect(await getColor('#hmr-test-1 .label')).toBe('red');
expect(await getColor('#hmr-test-2 .label')).toBe('red');
});

test('should have working increment button', async () => {
// increment counter of one instance to have local state to verify after hmr updates
await (await getEl(`#hmr-test-1 .increment`)).click();
await (await getEl('#hmr-test-1 .increment')).click();
await sleep(50);

// counter1 = 1, counter2 = 0
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getText(`#hmr-test-2 .counter`)).toBe('0');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
expect(await getText('#hmr-test-2 .counter')).toBe('0');
});

test('should apply css changes in HmrTest.svelte', async () => {
// update style, change label color from red to green
await updateHmrTest((content) => content.replace('color: red', 'color: green'));

// counter state should remain
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getText(`#hmr-test-2 .counter`)).toBe('0');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
expect(await getText('#hmr-test-2 .counter')).toBe('0');

// color should have changed
expect(await getColor(`#hmr-test-1 .label`)).toBe('green');
expect(await getColor(`#hmr-test-2 .label`)).toBe('green');
expect(await getColor('#hmr-test-1 .label')).toBe('green');
expect(await getColor('#hmr-test-2 .label')).toBe('green');
});

test('should apply js change in HmrTest.svelte ', async () => {
// update script, change label value
await updateHmrTest((content) =>
content.replace("const label = 'hmr-test'", "const label = 'hmr-test-updated'")
);
expect(await getText(`#hmr-test-1 .label`)).toBe('hmr-test-updated');
expect(await getText(`#hmr-test-2 .label`)).toBe('hmr-test-updated');
expect(await getText('#hmr-test-1 .label')).toBe('hmr-test-updated');
expect(await getText('#hmr-test-2 .label')).toBe('hmr-test-updated');
});

test('should keep state of external store intact on change of HmrTest.svelte', async () => {
// counter state should remain
await updateHmrTest((content) =>
content.replace('<!-- HMR-TEMPLATE-INJECT -->', '<span/>\n<!-- HMR-TEMPLATE-INJECT -->')
);
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getText(`#hmr-test-2 .counter`)).toBe('0');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
expect(await getText('#hmr-test-2 .counter')).toBe('0');
});

test('should preserve state of external store used by HmrTest.svelte when editing App.svelte', async () => {
Expand All @@ -120,53 +120,53 @@ if (!isBuild) {
)
);
// counter state is preserved
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getText(`#hmr-test-2 .counter`)).toBe('0');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
expect(await getText('#hmr-test-2 .counter')).toBe('0');
// a third instance has been added
expect(await getText(`#hmr-test-3 .counter`)).toBe('0');
expect(await getText('#hmr-test-3 .counter')).toBe('0');
});

test('should preserve state of store when editing hmr-stores.js', async () => {
// change state
await (await getEl(`#hmr-test-2 .increment`)).click();
await (await getEl('#hmr-test-2 .increment')).click();
await sleep(50);
// update store
await updateStore((content) => `${content}\n/*trigger change*/\n`);
// counter state is preserved
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getText(`#hmr-test-2 .counter`)).toBe('1');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
expect(await getText('#hmr-test-2 .counter')).toBe('1');
// a third instance has been added
expect(await getText(`#hmr-test-3 .counter`)).toBe('0');
expect(await getText('#hmr-test-3 .counter')).toBe('0');
});

test('should work when editing script context="module"', async () => {
expect(await getText(`#hmr-with-context`)).toContain('x=0 y=1 slot=1');
expect(await getText(`#hmr-without-context`)).toContain('x=0 y=1 slot=');
expect(await getText('#hmr-with-context')).toContain('x=0 y=1 slot=1');
expect(await getText('#hmr-without-context')).toContain('x=0 y=1 slot=');
expect(hmrCount('UsingNamed.svelte'), 'updates for UsingNamed.svelte').toBe(0);
expect(hmrCount('UsingDefault.svelte'), 'updates for UsingDefault.svelte').toBe(0);
await updateModuleContext((content) => content.replace('y = 1', 'y = 2'));
expect(await getText(`#hmr-with-context`)).toContain('x=0 y=2 slot=2');
expect(await getText(`#hmr-without-context`)).toContain('x=0 y=2 slot=');
expect(await getText('#hmr-with-context')).toContain('x=0 y=2 slot=2');
expect(await getText('#hmr-without-context')).toContain('x=0 y=2 slot=');
expect(hmrCount('UsingNamed.svelte'), 'updates for UsingNamed.svelte').toBe(1);
expect(hmrCount('UsingDefault.svelte'), 'updates for UsingDefault.svelte').toBe(0);
});

test('should work with emitCss: false in vite config', async () => {
await editViteConfig((c) => c.replace('svelte()', 'svelte({emitCss:false})'));
expect(await getText(`#hmr-test-1 .counter`)).toBe('0');
expect(await getColor(`#hmr-test-1 .label`)).toBe('green');
await (await getEl(`#hmr-test-1 .increment`)).click();
expect(await getText('#hmr-test-1 .counter')).toBe('0');
expect(await getColor('#hmr-test-1 .label')).toBe('green');
await (await getEl('#hmr-test-1 .increment')).click();
await sleep(50);
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
await updateHmrTest((content) => content.replace('color: green', 'color: red'));
expect(await getColor(`#hmr-test-1 .label`)).toBe('red');
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getColor('#hmr-test-1 .label')).toBe('red');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
});

test('should work with emitCss: false in svelte config', async () => {
addFile('svelte.config.cjs', `module.exports={vitePlugin:{emitCss:false}}`);
addFile('svelte.config.cjs', 'module.exports={vitePlugin:{emitCss:false}}');
await waitForServerRestartAndPageReload();
expect(await getColor(`#hmr-test-1 .label`)).toBe('red');
expect(await getColor('#hmr-test-1 .label')).toBe('red');
removeFile('svelte.config.cjs');
});

Expand All @@ -188,14 +188,14 @@ if (!isBuild) {
);
await waitForServerRestartAndPageReload();
expect(await getText('#preprocess-inject')).toBe('Injected');
expect(await getText(`#hmr-test-1 .counter`)).toBe('0');
expect(await getColor(`#hmr-test-1 .label`)).toBe('red');
await (await getEl(`#hmr-test-1 .increment`)).click();
expect(await getText('#hmr-test-1 .counter')).toBe('0');
expect(await getColor('#hmr-test-1 .label')).toBe('red');
await (await getEl('#hmr-test-1 .increment')).click();
await sleep(50);
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
await updateHmrTest((content) => content.replace('color: red', 'color: green'));
expect(await getColor(`#hmr-test-1 .label`)).toBe('green');
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getColor('#hmr-test-1 .label')).toBe('green');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
await editFile('svelte.config.cjs', (content) =>
content
.replace('preprocess-inject', 'preprocess-inject-2')
Expand All @@ -204,20 +204,20 @@ if (!isBuild) {
await waitForServerRestartAndPageReload();
expect(await getText('#preprocess-inject-2')).toBe('Injected 2');
expect(await getEl('#preprocess-inject')).toBe(null);
expect(await getColor(`#hmr-test-1 .label`)).toBe('green');
expect(await getText(`#hmr-test-1 .counter`)).toBe('0');
await (await getEl(`#hmr-test-1 .increment`)).click();
expect(await getColor('#hmr-test-1 .label')).toBe('green');
expect(await getText('#hmr-test-1 .counter')).toBe('0');
await (await getEl('#hmr-test-1 .increment')).click();
await sleep(50);
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
await updateHmrTest((content) => content.replace('color: green', 'color: red'));
expect(await getColor(`#hmr-test-1 .label`)).toBe('red');
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
expect(await getColor('#hmr-test-1 .label')).toBe('red');
expect(await getText('#hmr-test-1 .counter')).toBe('1');
await removeFile('svelte.config.cjs');
await waitForServerRestartAndPageReload();
expect(await getEl('#preprocess-inject-2')).toBe(null);
expect(await getEl('#preprocess-inject')).toBe(null);
expect(await getColor(`#hmr-test-1 .label`)).toBe('red');
expect(await getText(`#hmr-test-1 .counter`)).toBe('0');
expect(await getColor('#hmr-test-1 .label')).toBe('red');
expect(await getText('#hmr-test-1 .counter')).toBe('0');
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('raw', () => {
expect(result).toMatchFileSnapshot(snapshotFilename('preprocessed'));
});

test(`Dummy.svelte?raw&svelte&type=script`, async () => {
test('Dummy.svelte?raw&svelte&type=script', async () => {
const result = await getText('#script');
expect(normalizeSnapshot(result)).toMatchFileSnapshot(snapshotFilename('script'));
});
Expand Down
Loading

0 comments on commit f4d5113

Please sign in to comment.