Skip to content

Commit d39b85e

Browse files
committed
Added in a check on script tags in theme app extensions to make sure importmap is not being used
1 parent b9237f9 commit d39b85e

File tree

10 files changed

+98
-293
lines changed

10 files changed

+98
-293
lines changed

.changeset/chilled-bugs-juggle.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/yellow-insects-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/theme-check-common': minor
3+
---
4+
5+
This theme check will check for and suggest removing script tags with type importmap on theme app extensions

packages/theme-check-common/src/checks/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { ValidSchemaName } from './valid-schema-name';
4545
import { ValidStaticBlockType } from './valid-static-block-type';
4646
import { VariableName } from './variable-name';
4747
import { MissingSchema } from './missing-schema';
48-
import { ValidBlockPresetSettings } from './valid-block-preset-settings';
48+
import { NoImportmap } from './no-import-map';
4949

5050
export const allChecks: (LiquidCheckDefinition | JSONCheckDefinition)[] = [
5151
AppBlockValidTags,
@@ -93,7 +93,7 @@ export const allChecks: (LiquidCheckDefinition | JSONCheckDefinition)[] = [
9393
ValidStaticBlockType,
9494
VariableName,
9595
ValidSchemaName,
96-
ValidBlockPresetSettings,
96+
NoImportmap,
9797
];
9898

9999
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { NoImportmap } from '.';
3+
import { check as reportOffenses } from '../../test';
4+
5+
describe('Module: NoImportmap', () => {
6+
it('should report offense when using <script type="importmap">', async () => {
7+
const file = `<script type="importmap">
8+
{
9+
"imports": {
10+
"a.js": "https://foo.bar/baz.js"
11+
}
12+
}
13+
</script>`;
14+
const startIndex = file.indexOf('<script');
15+
const endIndex = file.indexOf('</script>') + '</script>'.length;
16+
17+
const offenses = await reportOffenses({ 'code.liquid': file }, [NoImportMap]);
18+
19+
expect(offenses).to.have.length(1);
20+
const { message, start, end } = offenses[0];
21+
22+
expect(message).toEqual(
23+
'Until browsers permit multiple importmap entries, only themes can have an importmap',
24+
);
25+
expect(start.index).toEqual(startIndex);
26+
expect(end.index).toEqual(endIndex);
27+
28+
expect(offenses[0].suggest).to.have.length(1);
29+
expect(offenses[0]!.suggest![0].message).to.equal("Remove the 'importmap' script tag");
30+
});
31+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ConfigTarget, LiquidCheckDefinition, Severity, SourceCodeType } from '../../types';
2+
3+
import { hasAttributeValueOf, isAttr, isValuedHtmlAttribute } from '../utils';
4+
5+
export const NoImportmap: LiquidCheckDefinition = {
6+
meta: {
7+
code: 'NoImportmap',
8+
name: 'Import map in theme app extensions',
9+
docs: {
10+
description:
11+
'Report offenses associated with import maps on script tags in theme app extensions',
12+
url: 'https://shopify.dev/docs/storefronts/themes/tools/theme-check/checks/no-import-map',
13+
},
14+
type: SourceCodeType.LiquidHtml,
15+
severity: Severity.ERROR,
16+
schema: {},
17+
targets: [ConfigTarget.ThemeAppExtension],
18+
},
19+
20+
create(context) {
21+
return {
22+
async HtmlRawNode(node) {
23+
if (node.name !== 'script') {
24+
return;
25+
}
26+
27+
const typeImportMap = node.attributes
28+
.filter(isValuedHtmlAttribute)
29+
.some((attr) => isAttr(attr, 'type') && hasAttributeValueOf(attr, 'importmap'));
30+
31+
const typeModule = node.attributes
32+
.filter(isValuedHtmlAttribute)
33+
.some((attr) => isAttr(attr, 'type') && hasAttributeValueOf(attr, 'importmap'));
34+
35+
if (!typeImportMap || !typeModule) {
36+
return;
37+
}
38+
39+
context.report({
40+
message:
41+
'Until browsers permit multiple importmap entries, only themes can have an importmap',
42+
startIndex: node.position.start,
43+
endIndex: node.position.end,
44+
suggest: [
45+
{
46+
message: `Remove the 'importmap' script tag`,
47+
fix: (corrector) => corrector.remove(node.position.start, node.position.end),
48+
},
49+
],
50+
});
51+
},
52+
};
53+
},
54+
};

packages/theme-check-common/src/checks/valid-block-preset-settings/index.spec.ts

Lines changed: 0 additions & 166 deletions
This file was deleted.

packages/theme-check-common/src/checks/valid-block-preset-settings/index.ts

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)