34dcede690
This fixes broken links in the eslint output by removing the trailing full stop. It also makes the formatting of (the output of) the various rules consistent. ## Documentation / Examples - [x] Make sure the linting passes by running `yarn lint` > I don't think this is a bug, nor a feature, nor is it really documentation. > It's just a small nuisance that I bumped into and felt compelled to fix. > I went with documentation as that seems the closest match ## What does this pull request do? The elslint output of `eslint-plugin-next` contains useful links to the documentation about the various rules. Unfortunately, on most (but not all) rules, those links are immediately followed by a full stop (`.`). The terminal (or any parser) has no way of knowing that the full stop is not part of the URL. So it includes it and clicking the link leads to a 404 on the nextjs.org website. ![eslint](https://user-images.githubusercontent.com/1708494/147452577-43ad4ce7-df75-4d48-ab78-70b9b8212b7e.png) This PR fixes that by removing the full stop. ## But a final full stop is better grammar I considered alternatives (such as [a zero-width space character](https://en.wikipedia.org/wiki/Zero-width_space#Prohibited_in_URLs)) in case the final full stop was part of the style guide or something. However, as I went through the eslint rules, I notices that the messages for various rules were formatted inconsistently. Some with final full stop, some without. As such, I made the all consistent with this structure: > [message]. See: [url] I feel this is a better solution than using the zero-width space as these sort of invisible characters in code can be a red flag that something fishy is going on. I submit this pull request in the hope it will be useful, and a positive contribution to a project I have a great deal of appreciation for. That being said, I fully understand if people would consider this a non-issue.
78 lines
2.5 KiB
JavaScript
78 lines
2.5 KiB
JavaScript
const NodeAttributes = require('../utils/node-attributes.js')
|
|
|
|
const SUPPORTED_SRCS = [
|
|
'www.google-analytics.com/analytics.js',
|
|
'www.googletagmanager.com/gtag/js',
|
|
]
|
|
const SUPPORTED_HTML_CONTENT_URLS = [
|
|
'www.google-analytics.com/analytics.js',
|
|
'www.googletagmanager.com/gtm.js',
|
|
]
|
|
const ERROR_MSG =
|
|
'Use the `next/script` component for loading third party scripts. See: https://nextjs.org/docs/messages/next-script-for-ga'
|
|
|
|
// Check if one of the items in the list is a substring of the passed string
|
|
const containsStr = (str, strList) => {
|
|
return strList.some((s) => str.includes(s))
|
|
}
|
|
|
|
module.exports = {
|
|
meta: {
|
|
docs: {
|
|
description:
|
|
'Prefer next script component when using the inline script for Google Analytics',
|
|
recommended: true,
|
|
url: 'https://nextjs.org/docs/messages/next-script-for-ga',
|
|
},
|
|
},
|
|
create: function (context) {
|
|
return {
|
|
JSXOpeningElement(node) {
|
|
if (node.name.name !== 'script') {
|
|
return
|
|
}
|
|
if (node.attributes.length === 0) {
|
|
return
|
|
}
|
|
const attributes = new NodeAttributes(node)
|
|
|
|
// Check if the Alternative async tag is being used to add GA.
|
|
// https://developers.google.com/analytics/devguides/collection/analyticsjs#alternative_async_tag
|
|
// https://developers.google.com/analytics/devguides/collection/gtagjs
|
|
if (
|
|
typeof attributes.value('src') === 'string' &&
|
|
containsStr(attributes.value('src'), SUPPORTED_SRCS)
|
|
) {
|
|
return context.report({
|
|
node,
|
|
message: ERROR_MSG,
|
|
})
|
|
}
|
|
|
|
// Check if inline script is being used to add GA.
|
|
// https://developers.google.com/analytics/devguides/collection/analyticsjs#the_google_analytics_tag
|
|
// https://developers.google.com/tag-manager/quickstart
|
|
if (
|
|
attributes.value('dangerouslySetInnerHTML') &&
|
|
attributes.value('dangerouslySetInnerHTML').length > 0
|
|
) {
|
|
const htmlContent =
|
|
attributes.value('dangerouslySetInnerHTML')[0].value.quasis &&
|
|
attributes.value('dangerouslySetInnerHTML')[0].value.quasis[0].value
|
|
.raw
|
|
if (
|
|
htmlContent &&
|
|
containsStr(htmlContent, SUPPORTED_HTML_CONTENT_URLS)
|
|
) {
|
|
context.report({
|
|
node,
|
|
message: ERROR_MSG,
|
|
})
|
|
}
|
|
}
|
|
},
|
|
}
|
|
},
|
|
}
|
|
|
|
module.exports.schema = []
|